Skip to content

Commit

Permalink
Merge pull request #1478 from jomasti/issue-1452
Browse files Browse the repository at this point in the history
Support propWrapperFunctions for boolean-prop-naming
  • Loading branch information
lencioni committed Dec 4, 2017
2 parents 58159c0 + fecd9a4 commit f6e4c89
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = {
const config = context.options[0] || {};
const rule = config.rule ? new RegExp(config.rule) : null;
const propTypeNames = config.propTypeNames || ['bool'];
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);

// Remembers all Flowtype object definitions
const objectTypeAnnotations = new Map();
Expand Down Expand Up @@ -138,6 +139,13 @@ module.exports = {
});
}

function checkPropWrapperArguments(node, args) {
if (!node || !Array.isArray(args)) {
return;
}
args.filter(arg => arg.type === 'ObjectExpression').forEach(object => validatePropNaming(node, object.properties));
}

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
Expand All @@ -147,6 +155,9 @@ module.exports = {
if (!rule || !propsUtil.isPropTypesDeclaration(node)) {
return;
}
if (node.value && node.value.type === 'CallExpression' && propWrapperFunctions.has(sourceCode.getText(node.value.callee))) {
checkPropWrapperArguments(node, node.value.arguments);
}
if (node.value && node.value.properties) {
validatePropNaming(node, node.value.properties);
}
Expand All @@ -160,7 +171,12 @@ module.exports = {
return;
}
const component = utils.getRelatedComponent(node);
if (!component || !node.parent.right.properties) {
if (!component || !node.parent.right) {
return;
}
const right = node.parent.right;
if (right.type === 'CallExpression' && propWrapperFunctions.has(sourceCode.getText(right.callee))) {
checkPropWrapperArguments(component.node, right.arguments);
return;
}
validatePropNaming(component.node, node.parent.right.properties);
Expand Down
119 changes: 119 additions & 0 deletions tests/lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,18 @@ ruleTester.run('boolean-prop-naming', rule, {
rule: '^is[A-Z]([A-Za-z0-9]?)+'
}],
parser: 'babel-eslint'
}, {
// No propWrapperFunctions setting
code: `
function Card(props) {
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
}
Card.propTypes = merge({}, Card.propTypes, {
showScore: PropTypes.bool
});`,
options: [{
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+'
}]
}],

invalid: [{
Expand Down Expand Up @@ -515,5 +527,112 @@ ruleTester.run('boolean-prop-naming', rule, {
}, {
message: 'Prop name (somethingElse) doesn\'t match rule (^is[A-Z]([A-Za-z0-9]?)+)'
}]
}, {
code: `
function Card(props) {
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
}
Card.propTypes = merge({}, Card.propTypes, {
showScore: PropTypes.bool
});`,
settings: {
propWrapperFunctions: ['merge']
},
options: [{
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+'
}],
errors: [{
message: 'Prop name (showScore) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)'
}]
}, {
code: `
function Card(props) {
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
}
Card.propTypes = Object.assign({}, Card.propTypes, {
showScore: PropTypes.bool
});`,
settings: {
propWrapperFunctions: ['Object.assign']
},
options: [{
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+'
}],
errors: [{
message: 'Prop name (showScore) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)'
}]
}, {
code: `
function Card(props) {
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
}
Card.propTypes = _.assign({}, Card.propTypes, {
showScore: PropTypes.bool
});`,
settings: {
propWrapperFunctions: ['_.assign']
},
options: [{
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+'
}],
errors: [{
message: 'Prop name (showScore) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)'
}]
}, {
code: `
function Card(props) {
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
}
Card.propTypes = forbidExtraProps({
showScore: PropTypes.bool
});`,
settings: {
propWrapperFunctions: ['forbidExtraProps']
},
options: [{
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+'
}],
errors: [{
message: 'Prop name (showScore) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)'
}]
}, {
code: `
class Card extends React.Component {
render() {
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
}
}
Card.propTypes = forbidExtraProps({
showScore: PropTypes.bool
});`,
settings: {
propWrapperFunctions: ['forbidExtraProps']
},
options: [{
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+'
}],
errors: [{
message: 'Prop name (showScore) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)'
}]
}, {
code: `
class Card extends React.Component {
static propTypes = forbidExtraProps({
showScore: PropTypes.bool
});
render() {
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
}
}`,
parser: 'babel-eslint',
settings: {
propWrapperFunctions: ['forbidExtraProps']
},
options: [{
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+'
}],
errors: [{
message: 'Prop name (showScore) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)'
}]
}]
});

0 comments on commit f6e4c89

Please sign in to comment.