Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
fix(eslint-plugin): [array-type] parenthesize ReadonlyArray fix (#2747)
- Loading branch information
|
@@ -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, |
|
|
}, |
|
|
], |
|
|
}, |
|
|
], |
|
|
}); |
|
|
|
|
|