diff --git a/packages/eslint-plugin/src/rules/prefer-as-const.ts b/packages/eslint-plugin/src/rules/prefer-as-const.ts index ca6b9921656..3d4a3ec9fea 100644 --- a/packages/eslint-plugin/src/rules/prefer-as-const.ts +++ b/packages/eslint-plugin/src/rules/prefer-as-const.ts @@ -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); diff --git a/packages/eslint-plugin/tests/rules/prefer-as-const.test.ts b/packages/eslint-plugin/tests/rules/prefer-as-const.test.ts index d5c15474f69..b31499f4e8a 100644 --- a/packages/eslint-plugin/tests/rules/prefer-as-const.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-as-const.test.ts @@ -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'; + } + `, + ` + 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: [ { @@ -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 = '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, + }, + ], + }, ], });