Skip to content

Commit 314af44

Browse files
authored
feat(eslint-plugin): array-type distinguish whether readonly or not (#4066)
* feat(eslint-plugin): distinguish whether readonly or not Signed-off-by: koooge <[email protected]> * refactor: template messages Signed-off-by: koooge <[email protected]>
1 parent 3c89e42 commit 314af44

File tree

2 files changed

+209
-99
lines changed

2 files changed

+209
-99
lines changed

Diff for: packages/eslint-plugin/src/rules/array-type.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ type Options = [
8080
];
8181
type MessageIds =
8282
| 'errorStringGeneric'
83-
| 'errorStringGenericSimple'
8483
| 'errorStringArray'
85-
| 'errorStringArraySimple';
84+
| 'errorStringArraySimple'
85+
| 'errorStringGenericSimple';
8686

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

@@ -98,13 +98,13 @@ export default util.createRule<Options, MessageIds>({
9898
fixable: 'code',
9999
messages: {
100100
errorStringGeneric:
101-
"Array type using '{{type}}[]' is forbidden. Use 'Array<{{type}}>' instead.",
102-
errorStringGenericSimple:
103-
"Array type using '{{type}}[]' is forbidden for non-simple types. Use 'Array<{{type}}>' instead.",
101+
"Array type using '{{readonlyPrefix}}{{type}}[]' is forbidden. Use '{{className}}<{{type}}>' instead.",
104102
errorStringArray:
105-
"Array type using 'Array<{{type}}>' is forbidden. Use '{{type}}[]' instead.",
103+
"Array type using '{{className}}<{{type}}>' is forbidden. Use '{{readonlyPrefix}}{{type}}[]' instead.",
106104
errorStringArraySimple:
107-
"Array type using 'Array<{{type}}>' is forbidden for simple types. Use '{{type}}[]' instead.",
105+
"Array type using '{{className}}<{{type}}>' is forbidden for simple types. Use '{{readonlyPrefix}}{{type}}[]' instead.",
106+
errorStringGenericSimple:
107+
"Array type using '{{readonlyPrefix}}{{type}}[]' is forbidden for non-simple types. Use '{{className}}<{{type}}>' instead.",
108108
},
109109
schema: [
110110
{
@@ -163,6 +163,8 @@ export default util.createRule<Options, MessageIds>({
163163
node: errorNode,
164164
messageId,
165165
data: {
166+
className: isReadonly ? 'ReadonlyArray' : 'Array',
167+
readonlyPrefix: isReadonly ? 'readonly ' : '',
166168
type: getMessageType(node.elementType),
167169
},
168170
fix(fixer) {
@@ -216,6 +218,8 @@ export default util.createRule<Options, MessageIds>({
216218
node,
217219
messageId,
218220
data: {
221+
className: isReadonlyArrayType ? 'ReadonlyArray' : 'Array',
222+
readonlyPrefix,
219223
type: 'any',
220224
},
221225
fix(fixer) {
@@ -250,6 +254,8 @@ export default util.createRule<Options, MessageIds>({
250254
node,
251255
messageId,
252256
data: {
257+
className: isReadonlyArrayType ? 'ReadonlyArray' : 'Array',
258+
readonlyPrefix,
253259
type: getMessageType(type),
254260
},
255261
fix(fixer) {

0 commit comments

Comments
 (0)