From 4da90ea19f805cb8e67523a983c2352aab144783 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 1 Jan 2019 22:09:09 -0800 Subject: [PATCH] [Fix] `prop-types`: avoid crash on used prevProps Fixes #2095. --- lib/util/usedPropTypes.js | 15 +++++++++------ tests/lib/rules/prop-types.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/util/usedPropTypes.js b/lib/util/usedPropTypes.js index 7c5477716c..e8d74d1e21 100644 --- a/lib/util/usedPropTypes.js +++ b/lib/util/usedPropTypes.js @@ -379,15 +379,18 @@ module.exports = function usedPropTypesInstructions(context, components, utils) } const nodeSource = sourceCode.getText(node); - const isDirectProp = DIRECT_PROPS_REGEX.test(nodeSource) || DIRECT_NEXT_PROPS_REGEX.test(nodeSource); + const isDirectProp = DIRECT_PROPS_REGEX.test(nodeSource) + || DIRECT_NEXT_PROPS_REGEX.test(nodeSource) + || DIRECT_PREV_PROPS_REGEX.test(nodeSource); + const reportedNode = ( + !isDirectProp && !inConstructor() && !inComponentWillReceiveProps() ? + node.parent.property : + node.property + ); usedPropTypes.push({ name: name, allNames: allNames, - node: ( - !isDirectProp && !inConstructor() && !inComponentWillReceiveProps() ? - node.parent.property : - node.property - ) + node: reportedNode }); break; case 'destructuring': diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index 32c601f9ce..70f1ac0807 100644 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -2247,6 +2247,36 @@ ruleTester.run('prop-types', rule, { query: PropTypes.string, }; ` + }, + { + code: [ + 'export default class LazyLoader extends Component {', + ' static propTypes = {', + ' children: PropTypes.node,', + ' load: PropTypes.any,', + ' };', + ' state = { mod: null };', + ' shouldComponentUpdate(prevProps) {', + ' assert(prevProps.load === this.props.load);', + ' return true;', + ' }', + ' load() {', + ' this.props.load(mod => {', + ' this.setState({', + ' mod: mod.default ? mod.default : mod', + ' });', + ' });', + ' }', + ' render() {', + ' if (this.state.mod !== null) {', + ' return this.props.children(this.state.mod);', + ' }', + ' this.load();', + ' return null;', + ' }', + '}' + ].join('\n'), + parser: 'babel-eslint' } ],