From dda03e934468f4b2b6f088146e070296a64ae463 Mon Sep 17 00:00:00 2001 From: Yuri S Date: Sat, 3 Feb 2018 17:45:09 +0200 Subject: [PATCH 1/5] Created test --- tests/lib/rules/no-unused-prop-types.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index 577148051e..4664de47cb 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -1228,6 +1228,20 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in componentWillReceiveProps shouldn't throw errors when used createReactClass + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillReceiveProps (nextProps) {', + ' const {something} = nextProps;', + ' doSomething(something);', + ' }', + '})' + ].join('\n'), + parser: 'babel-eslint' }, { // Destructured function props in componentWillReceiveProps shouldn't throw errors code: [ From 78e29517a285d905f109b62dccab0f76d10dc219 Mon Sep 17 00:00:00 2001 From: Yuri S Date: Sun, 4 Feb 2018 00:17:02 +0200 Subject: [PATCH 2/5] Fixed bug --- lib/rules/no-unused-prop-types.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/no-unused-prop-types.js b/lib/rules/no-unused-prop-types.js index 687b129e5e..17ac197794 100644 --- a/lib/rules/no-unused-prop-types.js +++ b/lib/rules/no-unused-prop-types.js @@ -266,7 +266,7 @@ module.exports = { * @return {Boolean} True if the node is inside a lifecycle method */ function isInLifeCycleMethod(node) { - if (node.type === 'MethodDefinition' && isNodeALifeCycleMethod(node)) { + if ((node.type === 'MethodDefinition' || node.type === 'Property') && isNodeALifeCycleMethod(node)) { return true; } From b1444be8dd8dc56a5936e540487dc2e8e3eb0d3b Mon Sep 17 00:00:00 2001 From: Yuri S Date: Sun, 4 Feb 2018 00:53:15 +0200 Subject: [PATCH 3/5] Added more tests --- tests/lib/rules/no-unused-prop-types.js | 132 ++++++++++++++++++++---- 1 file changed, 112 insertions(+), 20 deletions(-) diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index 4664de47cb..baf32be77e 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -1255,6 +1255,19 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in componentWillReceiveProps shouldn't throw errors when used createReactClass + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillReceiveProps ({something}) {', + ' doSomething(something);', + ' }', + '})' + ].join('\n'), + parser: 'babel-eslint' }, { // Destructured props in the constructor shouldn't throw errors code: [ @@ -1285,13 +1298,13 @@ ruleTester.run('no-unused-prop-types', rule, { ].join('\n'), parser: 'babel-eslint' }, { - // Destructured props in the `componentWillReceiveProps` method shouldn't throw errors + // Destructured props in the `shouldComponentUpdate` method shouldn't throw errors code: [ 'class Hello extends Component {', ' static propTypes = {', ' something: PropTypes.bool', ' }', - ' componentWillReceiveProps (nextProps, nextState) {', + ' shouldComponentUpdate (nextProps, nextState) {', ' const {something} = nextProps;', ' return something;', ' }', @@ -1299,43 +1312,43 @@ ruleTester.run('no-unused-prop-types', rule, { ].join('\n'), parser: 'babel-eslint' }, { - // Destructured function props in the `componentWillReceiveProps` method shouldn't throw errors + // Destructured props in `shouldComponentUpdate` shouldn't throw errors when used createReactClass code: [ - 'class Hello extends Component {', - ' static propTypes = {', - ' something: PropTypes.bool', - ' }', - ' componentWillReceiveProps ({something}, nextState) {', + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' shouldComponentUpdate (nextProps, nextState) {', + ' const {something} = nextProps;', ' return something;', ' }', - '}' + '})' ].join('\n'), parser: 'babel-eslint' }, { - // Destructured props in the `shouldComponentUpdate` method shouldn't throw errors + // Destructured function props in the `shouldComponentUpdate` method shouldn't throw errors code: [ 'class Hello extends Component {', ' static propTypes = {', ' something: PropTypes.bool', ' }', - ' shouldComponentUpdate (nextProps, nextState) {', - ' const {something} = nextProps;', + ' shouldComponentUpdate ({something}, nextState) {', ' return something;', ' }', '}' ].join('\n'), parser: 'babel-eslint' }, { - // Destructured function props in the `shouldComponentUpdate` method shouldn't throw errors + // Destructured function props in `shouldComponentUpdate` shouldn't throw errors when used createReactClass code: [ - 'class Hello extends Component {', - ' static propTypes = {', - ' something: PropTypes.bool', - ' }', + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', ' shouldComponentUpdate ({something}, nextState) {', ' return something;', ' }', - '}' + '})' ].join('\n'), parser: 'babel-eslint' }, { @@ -1352,6 +1365,20 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in `componentWillUpdate` shouldn't throw errors when used createReactClass + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillUpdate (nextProps, nextState) {', + ' const {something} = nextProps;', + ' return something;', + ' }', + '})' + ].join('\n'), + parser: 'babel-eslint' }, { // Destructured function props in the `componentWillUpdate` method shouldn't throw errors code: [ @@ -1365,6 +1392,19 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in the `componentWillUpdate` method shouldn't throw errors when used createReactClass + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillUpdate ({something}, nextState) {', + ' return something;', + ' }', + '})' + ].join('\n'), + parser: 'babel-eslint' }, { // Destructured props in the `componentDidUpdate` method shouldn't throw errors code: [ @@ -1372,13 +1412,27 @@ ruleTester.run('no-unused-prop-types', rule, { ' static propTypes = {', ' something: PropTypes.bool', ' }', - ' componentDidUpdate (prevProps, nextState) {', + ' componentDidUpdate (prevProps, prevState) {', ' const {something} = prevProps;', ' return something;', ' }', '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in `componentDidUpdate` shouldn't throw errors when used createReactClass + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentDidUpdate (prevProps, prevState) {', + ' const {something} = prevProps;', + ' return something;', + ' }', + '})' + ].join('\n'), + parser: 'babel-eslint' }, { // Destructured function props in the `componentDidUpdate` method shouldn't throw errors code: [ @@ -1386,12 +1440,25 @@ ruleTester.run('no-unused-prop-types', rule, { ' static propTypes = {', ' something: PropTypes.bool', ' }', - ' componentDidUpdate ({something}, nextState) {', + ' componentDidUpdate ({something}, prevState) {', ' return something;', ' }', '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in the `componentDidUpdate` method shouldn't throw errors when used createReactClass + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentDidUpdate ({something}, prevState) {', + ' return something;', + ' }', + '})' + ].join('\n'), + parser: 'babel-eslint' }, { // Destructured state props in `componentDidUpdate` [Issue #825] code: [ @@ -1405,6 +1472,19 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured state props in `componentDidUpdate` [Issue #825] when used createReactClass + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentDidUpdate ({something}, {state1, state2}) {', + ' return something;', + ' }', + '})' + ].join('\n'), + parser: 'babel-eslint' }, { // Destructured state props in `componentDidUpdate` without custom parser [Issue #825] code: [ @@ -1417,6 +1497,18 @@ ruleTester.run('no-unused-prop-types', rule, { ' }', '});' ].join('\n') + }, { + // Destructured state props in `componentDidUpdate` without custom parser [Issue #825] when used createReactClass + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentDidUpdate: function ({something}, {state1, state2}) {', + ' return something;', + ' }', + '})' + ].join('\n') }, { // Destructured props in a stateless function code: [ From 24658b56968d25c1b0fb4f3f238c7abbf4f4b5ba Mon Sep 17 00:00:00 2001 From: Yuri S Date: Sun, 4 Feb 2018 21:57:38 +0200 Subject: [PATCH 4/5] Added more valid tests --- tests/lib/rules/no-unused-prop-types.js | 100 ++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index baf32be77e..87a36c9b85 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -1242,6 +1242,19 @@ ruleTester.run('no-unused-prop-types', rule, { '})' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in componentWillReceiveProps shouldn't throw errors when used createReactClass, with default parser + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillReceiveProps (nextProps) {', + ' const {something} = nextProps;', + ' doSomething(something);', + ' }', + '})' + ].join('\n') }, { // Destructured function props in componentWillReceiveProps shouldn't throw errors code: [ @@ -1268,6 +1281,18 @@ ruleTester.run('no-unused-prop-types', rule, { '})' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in componentWillReceiveProps shouldn't throw errors when used createReactClass, with default parser + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillReceiveProps ({something}) {', + ' doSomething(something);', + ' }', + '})' + ].join('\n') }, { // Destructured props in the constructor shouldn't throw errors code: [ @@ -1325,6 +1350,19 @@ ruleTester.run('no-unused-prop-types', rule, { '})' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in `shouldComponentUpdate` shouldn't throw errors when used createReactClass, with default parser + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' shouldComponentUpdate (nextProps, nextState) {', + ' const {something} = nextProps;', + ' return something;', + ' }', + '})' + ].join('\n') }, { // Destructured function props in the `shouldComponentUpdate` method shouldn't throw errors code: [ @@ -1351,6 +1389,18 @@ ruleTester.run('no-unused-prop-types', rule, { '})' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in `shouldComponentUpdate` shouldn't throw errors when used createReactClass, with default parser + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' shouldComponentUpdate ({something}, nextState) {', + ' return something;', + ' }', + '})' + ].join('\n') }, { // Destructured props in the `componentWillUpdate` method shouldn't throw errors code: [ @@ -1379,6 +1429,19 @@ ruleTester.run('no-unused-prop-types', rule, { '})' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in `componentWillUpdate` shouldn't throw errors when used createReactClass, with default parser + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillUpdate (nextProps, nextState) {', + ' const {something} = nextProps;', + ' return something;', + ' }', + '})' + ].join('\n') }, { // Destructured function props in the `componentWillUpdate` method shouldn't throw errors code: [ @@ -1405,6 +1468,18 @@ ruleTester.run('no-unused-prop-types', rule, { '})' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in the `componentWillUpdate` method shouldn't throw errors when used createReactClass, with default parser + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillUpdate ({something}, nextState) {', + ' return something;', + ' }', + '})' + ].join('\n') }, { // Destructured props in the `componentDidUpdate` method shouldn't throw errors code: [ @@ -1433,6 +1508,19 @@ ruleTester.run('no-unused-prop-types', rule, { '})' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in `componentDidUpdate` shouldn't throw errors when used createReactClass, with default parser + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentDidUpdate (prevProps, prevState) {', + ' const {something} = prevProps;', + ' return something;', + ' }', + '})' + ].join('\n') }, { // Destructured function props in the `componentDidUpdate` method shouldn't throw errors code: [ @@ -1459,6 +1547,18 @@ ruleTester.run('no-unused-prop-types', rule, { '})' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in the `componentDidUpdate` method shouldn't throw errors when used createReactClass, with default parser + code: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentDidUpdate ({something}, prevState) {', + ' return something;', + ' }', + '})' + ].join('\n') }, { // Destructured state props in `componentDidUpdate` [Issue #825] code: [ From 3d83d13fe1900c8b16f7f65019e38d83e98746e5 Mon Sep 17 00:00:00 2001 From: Yuri S Date: Mon, 5 Feb 2018 09:27:17 +0200 Subject: [PATCH 5/5] Added more tests --- tests/lib/rules/no-unused-prop-types.js | 139 ++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index 87a36c9b85..5bfb270cda 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -1228,6 +1228,19 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in componentWillReceiveProps shouldn't throw errors + code: [ + 'class Hello extends Component {', + ' componentWillReceiveProps (nextProps) {', + ' const {something} = nextProps;', + ' doSomething(something);', + ' }', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') }, { // Destructured props in componentWillReceiveProps shouldn't throw errors when used createReactClass code: [ @@ -1268,6 +1281,18 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in componentWillReceiveProps shouldn't throw errors + code: [ + 'class Hello extends Component {', + ' componentWillReceiveProps ({something}) {', + ' doSomething(something);', + ' }', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') }, { // Destructured function props in componentWillReceiveProps shouldn't throw errors when used createReactClass code: [ @@ -1308,6 +1333,20 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in the constructor shouldn't throw errors + code: [ + 'class Hello extends Component {', + ' constructor (props) {', + ' super(props);', + ' const {something} = props;', + ' doSomething(something);', + ' }', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') }, { // Destructured function props in the constructor shouldn't throw errors code: [ @@ -1322,6 +1361,19 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in the constructor shouldn't throw errors + code: [ + 'class Hello extends Component {', + ' constructor ({something}) {', + ' super({something});', + ' doSomething(something);', + ' }', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') }, { // Destructured props in the `shouldComponentUpdate` method shouldn't throw errors code: [ @@ -1336,6 +1388,19 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in the `shouldComponentUpdate` method shouldn't throw errors + code: [ + 'class Hello extends Component {', + ' shouldComponentUpdate (nextProps, nextState) {', + ' const {something} = nextProps;', + ' return something;', + ' }', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') }, { // Destructured props in `shouldComponentUpdate` shouldn't throw errors when used createReactClass code: [ @@ -1376,6 +1441,18 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in the `shouldComponentUpdate` method shouldn't throw errors + code: [ + 'class Hello extends Component {', + ' shouldComponentUpdate ({something}, nextState) {', + ' return something;', + ' }', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') }, { // Destructured function props in `shouldComponentUpdate` shouldn't throw errors when used createReactClass code: [ @@ -1415,6 +1492,19 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in the `componentWillUpdate` method shouldn't throw errors + code: [ + 'class Hello extends Component {', + ' componentWillUpdate (nextProps, nextState) {', + ' const {something} = nextProps;', + ' return something;', + ' }', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') }, { // Destructured props in `componentWillUpdate` shouldn't throw errors when used createReactClass code: [ @@ -1455,6 +1545,18 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in the `componentWillUpdate` method shouldn't throw errors + code: [ + 'class Hello extends Component {', + ' componentWillUpdate ({something}, nextState) {', + ' return something;', + ' }', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') }, { // Destructured function props in the `componentWillUpdate` method shouldn't throw errors when used createReactClass code: [ @@ -1494,6 +1596,19 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured props in the `componentDidUpdate` method shouldn't throw errors + code: [ + 'class Hello extends Component {', + ' componentDidUpdate (prevProps, prevState) {', + ' const {something} = prevProps;', + ' return something;', + ' }', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') }, { // Destructured props in `componentDidUpdate` shouldn't throw errors when used createReactClass code: [ @@ -1534,6 +1649,18 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured function props in the `componentDidUpdate` method shouldn't throw errors + code: [ + 'class Hello extends Component {', + ' componentDidUpdate ({something}, prevState) {', + ' return something;', + ' }', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') }, { // Destructured function props in the `componentDidUpdate` method shouldn't throw errors when used createReactClass code: [ @@ -1572,6 +1699,18 @@ ruleTester.run('no-unused-prop-types', rule, { '}' ].join('\n'), parser: 'babel-eslint' + }, { + // Destructured state props in `componentDidUpdate` [Issue #825] + code: [ + 'class Hello extends Component {', + ' componentDidUpdate ({something}, {state1, state2}) {', + ' return something;', + ' }', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') }, { // Destructured state props in `componentDidUpdate` [Issue #825] when used createReactClass code: [