diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts index 12a42398b3ee..69dd6174fec2 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/src/rules/no-unnecessary-type-assertion.ts b/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts index 96de86efe116..58ef344bf515 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; } @@ -177,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 { @@ -274,7 +274,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 8cf362938bab..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 @@ -213,6 +213,30 @@ let values: number[] = []; 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(); + `, + ` +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: [ @@ -518,5 +542,118 @@ y = 0; }, ], }, + { + code: ` +declare function foo(): number; +const a = foo()!; + `, + output: ` +declare function foo(): number; +const a = foo(); + `, + errors: [ + { + 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, + }, + ], + }, + { + 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: [ + { + messageId: 'unnecessaryAssertion', + line: 3, + }, + ], + }, + { + 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', + }, + ], + }, + { + code: ` +declare const arr: object[]; +const item = arr[0]!; + `, + output: ` +declare const arr: object[]; +const item = arr[0]; + `, + errors: [ + { + messageId: 'unnecessaryAssertion', + }, + ], + }, ], });