diff --git a/README.md b/README.md index 9084d6ad27..44cdb6a64e 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ You can also specify some settings that will be shared across all the plugin rul "flowVersion": "0.53" // Flow version }, "propWrapperFunctions": [ "forbidExtraProps" ] // The names of any functions used to wrap the propTypes object, such as `forbidExtraProps`. If this isn't set, any propTypes wrapped in a function will be skipped. + "propObjectAssignFunctions": [ "Object.assign" ] // The names of functions used to merge props when declaring propTypes. If this isn't set, any propTypes set using any assign/merge function will be skipped. } } ``` diff --git a/lib/rules/boolean-prop-naming.js b/lib/rules/boolean-prop-naming.js index c627ebe695..a7153f979d 100644 --- a/lib/rules/boolean-prop-naming.js +++ b/lib/rules/boolean-prop-naming.js @@ -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(); @@ -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); diff --git a/tests/lib/rules/boolean-prop-naming.js b/tests/lib/rules/boolean-prop-naming.js index bbaf6e2d0a..abbfb8a91b 100644 --- a/tests/lib/rules/boolean-prop-naming.js +++ b/tests/lib/rules/boolean-prop-naming.js @@ -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
{props.showScore ? 'yeh' : 'no'}
; + } + Card.propTypes = merge({}, Card.propTypes, { + showScore: PropTypes.bool + });`, + options: [{ + rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+' + }] }], invalid: [{ @@ -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
{props.showScore ? 'yeh' : 'no'}
; + } + 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
{props.showScore ? 'yeh' : 'no'}
; + } + 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
{props.showScore ? 'yeh' : 'no'}
; + } + 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]?)+)' + }] }] });