Skip to content

Commit 90e3767

Browse files
committed
Update no-unused-prop-types rule for new React class component lifecycles
1 parent fb2fefd commit 90e3767

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

lib/rules/no-unused-prop-types.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ const docsUrl = require('../util/docsUrl');
2222
const DIRECT_PROPS_REGEX = /^props\s*(\.|\[)/;
2323
const DIRECT_NEXT_PROPS_REGEX = /^nextProps\s*(\.|\[)/;
2424
const DIRECT_PREV_PROPS_REGEX = /^prevProps\s*(\.|\[)/;
25-
const LIFE_CYCLE_METHODS = ['componentWillReceiveProps', 'shouldComponentUpdate', 'componentWillUpdate', 'componentDidUpdate'];
25+
const LIFE_CYCLE_METHODS = [
26+
'componentDidUpdate',
27+
'componentWillReceiveProps',
28+
'componentWillUpdate',
29+
'getDerivedStateFromProps',
30+
'shouldComponentUpdate',
31+
'UNSAFE_componentWillReceiveProps',
32+
'UNSAFE_componentWillUpdate'
33+
];
2634

2735
// ------------------------------------------------------------------------------
2836
// Rule Definition
@@ -252,10 +260,7 @@ module.exports = {
252260
const nodeKeyName = (node.key || {}).name;
253261
return (
254262
node.kind === 'constructor' ||
255-
nodeKeyName === 'componentWillReceiveProps' ||
256-
nodeKeyName === 'shouldComponentUpdate' ||
257-
nodeKeyName === 'componentWillUpdate' ||
258-
nodeKeyName === 'componentDidUpdate'
263+
LIFE_CYCLE_METHODS.indexOf(nodeKeyName) >= 0
259264
);
260265
}
261266

tests/lib/rules/no-unused-prop-types.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,58 @@ ruleTester.run('no-unused-prop-types', rule, {
28252825
}
28262826
MyComponent.propTypes = { * other() {} };
28272827
`
2828+
}, {
2829+
// Sanity test coverage for new UNSAFE_componentWillReceiveProps lifecycles
2830+
code: [`
2831+
class Hello extends Component {
2832+
static propTypes = {
2833+
something: PropTypes.bool
2834+
};
2835+
UNSAFE_componentWillReceiveProps (nextProps) {
2836+
const {something} = nextProps;
2837+
doSomething(something);
2838+
}
2839+
}
2840+
`].join('\n'),
2841+
parser: 'babel-eslint'
2842+
}, {
2843+
// Destructured props in the `UNSAFE_componentWillUpdate` method shouldn't throw errors
2844+
code: [`
2845+
class Hello extends Component {
2846+
static propTypes = {
2847+
something: PropTypes.bool
2848+
};
2849+
UNSAFE_componentWillUpdate (nextProps, nextState) {
2850+
const {something} = nextProps;
2851+
return something;
2852+
}
2853+
}
2854+
`].join('\n'),
2855+
parser: 'babel-eslint'
2856+
}, {
2857+
// Simple test of new static getDerivedStateFromProps lifecycle
2858+
code: [`
2859+
class MyComponent extends React.Component {
2860+
static propTypes = {
2861+
defaultValue: 'bar'
2862+
};
2863+
state = {
2864+
currentValue: null
2865+
};
2866+
static getDerivedStateFromProps(nextProps, prevState) {
2867+
if (prevState.currentValue === null) {
2868+
return {
2869+
currentValue: nextProps.defaultValue,
2870+
}
2871+
}
2872+
return null;
2873+
}
2874+
render() {
2875+
return <div>{ this.state.currentValue }</div>
2876+
}
2877+
}
2878+
`].join('\n'),
2879+
parser: 'babel-eslint'
28282880
}
28292881
],
28302882

0 commit comments

Comments
 (0)