From 9b9bc127548db6e2f9b17bafec64e5ab3c898625 Mon Sep 17 00:00:00 2001 From: auvred Date: Fri, 21 Jul 2023 21:47:54 +0000 Subject: [PATCH 1/2] fix(eslint-plugin): [non-nullable-type-assertion-style] consider operator precedence when fixing --- .../non-nullable-type-assertion-style.ts | 12 ++++++- .../non-nullable-type-assertion-style.test.ts | 36 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/non-nullable-type-assertion-style.ts b/packages/eslint-plugin/src/rules/non-nullable-type-assertion-style.ts index 2cfdd7ea1f88..69189417943d 100644 --- a/packages/eslint-plugin/src/rules/non-nullable-type-assertion-style.ts +++ b/packages/eslint-plugin/src/rules/non-nullable-type-assertion-style.ts @@ -117,11 +117,21 @@ export default util.createRule({ } if (sameTypeWithoutNullish(assertedTypes, originalTypes)) { + const expressionSourceCode = sourceCode.getText(node.expression); + + const higherPrecedenceThanUnary = + util.getOperatorPrecedence( + services.esTreeNodeToTSNodeMap.get(node.expression).kind, + ts.SyntaxKind.Unknown, + ) > util.OperatorPrecedence.Unary; + context.report({ fix(fixer) { return fixer.replaceText( node, - `${sourceCode.getText(node.expression)}!`, + higherPrecedenceThanUnary + ? `${expressionSourceCode}!` + : `(${expressionSourceCode})!`, ); }, messageId: 'preferNonNullAssertion', diff --git a/packages/eslint-plugin/tests/rules/non-nullable-type-assertion-style.test.ts b/packages/eslint-plugin/tests/rules/non-nullable-type-assertion-style.test.ts index 8b86634d2797..0171b1f14514 100644 --- a/packages/eslint-plugin/tests/rules/non-nullable-type-assertion-style.test.ts +++ b/packages/eslint-plugin/tests/rules/non-nullable-type-assertion-style.test.ts @@ -199,6 +199,42 @@ declare const x: T; const y = x!; `, }, + { + code: ` +declare function nullablePromise(): Promise; + +async function fn(): Promise { + return (await nullablePromise()) as string; +} + +declare const a: string | null; + +const b = (a || undefined) as string; + `, + errors: [ + { + column: 10, + line: 5, + messageId: 'preferNonNullAssertion', + }, + { + column: 11, + line: 10, + messageId: 'preferNonNullAssertion', + }, + ], + output: ` +declare function nullablePromise(): Promise; + +async function fn(): Promise { + return (await nullablePromise())!; +} + +declare const a: string | null; + +const b = (a || undefined)!; + `, + }, ], }); From 78fe250eb39edfd8c5f2ef8541498248fd691f6c Mon Sep 17 00:00:00 2001 From: auvred Date: Wed, 26 Jul 2023 15:30:22 +0000 Subject: [PATCH 2/2] test: split huge test case --- .../non-nullable-type-assertion-style.test.ts | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/non-nullable-type-assertion-style.test.ts b/packages/eslint-plugin/tests/rules/non-nullable-type-assertion-style.test.ts index 0171b1f14514..a5dd34cb94aa 100644 --- a/packages/eslint-plugin/tests/rules/non-nullable-type-assertion-style.test.ts +++ b/packages/eslint-plugin/tests/rules/non-nullable-type-assertion-style.test.ts @@ -206,10 +206,6 @@ declare function nullablePromise(): Promise; async function fn(): Promise { return (await nullablePromise()) as string; } - -declare const a: string | null; - -const b = (a || undefined) as string; `, errors: [ { @@ -217,11 +213,6 @@ const b = (a || undefined) as string; line: 5, messageId: 'preferNonNullAssertion', }, - { - column: 11, - line: 10, - messageId: 'preferNonNullAssertion', - }, ], output: ` declare function nullablePromise(): Promise; @@ -229,7 +220,22 @@ declare function nullablePromise(): Promise; async function fn(): Promise { return (await nullablePromise())!; } + `, + }, + { + code: ` +declare const a: string | null; +const b = (a || undefined) as string; + `, + errors: [ + { + column: 11, + line: 4, + messageId: 'preferNonNullAssertion', + }, + ], + output: ` declare const a: string | null; const b = (a || undefined)!;