Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [array-type] --fix flag removes parentheses from type #5997

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/array-type.ts
Expand Up @@ -252,8 +252,7 @@ export default util.createRule<Options, MessageIds>({
}

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 &&
Expand Down
55 changes: 55 additions & 0 deletions packages/eslint-plugin/tests/rules/array-type.test.ts
Expand Up @@ -1982,6 +1982,61 @@ class Foo<T = Bar[][]> extends Bar<T, T[]> implements Baz<T[]> {
}
`,
);
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 })[];
}
`,
);
Comment on lines +2010 to +2034
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case looks identical to the one above?

Copy link
Contributor Author

@mhnaeem mhnaeem Nov 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Josh-Cena One is with type and the other one is with interface, I wanted to be sure it doesn't affect the code different on either keyword. I can modify it a bit if you like @Josh-Cena

Maybe something like
type BrokenArray = Array<({ a: number } & { b: string })>

testOutput(
'array',
'const a: Array<(string|number)>;',
'const a: (string|number)[];',
);
testOutput(
'array-simple',
'let xx: Array<Array<number>> = [[1, 2], [3]];',
Expand Down