Skip to content

Commit 3c6d571

Browse files
committed
Fix crash in no-unused-prop-types
When checking props in setState updater functions, some assumptions were being made about the props argument being defined.
1 parent c148893 commit 3c6d571

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ module.exports = {
134134
// Make sure we are in the updater not the callback
135135
&& scope.block.parent.arguments[0].start === scope.block.start
136136
&& scope.block.parent.arguments[0].params
137-
&& scope.block.parent.arguments[0].params.length > 0
137+
&& scope.block.parent.arguments[0].params.length > 1
138138
) {
139139
return scope.block.parent.arguments[0].params[1].name === node.object.name;
140140
}
@@ -949,7 +949,7 @@ module.exports = {
949949
}
950950

951951
function handleSetStateUpdater(node) {
952-
if (!node.params || !node.params.length || !inSetStateUpdater()) {
952+
if (!node.params || node.params.length < 2 || !inSetStateUpdater()) {
953953
return;
954954
}
955955
markPropTypesAsUsed(node);

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,46 @@ ruleTester.run('no-unused-prop-types', rule, {
21972197
].join('\n'),
21982198
parser: 'babel-eslint',
21992199
options: [{skipShapeProps: false}]
2200+
}, {
2201+
// issue #1542
2202+
code: [
2203+
'class MyComponent extends React.Component {',
2204+
' onFoo() {',
2205+
' this.setState((prevState) => {',
2206+
' this.props.doSomething();',
2207+
' });',
2208+
' }',
2209+
' render() {',
2210+
' return (',
2211+
' <div onClick={this.onFoo}>Test</div>',
2212+
' );',
2213+
' }',
2214+
'}',
2215+
'MyComponent.propTypes = {',
2216+
' doSomething: PropTypes.func',
2217+
'};'
2218+
].join('\n'),
2219+
options: [{skipShapeProps: false}]
2220+
}, {
2221+
// issue #1542
2222+
code: [
2223+
'class MyComponent extends React.Component {',
2224+
' onFoo() {',
2225+
' this.setState(({ something }) => {',
2226+
' this.props.doSomething();',
2227+
' });',
2228+
' }',
2229+
' render() {',
2230+
' return (',
2231+
' <div onClick={this.onFoo}>Test</div>',
2232+
' );',
2233+
' }',
2234+
'}',
2235+
'MyComponent.propTypes = {',
2236+
' doSomething: PropTypes.func',
2237+
'};'
2238+
].join('\n'),
2239+
options: [{skipShapeProps: false}]
22002240
}, {
22012241
// issue #106
22022242
code: `

0 commit comments

Comments
 (0)