Skip to content

Commit

Permalink
[Fix] no-typos: avoid crash with computed method name
Browse files Browse the repository at this point in the history
Fixes #2870
  • Loading branch information
ljharb committed Dec 7, 2020
1 parent 63d5a1b commit 4073d0e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/no-typos.js
Expand Up @@ -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()) {
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/no-typos.js
Expand Up @@ -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: [{
Expand Down

0 comments on commit 4073d0e

Please sign in to comment.