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] parenthesize ReadonlyArray fix #2747

Merged
merged 1 commit into from Nov 8, 2020
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
19 changes: 10 additions & 9 deletions packages/eslint-plugin/src/rules/array-type.ts
Expand Up @@ -244,7 +244,14 @@ export default util.createRule<Options, MessageIds>({
}

const type = typeParams[0];
const parens = typeNeedsParentheses(type);
const typeParens = typeNeedsParentheses(type);
const parentParens =
readonlyPrefix && node.parent?.type === AST_NODE_TYPES.TSArrayType;

const start = `${parentParens ? '(' : ''}${readonlyPrefix}${
typeParens ? '(' : ''
}`;
const end = `${typeParens ? ')' : ''}[]${parentParens ? ')' : ''}`;

context.report({
node,
Expand All @@ -254,14 +261,8 @@ export default util.createRule<Options, MessageIds>({
},
fix(fixer) {
return [
fixer.replaceTextRange(
[node.range[0], type.range[0]],
`${readonlyPrefix}${parens ? '(' : ''}`,
),
fixer.replaceTextRange(
[type.range[1], node.range[1]],
parens ? ')[]' : '[]',
),
fixer.replaceTextRange([node.range[0], type.range[0]], start),
fixer.replaceTextRange([type.range[1], node.range[1]], end),
];
},
});
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint-plugin/tests/rules/array-type.test.ts
Expand Up @@ -1648,6 +1648,19 @@ interface FooInterface {
},
],
},
{
code: 'type Foo = ReadonlyArray<object>[];',
output: 'type Foo = (readonly object[])[];',
options: [{ default: 'array' }],
errors: [
{
messageId: 'errorStringArray',
data: { type: 'object' },
line: 1,
column: 12,
},
],
},
],
});

Expand Down