Skip to content

Commit 5038d51

Browse files
committed
Check class properties that use prop wrappers
1 parent a73d574 commit 5038d51

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/rules/boolean-prop-naming.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ module.exports = {
139139
});
140140
}
141141

142+
function checkPropWrapperArguments(node, args) {
143+
if (!node || !Array.isArray(args)) {
144+
return;
145+
}
146+
args.filter(arg => arg.type === 'ObjectExpression').forEach(object => validatePropNaming(node, object.properties));
147+
}
148+
142149
// --------------------------------------------------------------------------
143150
// Public
144151
// --------------------------------------------------------------------------
@@ -148,6 +155,9 @@ module.exports = {
148155
if (!rule || !propsUtil.isPropTypesDeclaration(node)) {
149156
return;
150157
}
158+
if (node.value && node.value.type === 'CallExpression' && propWrapperFunctions.has(sourceCode.getText(node.value.callee))) {
159+
checkPropWrapperArguments(node, node.value.arguments);
160+
}
151161
if (node.value && node.value.properties) {
152162
validatePropNaming(node, node.value.properties);
153163
}
@@ -166,7 +176,7 @@ module.exports = {
166176
}
167177
const right = node.parent.right;
168178
if (right.type === 'CallExpression' && propWrapperFunctions.has(sourceCode.getText(right.callee))) {
169-
right.arguments.filter(arg => arg.type === 'ObjectExpression').forEach(object => validatePropNaming(component.node, object.properties));
179+
checkPropWrapperArguments(component.node, right.arguments);
170180
return;
171181
}
172182
validatePropNaming(component.node, node.parent.right.properties);

tests/lib/rules/boolean-prop-naming.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,5 +614,25 @@ ruleTester.run('boolean-prop-naming', rule, {
614614
errors: [{
615615
message: 'Prop name (showScore) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)'
616616
}]
617+
}, {
618+
code: `
619+
class Card extends React.Component {
620+
static propTypes = forbidExtraProps({
621+
showScore: PropTypes.bool
622+
});
623+
render() {
624+
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
625+
}
626+
}`,
627+
parser: 'babel-eslint',
628+
settings: {
629+
propWrapperFunctions: ['forbidExtraProps']
630+
},
631+
options: [{
632+
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+'
633+
}],
634+
errors: [{
635+
message: 'Prop name (showScore) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)'
636+
}]
617637
}]
618638
});

0 commit comments

Comments
 (0)