From 42b33af256e5269feb214c7bb161079f770964fa Mon Sep 17 00:00:00 2001 From: Muhammad Hammad <33136628+mhnaeem@users.noreply.github.com> Date: Wed, 23 Nov 2022 18:40:20 -0500 Subject: [PATCH] fix(eslint-plugin): [array-type] --fix flag removes parentheses from type (#5997) --- .../eslint-plugin/src/rules/array-type.ts | 3 +- .../tests/rules/array-type.test.ts | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/array-type.ts b/packages/eslint-plugin/src/rules/array-type.ts index 92040d8106a..f353207e7d5 100644 --- a/packages/eslint-plugin/src/rules/array-type.ts +++ b/packages/eslint-plugin/src/rules/array-type.ts @@ -252,8 +252,7 @@ export default util.createRule({ } const type = typeParams[0]; - const typeParens = - !util.isParenthesized(type, sourceCode) && typeNeedsParentheses(type); + const typeParens = typeNeedsParentheses(type); const parentParens = readonlyPrefix && node.parent?.type === AST_NODE_TYPES.TSArrayType && diff --git a/packages/eslint-plugin/tests/rules/array-type.test.ts b/packages/eslint-plugin/tests/rules/array-type.test.ts index c32da51cc08..04ab47d0b8a 100644 --- a/packages/eslint-plugin/tests/rules/array-type.test.ts +++ b/packages/eslint-plugin/tests/rules/array-type.test.ts @@ -1982,6 +1982,61 @@ class Foo extends Bar implements Baz { } `, ); + testOutput( + 'array', + ` +interface WorkingArray { + outerProperty: Array< + { innerPropertyOne: string } & { innerPropertyTwo: string } + >; +} + +interface BrokenArray { + outerProperty: Array< + ({ innerPropertyOne: string } & { innerPropertyTwo: string }) + >; +} + `, + ` +interface WorkingArray { + outerProperty: ({ innerPropertyOne: string } & { innerPropertyTwo: string })[]; +} + +interface BrokenArray { + outerProperty: ({ innerPropertyOne: string } & { innerPropertyTwo: string })[]; +} + `, + ); + testOutput( + 'array', + ` +type WorkingArray = { + outerProperty: Array< + { innerPropertyOne: string } & { innerPropertyTwo: string } + >; +} + +type BrokenArray = { + outerProperty: Array< + ({ innerPropertyOne: string } & { innerPropertyTwo: string }) + >; +} + `, + ` +type WorkingArray = { + outerProperty: ({ innerPropertyOne: string } & { innerPropertyTwo: string })[]; +} + +type BrokenArray = { + outerProperty: ({ innerPropertyOne: string } & { innerPropertyTwo: string })[]; +} + `, + ); + testOutput( + 'array', + 'const a: Array<(string|number)>;', + 'const a: (string|number)[];', + ); testOutput( 'array-simple', 'let xx: Array> = [[1, 2], [3]];',