From 2b0d70c6b8c24b45838f132d3d5ccd7542935a0d Mon Sep 17 00:00:00 2001 From: "minhui.lmh" Date: Sun, 27 Sep 2020 10:47:05 +0800 Subject: [PATCH] [Fix] `prop-types`: fix Cannot read property 'type' of undefined error when destructured param see #2805, fixes #2804, https://github.com/yannickcr/eslint-plugin-react/pull/2805#issuecomment-698896658 --- CHANGELOG.md | 5 +++++ lib/rules/prop-types.js | 2 +- tests/lib/rules/prop-types.js | 24 +++++++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41769e1b08..91793e6cdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ## Unreleased +## Fixed +* [`prop-types`]: fix Cannot read property 'type' of undefined error when destructured param ([#2807][] @minwe) + +[#2807]: https://github.com/yannickcr/eslint-plugin-react/pull/2807 + ## [7.21.2] - 2020.09.24 ### Fixed diff --git a/lib/rules/prop-types.js b/lib/rules/prop-types.js index 6eec874a89..2cc1416115 100644 --- a/lib/rules/prop-types.js +++ b/lib/rules/prop-types.js @@ -150,7 +150,7 @@ module.exports = { return; } param.properties.forEach((property) => { - if (property.type === 'RestElement') { + if (property.type === 'RestElement' || property.type === 'ExperimentalRestProperty') { return; } const type = property.value.type; diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index 0ef8a072f0..ea5c6cbd3a 100644 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -3096,7 +3096,7 @@ ruleTester.run('prop-types', rule, { ) } - const mapDispatchToProps = (dispatch: ThunkDispatch) => + const mapDispatchToProps = (dispatch: ThunkDispatch) => bindActionCreators<{prop1: ()=>void,prop2: ()=>string}>( { prop1: importedAction, prop2: anotherImportedAction }, dispatch, @@ -6163,6 +6163,28 @@ ruleTester.run('prop-types', rule, { errors: [{ message: "'bar' is missing in props validation" }] + }, + // fix #2804 + { + code: ` + import React from 'react' + + const InputField = ({ type, ...restProps }) => { + + return( + + ) + } + + export default InputField; + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: "'type' is missing in props validation" + }] } ]) )