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] correct error message for readonly #4193

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 9 additions & 6 deletions packages/eslint-plugin/src/rules/array-type.ts
Expand Up @@ -98,13 +98,13 @@ export default util.createRule<Options, MessageIds>({
fixable: 'code',
messages: {
errorStringGeneric:
"Array type using '{{type}}[]' is forbidden. Use 'Array<{{type}}>' instead.",
"Array type using '{{type}}[]' is forbidden. Use '{{array}}<{{type}}>' instead.",
errorStringGenericSimple:
"Array type using '{{type}}[]' is forbidden for non-simple types. Use 'Array<{{type}}>' instead.",
"Array type using '{{type}}[]' is forbidden for non-simple types. Use '{{array}}<{{type}}>' instead.",
errorStringArray:
"Array type using 'Array<{{type}}>' is forbidden. Use '{{type}}[]' instead.",
"Array type using '{{array}}<{{type}}>' is forbidden. Use '{{type}}[]' instead.",
errorStringArraySimple:
"Array type using 'Array<{{type}}>' is forbidden for simple types. Use '{{type}}[]' instead.",
"Array type using '{{array}}<{{type}}>' is forbidden for simple types. Use '{{type}}[]' instead.",
},
schema: [
{
Expand Down Expand Up @@ -159,16 +159,17 @@ export default util.createRule<Options, MessageIds>({
: 'errorStringGenericSimple';
const errorNode = isReadonly ? node.parent! : node;

const arrayType = isReadonly ? 'ReadonlyArray' : 'Array';

context.report({
node: errorNode,
messageId,
data: {
array: arrayType,
type: getMessageType(node.elementType),
},
fix(fixer) {
const typeNode = node.elementType;
const arrayType = isReadonly ? 'ReadonlyArray' : 'Array';

return [
fixer.replaceTextRange(
[errorNode.range[0], typeNode.range[0]],
Expand Down Expand Up @@ -217,6 +218,7 @@ export default util.createRule<Options, MessageIds>({
messageId,
data: {
type: 'any',
array: node.typeName.name,
},
fix(fixer) {
return fixer.replaceText(node, `${readonlyPrefix}any[]`);
Expand Down Expand Up @@ -251,6 +253,7 @@ export default util.createRule<Options, MessageIds>({
messageId,
data: {
type: getMessageType(type),
array: node.typeName.name,
},
fix(fixer) {
return [
Expand Down