Skip to content

Commit

Permalink
fix(eslint-plugin): [array-type] parenthesize ReadonlyArray fix (#2747)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Nov 8, 2020
1 parent 0d0af64 commit 83385ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
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

0 comments on commit 83385ac

Please sign in to comment.