From 566dcd3b5522007807b43ba75655a809637a369e Mon Sep 17 00:00:00 2001 From: lvjiaxuan <11309921+lvjiaxuan@users.noreply.github.com> Date: Tue, 26 Dec 2023 09:53:39 +0000 Subject: [PATCH 1/7] feat(eslint-plugin): [no-unnecessary-type-assertion] add `Identifier` check --- .../rules/no-unnecessary-type-assertion.ts | 5 ++++- .../no-unnecessary-type-assertion.test.ts | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) 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 96de86efe116..993369e3c3ae 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts @@ -169,7 +169,10 @@ export default createRule({ const type = getConstrainedTypeAtLocation(services, node.expression); if (!isNullableType(type)) { - if (isPossiblyUsedBeforeAssigned(node.expression)) { + if ( + node.expression.type === AST_NODE_TYPES.Identifier && + isPossiblyUsedBeforeAssigned(node.expression) + ) { 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 8cf362938bab..38055a719112 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 @@ -213,6 +213,10 @@ let values: number[] = []; value = values.pop()!; `, + ` +declare function foo(): number | undefined; +const a = foo()!; + `, ], invalid: [ @@ -518,5 +522,21 @@ y = 0; }, ], }, + { + code: ` +declare function foo(): number; +const a = foo()!; + `, + output: ` +declare function foo(): number; +const a = foo(); + `, + errors: [ + { + messageId: 'unnecessaryAssertion', + line: 3, + }, + ], + }, ], }); From 7648e556403de18d00abb4bb64e55eece50d8252 Mon Sep 17 00:00:00 2001 From: lvjiaxuan <11309921+lvjiaxuan@users.noreply.github.com> Date: Wed, 27 Dec 2023 06:58:11 +0000 Subject: [PATCH 2/7] feat(eslint-plugin): [no-unnecessary-type-assertion] update fixer for `CallExpression` --- .../rules/no-unnecessary-type-assertion.ts | 5 ++- .../no-unnecessary-type-assertion.test.ts | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) 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 993369e3c3ae..fca9aafab71e 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts @@ -277,7 +277,10 @@ export default createRule({ : null; } return fixer.removeRange([ - node.expression.range[1] + 1, + node.expression.range[1] + + (node.expression.type === AST_NODE_TYPES.CallExpression + ? 0 + : 1), node.range[1], ]); }, 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 38055a719112..11065669697b 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 @@ -529,6 +529,39 @@ const a = foo()!; `, output: ` declare function foo(): number; +const a = foo(); + `, + errors: [ + { + messageId: 'unnecessaryAssertion', + line: 3, + }, + ], + }, + { + code: ` +declare function foo(): number; +const a = foo() as number; + `, + output: ` +declare function foo(): number; +const a = foo(); + `, + errors: [ + { + messageId: 'unnecessaryAssertion', + line: 3, + column: 11, + }, + ], + }, + { + code: ` +declare function foo(): number; +const a = foo(); + `, + output: ` +declare function foo(): number; const a = foo(); `, errors: [ From 3fd7304d72aea9ec7084e738ddb917f2adc6cff2 Mon Sep 17 00:00:00 2001 From: lvjiaxuan <11309921+lvjiaxuan@users.noreply.github.com> Date: Wed, 27 Dec 2023 07:18:11 +0000 Subject: [PATCH 3/7] feat(eslint-plugin): [no-unnecessary-type-assertion] add test case --- .../rules/no-unnecessary-type-assertion.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 11065669697b..11465e3821fa 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 @@ -571,5 +571,22 @@ const a = foo(); }, ], }, + { + code: ` +type RT = { log: () => void }; +declare function foo(): RT; +(foo() as RT).log; + `, + output: ` +type RT = { log: () => void }; +declare function foo(): RT; +(foo()).log; + `, + errors: [ + { + messageId: 'unnecessaryAssertion', + }, + ], + }, ], }); From 41b7ea5e5d8ee78858bd0595814a5b2a88d926c3 Mon Sep 17 00:00:00 2001 From: lvjiaxuan <11309921+lvjiaxuan@users.noreply.github.com> Date: Wed, 27 Dec 2023 10:33:56 +0000 Subject: [PATCH 4/7] feat(eslint-plugin): [no-unnecessary-type-assertion] fit more cases --- .../rules/no-unnecessary-type-assertion.ts | 5 +-- .../no-unnecessary-type-assertion.test.ts | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) 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 fca9aafab71e..58ef344bf515 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts @@ -180,10 +180,7 @@ export default createRule({ node, messageId: 'unnecessaryAssertion', fix(fixer) { - return fixer.removeRange([ - node.expression.range[1], - node.range[1], - ]); + return fixer.removeRange([node.range[1] - 1, node.range[1]]); }, }); } else { 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 11465e3821fa..d4bf46c36028 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 @@ -535,6 +535,38 @@ const a = foo(); { messageId: 'unnecessaryAssertion', line: 3, + column: 11, + endColumn: 17, + }, + ], + }, + { + code: ` +const b = new Date()!; + `, + output: ` +const b = new Date(); + `, + errors: [ + { + messageId: 'unnecessaryAssertion', + line: 2, + }, + ], + }, + { + code: ` +const b = (1 + 1)!; + `, + output: ` +const b = (1 + 1); + `, + errors: [ + { + messageId: 'unnecessaryAssertion', + line: 2, + column: 11, + endColumn: 19, }, ], }, From 4fcbc477b38454fdcfbb05fade05a45c5a13f0d9 Mon Sep 17 00:00:00 2001 From: lvjiaxuan <11309921+lvjiaxuan@users.noreply.github.com> Date: Wed, 27 Dec 2023 14:17:34 +0000 Subject: [PATCH 5/7] feat(eslint-plugin): [no-unnecessary-type-assertion] add valid test cases --- .../tests/rules/no-unnecessary-type-assertion.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) 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 d4bf46c36028..2f3edffe910a 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 @@ -217,6 +217,14 @@ value = values.pop()!; declare function foo(): number | undefined; const a = foo()!; `, + ` +declare function foo(): number | undefined; +const a = foo() as number; + `, + ` +declare function foo(): number | undefined; +const a = foo(); + `, ], invalid: [ From ca9e40256d586a9dfd72dee7f34600fecbd4ed76 Mon Sep 17 00:00:00 2001 From: lvjiaxuan <11309921+lvjiaxuan@users.noreply.github.com> Date: Fri, 12 Jan 2024 01:52:37 +0000 Subject: [PATCH 6/7] fix: fix a gap and add tests --- .../src/rules/no-unnecessary-condition.ts | 2 +- .../no-unnecessary-type-assertion.test.ts | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts index a55fd0d3956a..f032634dbfda 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts @@ -436,7 +436,7 @@ export default createRule({ function checkCallExpression(node: TSESTree.CallExpression): void { // If this is something like arr.filter(x => /*condition*/), check `condition` if (isArrayPredicateFunction(node) && node.arguments.length) { - const callback = node.arguments[0]!; + const callback = node.arguments[0]; // Inline defined functions if ( callback.type === AST_NODE_TYPES.ArrowFunctionExpression || 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 2f3edffe910a..2f04bf36d229 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 @@ -225,6 +225,18 @@ const a = foo() as number; declare function foo(): number | undefined; const a = foo(); `, + ` +declare const arr: (object | undefined)[]; +const item = arr[0]!; + `, + ` +declare const arr: (object | undefined)[]; +const item = arr[0] as object; + `, + ` +declare const arr: (object | undefined)[]; +const item = arr[0]; + `, ], invalid: [ @@ -628,5 +640,19 @@ declare function foo(): RT; }, ], }, + { + code: ` +declare const arr: object[]; +const item = arr[0]!; + `, + output: ` + declare const arr: object[] +const item = arr[0]`, + errors: [ + { + messageId: 'unnecessaryAssertion', + }, + ], + }, ], }); From cc0f363c43841880d2a69ede0eadb94edb388927 Mon Sep 17 00:00:00 2001 From: lvjiaxuan <11309921+lvjiaxuan@users.noreply.github.com> Date: Fri, 12 Jan 2024 02:29:19 +0000 Subject: [PATCH 7/7] fix: typo --- .../tests/rules/no-unnecessary-type-assertion.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 2f04bf36d229..010cf2ace83a 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 @@ -646,8 +646,9 @@ declare const arr: object[]; const item = arr[0]!; `, output: ` - declare const arr: object[] -const item = arr[0]`, +declare const arr: object[]; +const item = arr[0]; + `, errors: [ { messageId: 'unnecessaryAssertion',