Skip to content

Commit

Permalink
Merge pull request #4512 from StyleShit/feat/typedef-var-as-const
Browse files Browse the repository at this point in the history
[eslint-plugin]: Allow using `as const` in `typedef-var`
  • Loading branch information
iclanton authored Feb 6, 2024
2 parents fe1cbf4 + b75b160 commit 8752399
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/eslint-plugin",
"comment": "Allow using `as const` in `typedef-var`",
"type": "minor"
}
],
"packageName": "@rushstack/eslint-plugin"
}
9 changes: 9 additions & 0 deletions eslint/eslint-plugin/src/test/typedef-var.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ ruleTester.run('typedef-var', typedefVar, {
{
code: 'for (const x of []) { }'
},
{
code: 'const x = 1 as const;'
},
{
code: 'const x: 1 = 1;'
},
{
code: 'const x: number = 1;'
},
{
// prettier-ignore
code: [
Expand Down
10 changes: 10 additions & 0 deletions eslint/eslint-plugin/src/typedef-var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ const typedefVar: TSESLint.RuleModule<MessageIds, Options> = {
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 */ ||
Expand Down

0 comments on commit 8752399

Please sign in to comment.