Skip to content

Commit

Permalink
Merge pull request #1701 from louisscruz/master
Browse files Browse the repository at this point in the history
[fix] `boolean-prop-naming`: Handle `.isRequired`
  • Loading branch information
ljharb authored Feb 23, 2018
2 parents b7665b7 + f980c38 commit 9b16cab
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ module.exports = {
* propTypes: {
* full: React.PropTypes.bool,
* short: PropTypes.bool,
* direct: bool
* direct: bool,
* required: PropTypes.bool.isRequired
* }
* @param {Object} node The node we're getting the name of
*/
Expand All @@ -73,7 +74,11 @@ module.exports = {
return null;
}
if (node.value.property) {
return node.value.property.name;
const name = node.value.property.name;
if (name === 'isRequired') {
return node.value.object.property.name;
}
return name;
}
if (node.value.type === 'Identifier') {
return node.value.name;
Expand Down
98 changes: 98 additions & 0 deletions tests/lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,47 @@ ruleTester.run('boolean-prop-naming', rule, {
options: [{
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+'
}]
}, {
// Ensure the rule does not throw when a prop isRequired when ES5.
code: `
var Hello = createReactClass({
propTypes: {isSomething: PropTypes.bool.isRequired, hasValue: PropTypes.bool.isRequired},
render: function() { return <div />; }
});
`
}, {
// Ensure the rule does not throw when a prop isRequired when ES6 with static properties.
code: `
class Hello extends React.Component {
static propTypes = {
isSomething: PropTypes.bool.isRequired,
hasValue: PropTypes.bool.isRequired
};
render() {
return (
<div />
);
}
}
`,
parser: 'babel-eslint'
}, {
// Ensure the rule does not throw when a prop isRequired when ES6 without static properties.
code: `
class Hello extends React.Component {
render() {
return (
<div />
);
}
}
Hello.propTypes = {
isSomething: PropTypes.bool.isRequired,
hasValue: PropTypes.bool.isRequired
}
`
}],

invalid: [{
Expand Down Expand Up @@ -664,5 +705,62 @@ ruleTester.run('boolean-prop-naming', rule, {
errors: [{
message: 'It is better if your prop (something) matches this pattern: (^is[A-Z]([A-Za-z0-9]?)+)'
}]
}, {
// Works when a prop isRequired in ES5.
code: `
var Hello = createReactClass({
propTypes: {something: PropTypes.bool.isRequired},
render: function() { return <div />; }
});
`,
options: [{
rule: '^is[A-Z]([A-Za-z0-9]?)+'
}],
errors: [{
message: 'Prop name (something) doesn\'t match rule (^is[A-Z]([A-Za-z0-9]?)+)'
}]
}, {
// Works when a prop isRequired in ES6 with static properties.
code: `
class Hello extends React.Component {
static propTypes = {
something: PropTypes.bool.isRequired
};
render() {
return (
<div />
);
}
}
`,
options: [{
rule: '^is[A-Z]([A-Za-z0-9]?)+'
}],
parser: 'babel-eslint',
errors: [{
message: 'Prop name (something) doesn\'t match rule (^is[A-Z]([A-Za-z0-9]?)+)'
}]
}, {
// Works when a prop isRequired in ES6 without static properties.
code: `
class Hello extends React.Component {
render() {
return (
<div />
);
}
}
Hello.propTypes = {
something: PropTypes.bool.isRequired
}
`,
options: [{
rule: '^is[A-Z]([A-Za-z0-9]?)+'
}],
errors: [{
message: 'Prop name (something) doesn\'t match rule (^is[A-Z]([A-Za-z0-9]?)+)'
}]
}]
});

0 comments on commit 9b16cab

Please sign in to comment.