@@ -82,7 +82,11 @@ type MessageIds =
82
82
| 'errorStringGeneric'
83
83
| 'errorStringGenericSimple'
84
84
| 'errorStringArray'
85
- | 'errorStringArraySimple' ;
85
+ | 'errorStringArraySimple'
86
+ | 'errorStringGenericReadonly'
87
+ | 'errorStringGenericSimpleReadonly'
88
+ | 'errorStringArrayReadonly'
89
+ | 'errorStringArraySimpleReadonly' ;
86
90
87
91
const arrayOption = { enum : [ 'array' , 'generic' , 'array-simple' ] } ;
88
92
@@ -98,13 +102,21 @@ export default util.createRule<Options, MessageIds>({
98
102
fixable : 'code' ,
99
103
messages : {
100
104
errorStringGeneric :
101
- "Array type using '{{type}}[]' is forbidden. Use '{{array}} <{{type}}>' instead." ,
105
+ "Array type using '{{type}}[]' is forbidden. Use 'Array <{{type}}>' instead." ,
102
106
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." ,
104
108
errorStringArray :
105
- "Array type using '{{array}} <{{type}}>' is forbidden. Use '{{type}}[]' instead." ,
109
+ "Array type using 'Array <{{type}}>' is forbidden. Use '{{type}}[]' instead." ,
106
110
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." ,
108
120
} ,
109
121
schema : [
110
122
{
@@ -155,20 +167,23 @@ export default util.createRule<Options, MessageIds>({
155
167
156
168
const messageId =
157
169
currentOption === 'generic'
158
- ? 'errorStringGeneric'
170
+ ? isReadonly
171
+ ? 'errorStringGenericReadonly'
172
+ : 'errorStringGeneric'
173
+ : isReadonly
174
+ ? 'errorStringGenericSimpleReadonly'
159
175
: 'errorStringGenericSimple' ;
160
176
const errorNode = isReadonly ? node . parent ! : node ;
161
177
162
- const arrayType = isReadonly ? 'ReadonlyArray' : 'Array' ;
163
-
164
178
context . report ( {
165
179
node : errorNode ,
166
180
messageId,
167
181
data : {
168
- array : arrayType ,
169
182
type : getMessageType ( node . elementType ) ,
170
183
} ,
171
184
fix ( fixer ) {
185
+ const arrayType = isReadonly ? 'ReadonlyArray' : 'Array' ;
186
+
172
187
const typeNode = node . elementType ;
173
188
return [
174
189
fixer . replaceTextRange (
@@ -206,9 +221,14 @@ export default util.createRule<Options, MessageIds>({
206
221
207
222
const readonlyPrefix = isReadonlyArrayType ? 'readonly ' : '' ;
208
223
const typeParams = node . typeParameters ?. params ;
224
+
209
225
const messageId =
210
226
currentOption === 'array'
211
- ? 'errorStringArray'
227
+ ? isReadonlyArrayType
228
+ ? 'errorStringArrayReadonly'
229
+ : 'errorStringArray'
230
+ : isReadonlyArrayType
231
+ ? 'errorStringArraySimpleReadonly'
212
232
: 'errorStringArraySimple' ;
213
233
214
234
if ( ! typeParams || typeParams . length === 0 ) {
@@ -218,7 +238,6 @@ export default util.createRule<Options, MessageIds>({
218
238
messageId,
219
239
data : {
220
240
type : 'any' ,
221
- array : node . typeName . name ,
222
241
} ,
223
242
fix ( fixer ) {
224
243
return fixer . replaceText ( node , `${ readonlyPrefix } any[]` ) ;
@@ -253,7 +272,6 @@ export default util.createRule<Options, MessageIds>({
253
272
messageId
582A
,
254
273
data : {
255
274
type : getMessageType ( type ) ,
256
- array : node . typeName . name ,
257
275
} ,
258
276
fix ( fixer ) {
259
277
return [
0 commit comments