Skip to content

Commit

Permalink
feat(eslint-plugin): [prefer-as-const] adds support for class propert…
Browse files Browse the repository at this point in the history
…ies (#5413)

* feat(eslint-plugin): [prefer-as-const] adds support for class properties

* feat(eslint-plugin): [prefer-as-const] adds support for class properties
  • Loading branch information
undsoft committed Aug 1, 2022
1 parent 17dcf27 commit d2394f8
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/eslint-plugin/src/rules/prefer-as-const.ts
Expand Up @@ -65,6 +65,11 @@ export default util.createRule({
TSTypeAssertion(node): void {
compareTypes(node.expression, node.typeAnnotation, true);
},
PropertyDefinition(node): void {
if (node.value && node.typeAnnotation) {
compareTypes(node.value, node.typeAnnotation.typeAnnotation, false);
}
},
VariableDeclarator(node): void {
if (node.init && node.id.typeAnnotation) {
compareTypes(node.init, node.id.typeAnnotation.typeAnnotation, false);
Expand Down
177 changes: 172 additions & 5 deletions packages/eslint-plugin/tests/rules/prefer-as-const.test.ts
Expand Up @@ -23,19 +23,71 @@ ruleTester.run('prefer-as-const', rule, {
'let foo: number = 1;',
"let foo: 'bar' = baz;",
"let foo = 'bar';",
"let foo: 'bar';",
'let foo = { bar };',
"let foo: 'baz' = 'baz' as const;",
`
class foo {
bar: 'baz' = 'baz';
bar = 'baz';
}
`,
`
class foo {
bar = 'baz';
bar: 'baz';
}
`,
`
class foo {
bar;
}
`,
`
class foo {
bar = <baz>'baz';
}
`,
`
class foo {
bar: string = 'baz';
}
`,
`
class foo {
bar: number = 1;
}
`,
`
class foo {
bar = 'baz' as const;
}
`,
`
class foo {
bar = 2 as const;
}
`,
`
class foo {
get bar(): 'bar' {}
set bar(bar: 'bar') {}
}
`,
`
class foo {
bar = () => 'bar' as const;
}
`,
`
type BazFunction = () => 'baz';
class foo {
bar: BazFunction = () => 'bar';
}
`,
`
class foo {
bar(): void {}
}
`,
"let foo: 'bar';",
'let foo = { bar };',
"let foo: 'baz' = 'baz' as const;",
],
invalid: [
{
Expand Down Expand Up @@ -160,5 +212,120 @@ ruleTester.run('prefer-as-const', rule, {
},
],
},
{
code: `
class foo {
bar: 'baz' = 'baz';
}
`.trimRight(),
output: `
class foo {
bar: 'baz' = 'baz';
}
`.trimRight(),
errors: [
{
messageId: 'variableConstAssertion',
line: 3,
column: 8,
suggestions: [
{
messageId: 'variableSuggest',
output: `
class foo {
bar = 'baz' as const;
}
`.trimRight(),
},
],
},
],
},
{
code: `
class foo {
bar: 2 = 2;
}
`.trimRight(),
output: `
class foo {
bar: 2 = 2;
}
`.trimRight(),
errors: [
{
messageId: 'variableConstAssertion',
line: 3,
column: 8,
suggestions: [
{
messageId: 'variableSuggest',
output: `
class foo {
bar = 2 as const;
}
`.trimRight(),
},
],
},
],
},
{
code: `
class foo {
foo = <'bar'>'bar';
}
`.trimRight(),
output: `
class foo {
foo = <const>'bar';
}
`.trimRight(),
errors: [
{
messageId: 'preferConstAssertion',
line: 3,
column: 10,
},
],
},
{
code: `
class foo {
foo = 'bar' as 'bar';
}
`.trimRight(),
output: `
class foo {
foo = 'bar' as const;
}
`.trimRight(),
errors: [
{
messageId: 'preferConstAssertion',
line: 3,
column: 18,
},
],
},
{
code: `
class foo {
foo = 5 as 5;
}
`.trimRight(),
output: `
class foo {
foo = 5 as const;
}
`.trimRight(),
errors: [
{
messageId: 'preferConstAssertion',
line: 3,
column: 14,
},
],
},
],
});

0 comments on commit d2394f8

Please sign in to comment.