Skip to content

Commit c66c073

Browse files
committed
[Fix] no-typos: fix crash on private methods
Fixes #3043.
1 parent 85423f9 commit c66c073

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1919
* [`destructuring-assignment`]: get the contextName correctly ([#3025][] @ohhoney1)
2020
* [`no-typos`]: prevent crash on styled components and forwardRefs ([#3036][] @ljharb)
2121
* [`destructuring-assignment`], component detection: handle default exports edge case ([#3038][] @vedadeepta)
22+
* [`no-typos`]: fix crash on private methods ([#3043][] @ljharb)
2223

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

31+
[#3043]: https://github.com/yannickcr/eslint-plugin-react/issues/3043
3032
[#3039]: https://github.com/yannickcr/eslint-plugin-react/pull/3039
3133
[#3038]: https://github.com/yannickcr/eslint-plugin-react/pull/3038
3234
[#3036]: https://github.com/yannickcr/eslint-plugin-react/issues/3036

lib/rules/no-typos.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,12 @@ module.exports = {
157157
}
158158

159159
function reportErrorIfLifecycleMethodCasingTypo(node) {
160-
let nodeKeyName = node.key.name;
161-
if (node.key.type === 'Literal') {
162-
nodeKeyName = node.key.value;
160+
const key = node.key;
161+
let nodeKeyName = key.name;
162+
if (key.type === 'Literal') {
163+
nodeKeyName = key.value;
163164
}
164-
if (node.computed && typeof nodeKeyName !== 'string') {
165+
if (key.type === 'PrivateName' || (node.computed && typeof nodeKeyName !== 'string')) {
165166
return;
166167
}
167168

tests/lib/rules/no-typos.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
// Requirements
99
// -----------------------------------------------------------------------------
1010

11+
const babelEslintVersion = require('babel-eslint/package.json').version;
12+
const semver = require('semver');
1113
const RuleTester = require('eslint').RuleTester;
14+
1215
const rule = require('../../../lib/rules/no-typos');
1316

1417
const parsers = require('../../helpers/parsers');
@@ -656,7 +659,32 @@ ruleTester.run('no-typos', rule, {
656659
MyComponent.defaultProps = { value: "" };
657660
`,
658661
parserOptions
659-
}),
662+
}, semver.satisfies(babelEslintVersion, '>= 9') ? {
663+
code: `
664+
class Editor extends React.Component {
665+
#somethingPrivate() {
666+
// ...
667+
}
668+
669+
render() {
670+
const { value = '' } = this.props;
671+
672+
return (
673+
<textarea>
674+
{value}
675+
</textarea>
676+
);
677+
}
678+
}
679+
`,
680+
parser: require.resolve('babel-eslint'),
681+
parserOptions: {
682+
babelOptions: {
683+
classPrivateMethods: true
684+
},
685+
shippedProposals: true
686+
}
687+
} : []),
660688

661689
invalid: [].concat({
662690
code: `

0 commit comments

Comments
 (0)