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; } diff --git a/tests/lib/rules/no-unused-prop-types.js b/tests/lib/rules/no-unused-prop-types.js index 577148051e..5bfb270cda 100644 --- a/tests/lib/rules/no-unused-prop-types.js +++ b/tests/lib/rules/no-unused-prop-types.js @@ -1228,6 +1228,46 @@ 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: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillReceiveProps (nextProps) {', + ' const {something} = nextProps;', + ' doSomething(something);', + ' }', + '})' + ].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: [ @@ -1241,6 +1281,43 @@ 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: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillReceiveProps ({something}) {', + ' doSomething(something);', + ' }', + '})' + ].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: [ @@ -1256,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: [ @@ -1271,13 +1362,26 @@ ruleTester.run('no-unused-prop-types', rule, { ].join('\n'), parser: 'babel-eslint' }, { - // Destructured props in the `componentWillReceiveProps` method shouldn't throw errors + // 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: [ 'class Hello extends Component {', ' static propTypes = {', ' something: PropTypes.bool', ' }', - ' componentWillReceiveProps (nextProps, nextState) {', + ' shouldComponentUpdate (nextProps, nextState) {', ' const {something} = nextProps;', ' return something;', ' }', @@ -1285,27 +1389,53 @@ 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 the `shouldComponentUpdate` method shouldn't throw errors code: [ 'class Hello extends Component {', - ' static propTypes = {', - ' something: PropTypes.bool', + ' shouldComponentUpdate (nextProps, nextState) {', + ' const {something} = nextProps;', + ' return something;', ' }', - ' componentWillReceiveProps ({something}, nextState) {', + '}', + 'Hello.propTypes = {', + ' something: PropTypes.bool,', + '};' + ].join('\n') + }, { + // Destructured props in `shouldComponentUpdate` shouldn't throw errors when used createReactClass + code: [ + '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 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: [ 'class Hello extends Component {', ' static propTypes = {', ' something: PropTypes.bool', ' }', - ' shouldComponentUpdate (nextProps, nextState) {', - ' const {something} = nextProps;', + ' shouldComponentUpdate ({something}, nextState) {', ' return something;', ' }', '}' @@ -1315,15 +1445,39 @@ ruleTester.run('no-unused-prop-types', rule, { // Destructured function props in the `shouldComponentUpdate` method shouldn't throw errors code: [ 'class Hello extends Component {', - ' static propTypes = {', - ' something: PropTypes.bool', + ' 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: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', ' shouldComponentUpdate ({something}, nextState) {', ' return something;', ' }', - '}' + '})' ].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: [ @@ -1338,6 +1492,46 @@ 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: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillUpdate (nextProps, nextState) {', + ' const {something} = nextProps;', + ' return something;', + ' }', + '})' + ].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: [ @@ -1351,6 +1545,43 @@ 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: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentWillUpdate ({something}, nextState) {', + ' return something;', + ' }', + '})' + ].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: [ @@ -1358,13 +1589,53 @@ 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 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: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentDidUpdate (prevProps, prevState) {', + ' const {something} = prevProps;', + ' return something;', + ' }', + '})' + ].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: [ @@ -1372,12 +1643,49 @@ 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 + 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: [ + 'var Hello = createReactClass({', + ' propTypes: {', + ' something: PropTypes.bool,', + ' },', + ' componentDidUpdate ({something}, prevState) {', + ' return something;', + ' }', + '})' + ].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: [ @@ -1391,6 +1699,31 @@ 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: [ + '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: [ @@ -1403,6 +1736,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: [