From 56db72e56dd8cd17aa0bfd2861f5592ea2b3d60a Mon Sep 17 00:00:00 2001 From: Joshua Stiefer Date: Sat, 14 Oct 2017 14:57:24 -0700 Subject: [PATCH 1/3] Support propWrapperFunctions for bool-prop-naming --- lib/rules/boolean-prop-naming.js | 8 +++- tests/lib/rules/boolean-prop-naming.js | 63 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) 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]?)+)' + }] }] }); From c8b3f164a72ea0d968acb1854f1e0974929e8af8 Mon Sep 17 00:00:00 2001 From: Joshua Stiefer Date: Tue, 21 Nov 2017 21:41:31 -0700 Subject: [PATCH 2/3] Add more invalid tests for boolean-prop-naming --- tests/lib/rules/boolean-prop-naming.js | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/lib/rules/boolean-prop-naming.js b/tests/lib/rules/boolean-prop-naming.js index abbfb8a91b..ed0a364089 100644 --- a/tests/lib/rules/boolean-prop-naming.js +++ b/tests/lib/rules/boolean-prop-naming.js @@ -578,5 +578,41 @@ ruleTester.run('boolean-prop-naming', rule, { 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 = forbidExtraProps({ + showScore: PropTypes.bool + });`, + settings: { + propWrapperFunctions: ['forbidExtraProps'] + }, + 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: ` + class Card extends React.Component { + render() { + return
{props.showScore ? 'yeh' : 'no'}
; + } + } + Card.propTypes = forbidExtraProps({ + showScore: PropTypes.bool + });`, + settings: { + propWrapperFunctions: ['forbidExtraProps'] + }, + 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]?)+)' + }] }] }); From fecd9a4addd7956b2ea29cfd3bf98161af9df07a Mon Sep 17 00:00:00 2001 From: Joshua Stiefer Date: Tue, 21 Nov 2017 21:54:24 -0700 Subject: [PATCH 3/3] Check class properties that use prop wrappers --- lib/rules/boolean-prop-naming.js | 12 +++++++++++- tests/lib/rules/boolean-prop-naming.js | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/rules/boolean-prop-naming.js b/lib/rules/boolean-prop-naming.js index a7153f979d..45ae8f1b78 100644 --- a/lib/rules/boolean-prop-naming.js +++ b/lib/rules/boolean-prop-naming.js @@ -139,6 +139,13 @@ module.exports = { }); } + function checkPropWrapperArguments(node, args) { + if (!node || !Array.isArray(args)) { + return; + } + args.filter(arg => arg.type === 'ObjectExpression').forEach(object => validatePropNaming(node, object.properties)); + } + // -------------------------------------------------------------------------- // Public // -------------------------------------------------------------------------- @@ -148,6 +155,9 @@ module.exports = { if (!rule || !propsUtil.isPropTypesDeclaration(node)) { return; } + if (node.value && node.value.type === 'CallExpression' && propWrapperFunctions.has(sourceCode.getText(node.value.callee))) { + checkPropWrapperArguments(node, node.value.arguments); + } if (node.value && node.value.properties) { validatePropNaming(node, node.value.properties); } @@ -166,7 +176,7 @@ module.exports = { } 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)); + checkPropWrapperArguments(component.node, right.arguments); 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 ed0a364089..fa05ec6874 100644 --- a/tests/lib/rules/boolean-prop-naming.js +++ b/tests/lib/rules/boolean-prop-naming.js @@ -614,5 +614,25 @@ ruleTester.run('boolean-prop-naming', rule, { errors: [{ message: 'Prop name (showScore) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)' }] + }, { + code: ` + class Card extends React.Component { + static propTypes = forbidExtraProps({ + showScore: PropTypes.bool + }); + render() { + return
{props.showScore ? 'yeh' : 'no'}
; + } + }`, + parser: 'babel-eslint', + settings: { + propWrapperFunctions: ['forbidExtraProps'] + }, + 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]?)+)' + }] }] });