From 44d65cdd356192b38dcbf737c265f0b37833a830 Mon Sep 17 00:00:00 2001 From: Ronen Amiel Date: Thu, 19 Dec 2024 17:46:09 +0200 Subject: [PATCH] autofix ts-conditional-type to be wrapped with parens --- .../eslint-plugin/src/rules/array-type.ts | 1 + .../tests/rules/array-type.test.ts | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/eslint-plugin/src/rules/array-type.ts b/packages/eslint-plugin/src/rules/array-type.ts index 08330497cb82..70b6211ee442 100644 --- a/packages/eslint-plugin/src/rules/array-type.ts +++ b/packages/eslint-plugin/src/rules/array-type.ts @@ -64,6 +64,7 @@ function typeNeedsParentheses(node: TSESTree.Node): boolean { case AST_NODE_TYPES.TSTypeOperator: case AST_NODE_TYPES.TSInferType: case AST_NODE_TYPES.TSConstructorType: + case AST_NODE_TYPES.TSConditionalType: return true; case AST_NODE_TYPES.Identifier: return node.name === 'ReadonlyArray'; diff --git a/packages/eslint-plugin/tests/rules/array-type.test.ts b/packages/eslint-plugin/tests/rules/array-type.test.ts index ed58c32d1fe2..8d0f1b5517d7 100644 --- a/packages/eslint-plugin/tests/rules/array-type.test.ts +++ b/packages/eslint-plugin/tests/rules/array-type.test.ts @@ -433,6 +433,32 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array' }], output: 'let a: (string | number)[] = [];', }, + { + code: 'let a: Array = [];', + errors: [ + { + column: 8, + data: { className: 'Array', readonlyPrefix: '', type: 'T' }, + line: 1, + messageId: 'errorStringArray', + }, + ], + options: [{ default: 'array' }], + output: 'let a: (T extends string ? number : string)[] = [];', + }, + { + code: 'let a: (T extends string ? number : string)[] = [];', + errors: [ + { + column: 8, + data: { className: 'Array', readonlyPrefix: '', type: 'T' }, + line: 1, + messageId: 'errorStringGeneric', + }, + ], + options: [{ default: 'generic' }], + output: 'let a: Array = [];', + }, { code: 'let a: ReadonlyArray = [];', errors: [