From 9d0707e912f4e4da39bfec650a69a860fea4c4c8 Mon Sep 17 00:00:00 2001 From: Diana Suvorova Date: Fri, 14 Jul 2017 13:05:28 -0700 Subject: [PATCH 1/2] no ignoring components with no used props --- lib/rules/no-unused-prop-types.js | 13 +++- tests/lib/rules/no-unused-prop-types.js | 83 +++++-------------------- 2 files changed, 25 insertions(+), 71 deletions(-) diff --git a/lib/rules/no-unused-prop-types.js b/lib/rules/no-unused-prop-types.js index 5127971d1a..752f15e31b 100644 --- a/lib/rules/no-unused-prop-types.js +++ b/lib/rules/no-unused-prop-types.js @@ -175,7 +175,6 @@ module.exports = { function mustBeValidated(component) { return Boolean( component && - component.usedPropTypes && !component.ignorePropsValidation ); } @@ -234,8 +233,9 @@ module.exports = { * @returns {Boolean} True if the prop is used, false if not. */ function isPropUsed(node, prop) { - for (let i = 0, l = node.usedPropTypes.length; i < l; i++) { - const usedProp = node.usedPropTypes[i]; + const usedPropTypes = node.usedPropTypes || []; + for (let i = 0, l = usedPropTypes.length; i < l; i++) { + const usedProp = usedPropTypes[i]; if ( prop.type === 'shape' || prop.name === '__ANY_KEY__' || @@ -897,6 +897,13 @@ module.exports = { } }, + JSXSpreadAttribute: function(node) { + const component = components.get(utils.getParentComponent()); + components.set(component ? component.node : node, { + ignorePropsValidation: true + }); + }, + MethodDefinition: function(node) { if (!isPropTypesDeclaration(node.key)) { return; diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index eff0e9c962..c0e39a78e2 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -557,26 +557,6 @@ ruleTester.run('no-unused-prop-types', rule, { '});' ].join('\n'), options: [{customValidators: ['CustomValidator']}] - }, { - code: [ - 'class Comp1 extends Component {', - ' render() {', - ' return ;', - ' }', - '}', - 'Comp1.propTypes = {', - ' prop1: PropTypes.number', - '};', - 'class Comp2 extends Component {', - ' render() {', - ' return ;', - ' }', - '}', - 'Comp2.propTypes = {', - ' prop2: PropTypes.arrayOf(Comp1.propTypes.prop1)', - '};' - ].join('\n'), - parser: 'babel-eslint' }, { code: [ 'const SomeComponent = createReactClass({', @@ -782,54 +762,6 @@ ruleTester.run('no-unused-prop-types', rule, { ' }', '});' ].join('\n') - }, { - code: [ - 'const statelessComponent = (props) => {', - ' const subRender = () => {', - ' return {props.someProp};', - ' };', - ' return
{subRender()}
;', - '};', - 'statelessComponent.propTypes = {', - ' someProp: PropTypes.string', - '};' - ].join('\n') - }, { - code: [ - 'const statelessComponent = ({ someProp }) => {', - ' const subRender = () => {', - ' return {someProp};', - ' };', - ' return
{subRender()}
;', - '};', - 'statelessComponent.propTypes = {', - ' someProp: PropTypes.string', - '};' - ].join('\n') - }, { - code: [ - 'const statelessComponent = function({ someProp }) {', - ' const subRender = () => {', - ' return {someProp};', - ' };', - ' return
{subRender()}
;', - '};', - 'statelessComponent.propTypes = {', - ' someProp: PropTypes.string', - '};' - ].join('\n') - }, { - code: [ - 'function statelessComponent({ someProp }) {', - ' const subRender = () => {', - ' return {someProp};', - ' };', - ' return
{subRender()}
;', - '};', - 'statelessComponent.propTypes = {', - ' someProp: PropTypes.string', - '};' - ].join('\n') }, { code: [ 'function notAComponent({ something }) {', @@ -3215,6 +3147,21 @@ ruleTester.run('no-unused-prop-types', rule, { line: 11, column: 8 }] + }, { // None of the props are used issue #1162 + code: [ + 'import React from "react"; ', + 'var Hello = React.createReactClass({', + ' propTypes: {', + ' name: React.PropTypes.string', + ' },', + ' render: function() {', + ' return
Hello Bob
;', + ' }', + '});' + ].join('\n'), + errors: [{ + message: '\'name\' PropType is defined but prop is never used' + }] } /* , { // Enable this when the following issue is fixed From 430a89f10e142dfee72ca2137f2cd523035df08c Mon Sep 17 00:00:00 2001 From: Diana Suvorova Date: Sun, 16 Jul 2017 09:40:50 -0700 Subject: [PATCH 2/2] moving test into invalid --- tests/lib/rules/no-unused-prop-types.js | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index c0e39a78e2..93960233e7 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -3162,6 +3162,34 @@ ruleTester.run('no-unused-prop-types', rule, { errors: [{ message: '\'name\' PropType is defined but prop is never used' }] + }, { + code: [ + 'class Comp1 extends Component {', + ' render() {', + ' return ;', + ' }', + '}', + 'Comp1.propTypes = {', + ' prop1: PropTypes.number', + '};', + 'class Comp2 extends Component {', + ' render() {', + ' return ;', + ' }', + '}', + 'Comp2.propTypes = {', + ' prop2: PropTypes.arrayOf(Comp1.propTypes.prop1)', + '};' + ].join('\n'), + parser: 'babel-eslint', + errors: [{ + message: '\'prop1\' PropType is defined but prop is never used' + }, { + message: '\'prop2\' PropType is defined but prop is never used' + }, { + message: '\'prop2.*\' PropType is defined but prop is never used' + }] + } /* , { // Enable this when the following issue is fixed