Skip to content

Commit 2b0d70c

Browse files
minweljharb
authored andcommitted
[Fix] prop-types: fix Cannot read property 'type' of undefined error when destructured param
see #2805, fixes #2804, #2805 (comment)
1 parent 4589522 commit 2b0d70c

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
55

66
## Unreleased
77

8+
## Fixed
9+
* [`prop-types`]: fix Cannot read property 'type' of undefined error when destructured param ([#2807][] @minwe)
10+
11+
[#2807]: https://github.com/yannickcr/eslint-plugin-react/pull/2807
12+
813
## [7.21.2] - 2020.09.24
914

1015
### Fixed

lib/rules/prop-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ module.exports = {
150150
return;
151151
}
152152
param.properties.forEach((property) => {
153-
if (property.type === 'RestElement') {
153+
if (property.type === 'RestElement' || property.type === 'ExperimentalRestProperty') {
154154
return;
155155
}
156156
const type = property.value.type;

tests/lib/rules/prop-types.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3096,7 +3096,7 @@ ruleTester.run('prop-types', rule, {
30963096
)
30973097
}
30983098
3099-
const mapDispatchToProps = (dispatch: ThunkDispatch<State, null, Action>) =>
3099+
const mapDispatchToProps = (dispatch: ThunkDispatch<State, null, Action>) =>
31003100
bindActionCreators<{prop1: ()=>void,prop2: ()=>string}>(
31013101
{ prop1: importedAction, prop2: anotherImportedAction },
31023102
dispatch,
@@ -6163,6 +6163,28 @@ ruleTester.run('prop-types', rule, {
61636163
errors: [{
61646164
message: "'bar' is missing in props validation"
61656165
}]
6166+
},
6167+
// fix #2804
6168+
{
6169+
code: `
6170+
import React from 'react'
6171+
6172+
const InputField = ({ type, ...restProps }) => {
6173+
6174+
return(
6175+
<input
6176+
type={type}
6177+
{...restProps}
6178+
/>
6179+
)
6180+
}
6181+
6182+
export default InputField;
6183+
`,
6184+
parser: parsers.BABEL_ESLINT,
6185+
errors: [{
6186+
message: "'type' is missing in props validation"
6187+
}]
61666188
}
61676189
])
61686190
)

0 commit comments

Comments
 (0)