8000 fix: [array-type] correct error message for readonly part 2 · dotJack/typescript-eslint@ecf2cf2 · GitHub
[go: up one dir, main page]

Skip to content

Commit ecf2cf2

Browse files
committed
fix: [array-type] correct error message for readonly part 2
1 parent 8c2122a commit ecf2cf2

File tree

2 files changed

+147
-129
lines changed

2 files changed

+147
-129
lines changed

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ type MessageIds =
8282
| 'errorStringGeneric'
8383
| 'errorStringGenericSimple'
8484
| 'errorStringArray'
85-
| 'errorStringArraySimple';
85+
| 'errorStringArraySimple'
86+
| 'errorStringGenericReadonly'
87+
| 'errorStringGenericSimpleReadonly'
88+
| 'errorStringArrayReadonly'
89+
| 'errorStringArraySimpleReadonly';
8690

8791
const arrayOption = { enum: ['array', 'generic', 'array-simple'] };
8892

@@ -98,13 +102,21 @@ export default util.createRule<Options, MessageIds>({
98102
fixable: 'code',
99103
messages: {
100104
errorStringGeneric:
101-
"Array type using '{{type}}[]' is forbidden. Use '{{array}}<{{type}}>' instead.",
105+
"Array type using '{{type}}[]' is forbidden. Use 'Array<{{type}}>' instead.",
102106
errorStringGenericSimple:
103-
"Array type using '{{type}}[]' is forbidden for non-simple types. Use '{{array}}<{{type}}>' instead.",
107+
"Array type using '{{type}}[]' is forbidden for non-simple types. Use 'Array<{{type}}>' instead.",
104108
errorStringArray:
105-
"Array type using '{{array}}<{{type}}>' is forbidden. Use '{{type}}[]' instead.",
109+
"Array type using 'Array<{{type}}>' is forbidden. Use '{{type}}[]' instead.",
106110
errorStringArraySimple:
107-
"Array type using '{{array}}<{{type}}>' is forbidden for simple types. Use '{{type}}[]' instead.",
111+
"Array type using 'Array<{{type}}>' is forbidden for simple types. Use '{{type}}[]' instead.",
112+
errorStringGenericReadonly:
113+
"Array type using 'readonly {{type}}[]' is forbidden. Use 'ReadonlyArray<{{type}}>' instead.",
114+
errorStringGenericSimpleReadonly:
115+
"Array type using 'readonly {{type}}[]' is forbidden for non-simple types. Use 'ReadonlyArray<{{type}}>' instead.",
116+
errorStringArrayReadonly:
117+
"Array type using 'ReadonlyArray<{{type}}>' is forbidden. Use 'readonly {{type}}[]' instead.",
118+
errorStringArraySimpleReadonly:
119+
"Array type using 'ReadonlyArray<{{type}}>' is forbidden for simple types. Use 'readonly {{type}}[]' instead.",
108120
},
109121
schema: [
110122
{
@@ -155,20 +167,23 @@ export default util.createRule<Options, MessageIds>({
155167

156168
const messageId =
157169
currentOption === 'generic'
158-
? 'errorStringGeneric'
170+
? isReadonly
171+
? 'errorStringGenericReadonly'
172+
: 'errorStringGeneric'
173+
: isReadonly
174+
? 'errorStringGenericSimpleReadonly'
159175
: 'errorStringGenericSimple';
160176
const errorNode = isReadonly ? node.parent! : node;
161177

162-
const arrayType = isReadonly ? 'ReadonlyArray' : 'Array';
163-
164178
context.report({
165179
node: errorNode,
166180
messageId,
167181
data: {
168-
array: arrayType,
169182
type: getMessageType(node.elementType),
170183
},
171184
fix(fixer) {
185+
const arrayType = isReadonly ? 'ReadonlyArray' : 'Array';
186+
172187
const typeNode = node.elementType;
173188
return [
174189
fixer.replaceTextRange(
@@ -206,9 +221,14 @@ export default util.createRule<Options, MessageIds>({
206221

207222
const readonlyPrefix = isReadonlyArrayType ? 'readonly ' : '';
208223
const typeParams = node.typeParameters?.params;
224+
209225
const messageId =
210226
currentOption === 'array'
211-
? 'errorStringArray'
227+
? isReadonlyArrayType
228+
? 'errorStringArrayReadonly'
229+
: 'errorStringArray'
230+
: isReadonlyArrayType
231+
? 'errorStringArraySimpleReadonly'
212232
: 'errorStringArraySimple';
213233

214234
if (!typeParams || typeParams.length === 0) {
@@ -218,7 +238,6 @@ export default util.createRule<Options, MessageIds>({
218238
messageId,
219239
data: {
220240
type: 'any',
221-
array: node.typeName.name,
222241
},
223242
fix(fixer) {
224243
return fixer.replaceText(node, `${readonlyPrefix}any[]`);
@@ -253,7 +272,6 @@ export default util.createRule<Options, MessageIds>({
253272
messageId 582A ,
254273
data: {
255274
type: getMessageType(type),
256-
array: node.typeName.name,
257275
},
258276
fix(fixer) {
259277
return [

0 commit comments

Comments
 (0)
0