Skip to content

Commit 83385ac

Browse files
authored
fix(eslint-plugin): [array-type] parenthesize ReadonlyArray fix (typescript-eslint#2747)
1 parent 0d0af64 commit 83385ac

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

packages/eslint-plugin/src/rules/array-type.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,14 @@ export default util.createRule<Options, MessageIds>({
244244
}
245245

246246
const type = typeParams[0];
247-
const parens = typeNeedsParentheses(type);
247+
const typeParens = typeNeedsParentheses(type);
248+
const parentParens =
249+
readonlyPrefix && node.parent?.type === AST_NODE_TYPES.TSArrayType;
250+
251+
const start = `${parentParens ? '(' : ''}${readonlyPrefix}${
252+
typeParens ? '(' : ''
253+
}`;
254+
const end = `${typeParens ? ')' : ''}[]${parentParens ? ')' : ''}`;
248255

249256
context.report({
250257
node,
@@ -254,14 +261,8 @@ export default util.createRule<Options, MessageIds>({
254261
},
255262
fix(fixer) {
256263
return [
257-
fixer.replaceTextRange(
258-
[node.range[0], type.range[0]],
259-
`${readonlyPrefix}${parens ? '(' : ''}`,
260-
),
261-
fixer.replaceTextRange(
262-
[type.range[1], node.range[1]],
263-
parens ? ')[]' : '[]',
264-
),
264+
fixer.replaceTextRange([node.range[0], type.range[0]], start),
265+
fixer.replaceTextRange([type.range[1], node.range[1]], end),
265266
];
266267
},
267268
});

packages/eslint-plugin/tests/rules/array-type.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,19 @@ interface FooInterface {
16481648
},
16491649
],
16501650
},
1651+
{
1652+
code: 'type Foo = ReadonlyArray<object>[];',
1653+
output: 'type Foo = (readonly object[])[];',
1654+
options: [{ default: 'array' }],
1655+
errors: [
1656+
{
1657+
messageId: 'errorStringArray',
1658+
data: { type: 'object' },
1659+
line: 1,
1660+
column: 12,
1661+
},
1662+
],
1663+
},
16511664
],
16521665
});
16531666

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy