Skip to content

Commit

Permalink
[Fix] prop-types: avoid crash on used prevProps
Browse files Browse the repository at this point in the history
Fixes #2095.
  • Loading branch information
ljharb committed Jan 2, 2019
1 parent 0202de3 commit 4da90ea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/util/usedPropTypes.js
Expand Up @@ -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':
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -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'
}
],

Expand Down

0 comments on commit 4da90ea

Please sign in to comment.