Skip to content
Permalink
Browse files

fix(eslint-plugin): [array-type] parenthesize ReadonlyArray fix (#2747)

  • Loading branch information
a-tarasyuk committed Nov 8, 2020
1 parent 0d0af64 commit 83385ac351f45d3bcbd19f72711838e9a8473827
@@ -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,
@@ -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),
];
},
});
@@ -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,
},
],
},
],
});

0 comments on commit 83385ac

Please sign in to comment.