Skip to content

Commit

Permalink
Check class properties that use prop wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
jomasti committed Nov 22, 2017
1 parent c8b3f16 commit fecd9a4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,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 @@ -148,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 @@ -166,7 +176,7 @@ module.exports = {
}
const right = node.parent.right;
if (right.type === 'CallExpression' && propWrapperFunctions.has(sourceCode.getText(right.callee))) {
right.arguments.filter(arg => arg.type === 'ObjectExpression').forEach(object => validatePropNaming(component.node, object.properties));
checkPropWrapperArguments(component.node, right.arguments);
return;
}
validatePropNaming(component.node, node.parent.right.properties);
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,5 +614,25 @@ ruleTester.run('boolean-prop-naming', rule, {
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 fecd9a4

Please sign in to comment.