From 8a6b8a84a8e02394336c8933128797f84386b2f7 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Mon, 16 Mar 2020 11:13:41 +0200 Subject: [PATCH] feat(eslint-plugin): [no-unnec-type-assertion] allow const assertions --- .../rules/no-unnecessary-type-assertion.md | 4 +++ .../rules/no-unnecessary-type-assertion.ts | 16 ++++++++-- .../no-unnecessary-type-assertion.test.ts | 32 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-unnecessary-type-assertion.md b/packages/eslint-plugin/docs/rules/no-unnecessary-type-assertion.md index 14819c56bf78..4464b6483ef1 100644 --- a/packages/eslint-plugin/docs/rules/no-unnecessary-type-assertion.md +++ b/packages/eslint-plugin/docs/rules/no-unnecessary-type-assertion.md @@ -43,6 +43,10 @@ const foo = 3; const foo = 3 as number; ``` +```ts +const foo = 'foo' as const; +``` + ```ts function foo(x: number | undefined): number { return x!; diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts b/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts index dd4395b290b8..4fe9f9a59d25 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts @@ -1,4 +1,7 @@ -import { TSESTree } from '@typescript-eslint/experimental-utils'; +import { + TSESTree, + AST_NODE_TYPES, +} from '@typescript-eslint/experimental-utils'; import { isObjectType, isObjectFlagSet, @@ -122,6 +125,14 @@ export default util.createRule({ return false; } + function isConstAssertion(node: TSESTree.TypeNode): boolean { + return ( + node.type === AST_NODE_TYPES.TSTypeReference && + node.typeName.type === AST_NODE_TYPES.Identifier && + node.typeName.name === 'const' + ); + } + return { TSNonNullExpression(node): void { const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node); @@ -201,7 +212,8 @@ export default util.createRule({ if ( options.typesToIgnore?.includes( sourceCode.getText(node.typeAnnotation), - ) + ) || + isConstAssertion(node.typeAnnotation) ) { return; } diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts index e7248a9693ef..4d1ae880e840 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts @@ -130,6 +130,38 @@ function Test(props: { `, filename: 'react.tsx', }, + { + code: ` +const a = [1, 2]; +const b = [3, 4]; +const c = [...a, ...b] as const; + `, + }, + { + code: `const a = [1, 2] as const;`, + }, + { + code: `const a = 'a' as const;`, + }, + { + code: `const a = { foo: 'foo' } as const`, + }, + { + code: ` +const a = [1, 2]; +const b = [3, 4]; +const c = [...a, ...b]; + `, + }, + { + code: `const a = [1, 2];`, + }, + { + code: `const a = 'a';`, + }, + { + code: `const a = { foo: 'foo' };`, + }, ], invalid: [ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy