From a93ef203f8a5ccf151b362e4931ab9d049c52858 Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Thu, 26 Mar 2020 07:28:05 +0800 Subject: [PATCH] [Fix] `prop-types`: avoid crash when spreading any type Fixes #2605. --- lib/util/ast.js | 5 ++++- tests/lib/rules/prop-types.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/util/ast.js b/lib/util/ast.js index 731a27330c..773cd65999 100644 --- a/lib/util/ast.js +++ b/lib/util/ast.js @@ -155,7 +155,7 @@ function stripQuotes(string) { * Retrieve the name of a key node * @param {Context} context The AST node with the key. * @param {ASTNode} node The AST node with the key. - * @return {string} the name of the key + * @return {string | undefined} the name of the key */ function getKeyValue(context, node) { if (node.type === 'ObjectTypeProperty') { @@ -172,6 +172,9 @@ function getKeyValue(context, node) { return; } const key = node.key || node.argument; + if (!key) { + return; + } return key.type === 'Identifier' ? key.name : key.value; } diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index 7f1ec41835..e120d34f5f 100755 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -1650,6 +1650,17 @@ ruleTester.run('prop-types', rule, { '}' ].join('\n'), parser: parsers.BABEL_ESLINT + }, { + code: ` + type FooProps = { + ...any, + ...42, + } + function Foo(props: FooProps) { + return

; + } + `, + parser: parsers.BABEL_ESLINT }, { code: [ 'type Person = {',