Skip to content

Commit

Permalink
Support propWrapperFunctions for bool-prop-naming
Browse files Browse the repository at this point in the history
  • Loading branch information
jomasti committed Nov 22, 2017
1 parent 6829c5c commit 56db72e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rules/boolean-prop-naming.js
Expand Up @@ -46,6 +46,7 @@ module.exports = {
const config = context.options[0] || {};
const rule = config.rule ? new RegExp(config.rule) : null;
const propTypeNames = config.propTypeNames || ['bool'];
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);

// Remembers all Flowtype object definitions
const objectTypeAnnotations = new Map();
Expand Down Expand Up @@ -160,7 +161,12 @@ module.exports = {
return;
}
const component = utils.getRelatedComponent(node);
if (!component || !node.parent.right.properties) {
if (!component || !node.parent.right) {
return;
}
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));
return;
}
validatePropNaming(component.node, node.parent.right.properties);
Expand Down
63 changes: 63 additions & 0 deletions tests/lib/rules/boolean-prop-naming.js
Expand Up @@ -295,6 +295,18 @@ ruleTester.run('boolean-prop-naming', rule, {
rule: '^is[A-Z]([A-Za-z0-9]?)+'
}],
parser: 'babel-eslint'
}, {
// No propWrapperFunctions setting
code: `
function Card(props) {
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
}
Card.propTypes = merge({}, Card.propTypes, {
showScore: PropTypes.bool
});`,
options: [{
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+'
}]
}],

invalid: [{
Expand Down Expand Up @@ -515,5 +527,56 @@ ruleTester.run('boolean-prop-naming', rule, {
}, {
message: 'Prop name (somethingElse) doesn\'t match rule (^is[A-Z]([A-Za-z0-9]?)+)'
}]
}, {
code: `
function Card(props) {
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
}
Card.propTypes = merge({}, Card.propTypes, {
showScore: PropTypes.bool
});`,
settings: {
propWrapperFunctions: ['merge']
},
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]?)+)'
}]
}, {
code: `
function Card(props) {
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
}
Card.propTypes = Object.assign({}, Card.propTypes, {
showScore: PropTypes.bool
});`,
settings: {
propWrapperFunctions: ['Object.assign']
},
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]?)+)'
}]
}, {
code: `
function Card(props) {
return <div>{props.showScore ? 'yeh' : 'no'}</div>;
}
Card.propTypes = _.assign({}, Card.propTypes, {
showScore: PropTypes.bool
});`,
settings: {
propWrapperFunctions: ['_.assign']
},
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 56db72e

Please sign in to comment.