Skip to content

Commit d8e65c5

Browse files
committed
Support function call for setting propTypes
For example, using `Object.assign` for setting the propTypes like this: ``` function Foo() {...} Foo.propTypes = Object.assign({}, morePropTypes, { isLoading: PropTypes.bool }); ```
1 parent a120758 commit d8e65c5

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

lib/rules/boolean-prop-naming.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@ module.exports = {
180180
return;
181181
}
182182
const component = utils.getRelatedComponent(node);
183-
if (!component || !node.parent.right.properties) {
183+
if (!component || !node.parent.right) {
184+
return;
185+
}
186+
const right = node.parent.right;
187+
if (right.type === 'CallExpression') {
188+
right.arguments.filter(arg => arg.type === 'ObjectExpression').forEach(object => validatePropNaming(component.node, object.properties));
184189
return;
185190
}
186191
validatePropNaming(component.node, node.parent.right.properties);

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,7 @@ ruleTester.run('boolean-prop-naming', rule, {
101101
options: [{
102102
rule: '^is[A-Z]([A-Za-z0-9]?)+'
103103
}]
104-
}, {
105-
// ES6 components as React.Component with non-boolean PropTypes
106-
code: `
107-
class Hello extends React.Component {
108-
render () { return <div />; }
109-
}
110-
Hello.propTypes = wrap({ a: PropTypes.bool })
111-
`,
112-
options: [{
113-
rule: '^is[A-Z]([A-Za-z0-9]?)+'
114-
}]
104+
115105
}, {
116106
code: `
117107
class Hello extends React.Component {
@@ -458,5 +448,32 @@ ruleTester.run('boolean-prop-naming', rule, {
458448
}, {
459449
message: 'Prop name (somethingElse) doesn\'t match rule (^is[A-Z]([A-Za-z0-9]?)+)'
460450
}]
451+
}, {
452+
code: `
453+
function Card(props) {
454+
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
455+
}
456+
Card.propTypes = merge({}, Card.propTypes, {
457+
showScore: PropTypes.bool
458+
});`,
459+
options: [{
460+
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+'
461+
}],
462+
errors: [{
463+
message: 'Prop name (showScore) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)'
464+
}]
465+
}, {
466+
code: `
467+
class Hello extends React.Component {
468+
render () { return <div />; }
469+
}
470+
Hello.propTypes = wrap({ a: PropTypes.bool })
471+
`,
472+
options: [{
473+
rule: '^is[A-Z]([A-Za-z0-9]?)+'
474+
}],
475+
errors: [{
476+
message: 'Prop name (a) doesn\'t match rule (^is[A-Z]([A-Za-z0-9]?)+)'
477+
}]
461478
}]
462479
});

0 commit comments

Comments
 (0)