Skip to content

Commit

Permalink
[Fix] no-typos: fix crash on private methods
Browse files Browse the repository at this point in the history
Fixes #3043.
  • Loading branch information
ljharb committed Aug 20, 2021
1 parent 85423f9 commit c66c073
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`destructuring-assignment`]: get the contextName correctly ([#3025][] @ohhoney1)
* [`no-typos`]: prevent crash on styled components and forwardRefs ([#3036][] @ljharb)
* [`destructuring-assignment`], component detection: handle default exports edge case ([#3038][] @vedadeepta)
* [`no-typos`]: fix crash on private methods ([#3043][] @ljharb)

### Changed
* [Docs] [`jsx-no-bind`]: updates discussion of refs ([#2998][] @dimitropoulos)
Expand All @@ -27,6 +28,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [Docs] [`require-default-props`]: fix small typo ([#2994][] @evsasse)
* [Tests] add weekly scheduled smoke tests ([#2963][] @AriPerkkio)

[#3043]: https://github.com/yannickcr/eslint-plugin-react/issues/3043
[#3039]: https://github.com/yannickcr/eslint-plugin-react/pull/3039
[#3038]: https://github.com/yannickcr/eslint-plugin-react/pull/3038
[#3036]: https://github.com/yannickcr/eslint-plugin-react/issues/3036
Expand Down
9 changes: 5 additions & 4 deletions lib/rules/no-typos.js
Expand Up @@ -157,11 +157,12 @@ module.exports = {
}

function reportErrorIfLifecycleMethodCasingTypo(node) {
let nodeKeyName = node.key.name;
if (node.key.type === 'Literal') {
nodeKeyName = node.key.value;
const key = node.key;
let nodeKeyName = key.name;
if (key.type === 'Literal') {
nodeKeyName = key.value;
}
if (node.computed && typeof nodeKeyName !== 'string') {
if (key.type === 'PrivateName' || (node.computed && typeof nodeKeyName !== 'string')) {
return;
}

Expand Down
30 changes: 29 additions & 1 deletion tests/lib/rules/no-typos.js
Expand Up @@ -8,7 +8,10 @@
// Requirements
// -----------------------------------------------------------------------------

const babelEslintVersion = require('babel-eslint/package.json').version;
const semver = require('semver');
const RuleTester = require('eslint').RuleTester;

const rule = require('../../../lib/rules/no-typos');

const parsers = require('../../helpers/parsers');
Expand Down Expand Up @@ -656,7 +659,32 @@ ruleTester.run('no-typos', rule, {
MyComponent.defaultProps = { value: "" };
`,
parserOptions
}),
}, semver.satisfies(babelEslintVersion, '>= 9') ? {
code: `
class Editor extends React.Component {
#somethingPrivate() {
// ...
}
render() {
const { value = '' } = this.props;
return (
<textarea>
{value}
</textarea>
);
}
}
`,
parser: require.resolve('babel-eslint'),
parserOptions: {
babelOptions: {
classPrivateMethods: true
},
shippedProposals: true
}
} : []),

invalid: [].concat({
code: `
Expand Down

0 comments on commit c66c073

Please sign in to comment.