From 4073d0eec828b04b891c719174d59d34791bace9 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 7 Dec 2020 10:38:03 -0800 Subject: [PATCH] [Fix] `no-typos`: avoid crash with computed method name Fixes #2870 --- CHANGELOG.md | 4 +++- lib/rules/no-typos.js | 3 +++ tests/lib/rules/no-typos.js | 13 +++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 218f0d5197..60a0643fa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,9 +14,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel * [`display-name`]/component detection: avoid a crash on anonymous components ([#2840][] @ljharb) * [`prop-types`]: function in class that returns a component causes false warning in typescript ([#2843][] @SyMind) * [`jsx-no-target-blank`]: avoid a crash with a non-string literal ([#2851][] @ljharb) -* [`jsx-no-script-url`]: avoid crash with boolean `href` ([#2871][] @ljharb) +* [`jsx-no-script-url`]: avoid crash with boolean `href` ([#2871][] @ljharb, @AriPerkkio) +* [`no-typos`]: avoid crash with computed method name ([#2870][] @ljharb, @AriPerkkio) [#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2871 +[#2870]: https://github.com/yannickcr/eslint-plugin-react/issues/2870 [#2851]: https://github.com/yannickcr/eslint-plugin-react/issues/2851 [#2843]: https://github.com/yannickcr/eslint-plugin-react/pull/2843 [#2840]: https://github.com/yannickcr/eslint-plugin-react/issues/2840 diff --git a/lib/rules/no-typos.js b/lib/rules/no-typos.js index 2bb3c019c8..f9ce71329b 100644 --- a/lib/rules/no-typos.js +++ b/lib/rules/no-typos.js @@ -150,6 +150,9 @@ module.exports = { if (node.key.type === 'Literal') { nodeKeyName = node.key.value; } + if (node.computed && typeof nodeKeyName !== 'string') { + return; + } STATIC_LIFECYCLE_METHODS.forEach((method) => { if (!node.static && nodeKeyName.toLowerCase() === method.toLowerCase()) { diff --git a/tests/lib/rules/no-typos.js b/tests/lib/rules/no-typos.js index 99aeb24d75..e9eaf6f945 100644 --- a/tests/lib/rules/no-typos.js +++ b/tests/lib/rules/no-typos.js @@ -634,6 +634,19 @@ ruleTester.run('no-typos', rule, { }; `, parserOptions + }, { + code: ` + import React from 'react'; + + const A = { B: 'C' }; + + export default class MyComponent extends React.Component { + [A.B] () { + return null + } + } + `, + parserOptions }], invalid: [{