Skip to content

Fix boolean-prop-naming with Object.spread syntax #1485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ module.exports = {
* @param {Object} node The node we're getting the name of
*/
function getPropKey(node) {
// Check for `ExperimentalSpreadProperty` so we can skip validation of those fields.
// Otherwise it will look for `node.value.property` which doesn't exist and breaks Eslint.
if (node.type === 'ExperimentalSpreadProperty') {
return null;
}
if (node.value.property) {
return node.value.property.name;
}
Expand Down
57 changes: 57 additions & 0 deletions tests/lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const parserOptions = {
ecmaVersion: 6,
sourceType: 'module',
ecmaFeatures: {
experimentalObjectRestSpread: true,
jsx: true
}
};
Expand Down Expand Up @@ -145,6 +146,31 @@ ruleTester.run('boolean-prop-naming', rule, {
rule: '^is[A-Z]([A-Za-z0-9]?)+'
}],
parser: 'babel-eslint'
}, {
// ES6 components with static class properties and Object.spread syntax in PropTypes
code: `
const spreadProps = { aSpreadProp: PropTypes.string };
class Hello extends React.Component {
static propTypes = {isSomething: PropTypes.bool, ...spreadProps};
render () { return <div />; }
}
`,
options: [{
rule: '^is[A-Z]([A-Za-z0-9]?)+'
}],
parser: 'babel-eslint'
}, {
// ES6 components as Component with boolean PropTypes and Object.spread syntax in PropTypes
code: `
const spreadProps = { aSpreadProp: PropTypes.string };
class Hello extends Component {
render () { return <div />; }
}
Hello.propTypes = {isSomething: PropTypes.bool, ...spreadProps}
`,
options: [{
rule: '^is[A-Z]([A-Za-z0-9]?)+'
}]
}, {
// ES6 components with static class properties and React.PropTypes
code: `
Expand Down Expand Up @@ -361,6 +387,37 @@ ruleTester.run('boolean-prop-naming', rule, {
errors: [{
message: 'Prop name (something) doesn\'t match rule (^is[A-Z]([A-Za-z0-9]?)+)'
}]
}, {
// ES6 components as React.Component with non-boolean PropTypes and Object.spread syntax
code: `
const spreadProps = { aSpreadProp: PropTypes.string };
class Hello extends Component {
render () { return <div />; }
}
Hello.propTypes = {something: PropTypes.bool, ...spreadProps}
`,
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]?)+)'
}]
}, {
// ES6 components as React.Component with static class property, non-boolean PropTypes, and Object.spread syntax
code: `
const spreadProps = { aSpreadProp: PropTypes.string };
class Hello extends React.Component {
static propTypes = {something: PropTypes.bool, ...spreadProps};
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]?)+)'
}]
}, {
// ES6 components as React.Component with non-boolean PropTypes
code: `
Expand Down