Skip to content

Commit

Permalink
prefer-number-properties: Support computed key of ClassProperty
Browse files Browse the repository at this point in the history
… `MethodDefinition` `Property` (#886)
  • Loading branch information
fisker committed Oct 21, 2020
1 parent 262a4be commit 769ac35
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 27 deletions.
5 changes: 3 additions & 2 deletions rules/prefer-number-properties.js
Expand Up @@ -36,9 +36,10 @@ const propertiesSelector = [
'MemberExpression[computed=false] > Identifier.property',
'FunctionDeclaration > Identifier.id',
'ClassDeclaration > Identifier.id',
'MethodDefinition > Identifier.key',
'ClassProperty[computed=false] > Identifier.key',
'MethodDefinition[computed=false] > Identifier.key',
'VariableDeclarator > Identifier.id',
'Property[shorthand=false] > Identifier.key',
'Property[shorthand=false][computed=false] > Identifier.key',
'TSDeclareFunction > Identifier.id',
'TSEnumMember > Identifier.id',
'TSPropertySignature > Identifier.key'
Expand Down
38 changes: 35 additions & 3 deletions test/prefer-number-properties.js
Expand Up @@ -163,6 +163,10 @@ const errorNaN = [
}
}
];

// TODO: Add following tests whenESLint support `proposal-class-fields`
// 'class Foo {NaN = 1}',
// 'class Foo {[NaN] = 1}',
test({
valid: [
'const foo = Number.NaN;',
Expand All @@ -180,7 +184,7 @@ test({
'const {NaN} = {};',
'function NaN() {}',
'class NaN {}',
'class Foo { NaN(){}}',
'class Foo {NaN(){}}',

'const foo = Number.POSITIVE_INFINITY;',
'const foo = window.Number.POSITIVE_INFINITY;',
Expand Down Expand Up @@ -255,6 +259,19 @@ test({
]
});

test.babel({
valid: [
'class Foo2 {NaN = 1}'
],
invalid: [
{
code: 'class Foo2 {[NaN] = 1}',
output: 'class Foo2 {[Number.NaN] = 1}',
errors: 1
}
]
});

test.typescript({
valid: [
// https://github.com/angular/angular/blob/b4972fa1656101c02c92ddbf247db6e0de139937/packages/common/src/i18n/locale_data_api.ts#L178
Expand All @@ -278,18 +295,33 @@ test.typescript({
}
`
},
'declare function NaN(s: string, radix?: number): number;'
'declare function NaN(s: string, radix?: number): number;',
'class Foo {NaN = 1}'
],
invalid: []
invalid: [
{
code: 'class Foo {[NaN] = 1}',
output: 'class Foo {[Number.NaN] = 1}',
errors: 1
}
]
});

test.visualize([
'const foo = {[NaN]: 1}',
'const foo = {[NaN]() {}}',
'foo[NaN] = 1;',
'class A {[NaN](){}}',
'foo = {[NaN]: 1}',

'const foo = Infinity;',
'if (Number.isNaN(Infinity)) {}',
'if (Object.is(foo, Infinity)) {}',
'const foo = bar[Infinity];',
'const foo = {Infinity};',
'const foo = {Infinity: Infinity};',
'const foo = {[Infinity]: -Infinity};',
'const foo = {[-Infinity]: Infinity};',
'const foo = {Infinity: -Infinity};',
'const {foo = Infinity} = {};',
'const {foo = -Infinity} = {};',
Expand Down

0 comments on commit 769ac35

Please sign in to comment.