From 9509a3f5f8b2b0cfd1c129230934e78eafb79388 Mon Sep 17 00:00:00 2001 From: StyleShit <32631382+StyleShit@users.noreply.github.com> Date: Mon, 5 Feb 2024 20:28:19 +0200 Subject: [PATCH 1/5] [eslint-config]: Allow using `as const` in `typedef-var` Closes #4446 --- eslint/eslint-plugin/src/test/typedef-var.test.ts | 3 +++ eslint/eslint-plugin/src/typedef-var.ts | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/eslint/eslint-plugin/src/test/typedef-var.test.ts b/eslint/eslint-plugin/src/test/typedef-var.test.ts index fb12ffe09ff..f74ecaf5112 100644 --- a/eslint/eslint-plugin/src/test/typedef-var.test.ts +++ b/eslint/eslint-plugin/src/test/typedef-var.test.ts @@ -46,6 +46,9 @@ ruleTester.run('typedef-var', typedefVar, { { code: 'for (const x of []) { }' }, + { + code: 'const x = 1 as const;' + }, { // prettier-ignore code: [ diff --git a/eslint/eslint-plugin/src/typedef-var.ts b/eslint/eslint-plugin/src/typedef-var.ts index 33ed7cd1f57..6202d4d2844 100644 --- a/eslint/eslint-plugin/src/typedef-var.ts +++ b/eslint/eslint-plugin/src/typedef-var.ts @@ -50,6 +50,16 @@ const typedefVar: TSESLint.RuleModule = { return; } + if ( + node.init?.type === AST_NODE_TYPES.TSAsExpression && + node.init.typeAnnotation.type === AST_NODE_TYPES.TSTypeReference && + node.init.typeAnnotation.typeName.type === AST_NODE_TYPES.Identifier && + node.init.typeAnnotation.typeName.name === 'const' + ) { + // An `as const` type declaration was provided + return; + } + // These are @typescript-eslint/typedef exemptions if ( node.id.type === AST_NODE_TYPES.ArrayPattern /* ArrayDestructuring */ || From 0192bceecb224e249804b6aaa92dce51dee547a3 Mon Sep 17 00:00:00 2001 From: StyleShit <32631382+StyleShit@users.noreply.github.com> Date: Mon, 5 Feb 2024 20:49:24 +0200 Subject: [PATCH 2/5] wip --- eslint/eslint-plugin/src/test/typedef-var.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/eslint/eslint-plugin/src/test/typedef-var.test.ts b/eslint/eslint-plugin/src/test/typedef-var.test.ts index f74ecaf5112..c29bc76b353 100644 --- a/eslint/eslint-plugin/src/test/typedef-var.test.ts +++ b/eslint/eslint-plugin/src/test/typedef-var.test.ts @@ -49,6 +49,9 @@ ruleTester.run('typedef-var', typedefVar, { { code: 'const x = 1 as const;' }, + { + code: 'const x: number = 1;' + }, { // prettier-ignore code: [ From 21a0b675755381eb56fcaa90bf45ba07becd1e4e Mon Sep 17 00:00:00 2001 From: StyleShit <32631382+StyleShit@users.noreply.github.com> Date: Mon, 5 Feb 2024 20:57:08 +0200 Subject: [PATCH 3/5] add changes json --- .../feat-typedef-var-as-const_2024-02-05-18-53.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 common/changes/@rushstack/eslint-plugin/feat-typedef-var-as-const_2024-02-05-18-53.json diff --git a/common/changes/@rushstack/eslint-plugin/feat-typedef-var-as-const_2024-02-05-18-53.json b/common/changes/@rushstack/eslint-plugin/feat-typedef-var-as-const_2024-02-05-18-53.json new file mode 100644 index 00000000000..244e0917993 --- /dev/null +++ b/common/changes/@rushstack/eslint-plugin/feat-typedef-var-as-const_2024-02-05-18-53.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@rushstack/eslint-plugin", + "comment": "Allow using `as const` in `typedef-var`", + "type": "patch" + } + ], + "packageName": "@rushstack/eslint-plugin" +} \ No newline at end of file From 82e3fb0be32c3bfb2db00a768d839a48584f53b8 Mon Sep 17 00:00:00 2001 From: StyleShit <32631382+StyleShit@users.noreply.github.com> Date: Tue, 6 Feb 2024 18:57:57 +0200 Subject: [PATCH 4/5] change to minor --- .../feat-typedef-var-as-const_2024-02-05-18-53.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/changes/@rushstack/eslint-plugin/feat-typedef-var-as-const_2024-02-05-18-53.json b/common/changes/@rushstack/eslint-plugin/feat-typedef-var-as-const_2024-02-05-18-53.json index 244e0917993..7ce436c2107 100644 --- a/common/changes/@rushstack/eslint-plugin/feat-typedef-var-as-const_2024-02-05-18-53.json +++ b/common/changes/@rushstack/eslint-plugin/feat-typedef-var-as-const_2024-02-05-18-53.json @@ -3,7 +3,7 @@ { "packageName": "@rushstack/eslint-plugin", "comment": "Allow using `as const` in `typedef-var`", - "type": "patch" + "type": "minor" } ], "packageName": "@rushstack/eslint-plugin" From b75b1600a506cb7fa3c730bc2d39383e10264525 Mon Sep 17 00:00:00 2001 From: StyleShit <32631382+StyleShit@users.noreply.github.com> Date: Tue, 6 Feb 2024 19:17:33 +0200 Subject: [PATCH 5/5] add test case --- eslint/eslint-plugin/src/test/typedef-var.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/eslint/eslint-plugin/src/test/typedef-var.test.ts b/eslint/eslint-plugin/src/test/typedef-var.test.ts index c29bc76b353..a873952faae 100644 --- a/eslint/eslint-plugin/src/test/typedef-var.test.ts +++ b/eslint/eslint-plugin/src/test/typedef-var.test.ts @@ -49,6 +49,9 @@ ruleTester.run('typedef-var', typedefVar, { { code: 'const x = 1 as const;' }, + { + code: 'const x: 1 = 1;' + }, { code: 'const x: number = 1;' },