Skip to content

Commit 4fedc5f

Browse files
李凤宝ljharb
authored andcommitted
[Fix] no-typos: Compilation error when method name is string instead of identifier
1 parent d52002a commit 4fedc5f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/rules/no-typos.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ module.exports = {
144144

145145
function reportErrorIfLifecycleMethodCasingTypo(node) {
146146
LIFECYCLE_METHODS.forEach((method) => {
147-
if (method.toLowerCase() === node.key.name.toLowerCase() && method !== node.key.name) {
147+
let nodeKeyName = node.key.name;
148+
if (node.key.type === 'Literal') {
149+
nodeKeyName = node.key.value;
150+
}
151+
if (method.toLowerCase() === nodeKeyName.toLowerCase() && method !== nodeKeyName) {
148152
context.report({
149153
node,
150154
message: 'Typo in component lifecycle method declaration'

tests/lib/rules/no-typos.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ ruleTester.run('no-typos', rule, {
203203
}
204204
`,
205205
parserOptions
206+
}, {
207+
code: `
208+
class Hello extends React.Component {
209+
"componentDidMount"() { }
210+
"my-method"() { }
211+
}
212+
`,
213+
parserOptions
206214
}, {
207215
code: `
208216
class MyClass {

0 commit comments

Comments
 (0)