@@ -19,14 +19,17 @@ module.exports = {
19
19
category : "TypeScript" ,
20
20
url : util . metaDocsUrl ( "no-type-alias" ) ,
21
21
} ,
22
+ messages : {
23
+ noTypeAlias : "Type {{alias}} are not allowed." ,
24
+ noCompositionAlias :
25
+ "{{typeName}} in {{compositionType}} types are not allowed." ,
26
+ } ,
22
27
schema : [
23
28
{
24
29
type : "object" ,
25
30
properties : {
26
31
allowAliases : {
27
32
enum : [
28
- true ,
29
- false ,
30
33
"always" ,
31
34
"never" ,
32
35
"in-unions" ,
@@ -35,12 +38,10 @@ module.exports = {
35
38
] ,
36
39
} ,
37
40
allowCallbacks : {
38
- enum : [ true , false , "always" , "never" ] ,
41
+ enum : [ "always" , "never" ] ,
39
42
} ,
40
43
allowLiterals : {
41
44
enum : [
42
- true ,
43
- false ,
44
45
"always" ,
45
46
"never" ,
46
47
"in-unions" ,
@@ -50,8 +51,6 @@ module.exports = {
50
51
} ,
51
52
allowMappedTypes : {
52
53
enum : [
53
- true ,
54
- false ,
55
54
"always" ,
56
55
"never" ,
57
56
"in-unions" ,
@@ -66,22 +65,15 @@ module.exports = {
66
65
} ,
67
66
68
67
create ( context ) {
69
- const options = context . options [ 0 ] ;
68
+ const options = context . options [ 0 ] || { } ;
70
69
71
- const allowAliases = ( options && options . allowAliases ) || "never" ;
72
- const allowCallbacks = ( options && options . allowCallbacks ) || "never" ;
73
- const allowLiterals = ( options && options . allowLiterals ) || "never" ;
74
- const allowMappedTypes =
75
- ( options && options . allowMappedTypes ) || "never" ;
70
+ const allowAliases = options . allowAliases || "never" ;
71
+ const allowCallbacks = options . allowCallbacks || "never" ;
72
+ const allowLiterals = options . allowLiterals || "never" ;
73
+ const allowMappedTypes = options . allowMappedTypes || "never" ;
76
74
77
- const unions = [
78
- true ,
79
- "always" ,
80
- "in-unions" ,
81
- "in-unions-and-intersections" ,
82
- ] ;
75
+ const unions = [ "always" , "in-unions" , "in-unions-and-intersections" ] ;
83
76
const intersections = [
84
- true ,
85
77
"always" ,
86
78
"in-intersections" ,
87
79
"in-unions-and-intersections" ,
@@ -181,27 +173,36 @@ module.exports = {
181
173
182
174
/**
183
175
* Gets the message to be displayed based on the node type and whether the node is a top level declaration.
176
+ * @param {Object } node the location
184
177
* @param {string } compositionType the type of composition this alias is part of (undefined if not
185
178
* part of a composition)
186
179
* @param {boolean } isRoot a flag indicating we are dealing with the top level declaration.
187
180
* @param {string } type the kind of type alias being validated.
188
181
* @returns {string } the message to be displayed.
189
182
* @private
190
183
*/
191
- function getMessage ( compositionType , isRoot , type ) {
184
+ function getMessage ( node , compositionType , isRoot , type ) {
192
185
if ( isRoot ) {
193
- return type
194
- ? `Type ${ type } are not allowed.`
195
- : "Type aliases are not allowed." ;
186
+ return {
187
+ node,
188
+ messageId : "noTypeAlias" ,
189
+ data : {
190
+ alias : type || "aliases" ,
191
+ } ,
192
+ } ;
196
193
}
197
194
198
- return compositionType === "TSUnionType"
199
- ? `${ type [ 0 ] . toUpperCase ( ) } ${ type . slice (
200
- 1
201
- ) } in union types are not allowed.`
202
- : `${ type [ 0 ] . toUpperCase ( ) } ${ type . slice (
203
- 1
204
- ) } in intersection types are not allowed.`;
195
+ return {
196
+ node,
197
+ messageId : "noCompositionAlias" ,
198
+ data : {
199
+ compositionType :
200
+ compositionType === "TSUnionType"
201
+ ? "union"
202
+ : "intersection" ,
203
+ typeName : util . upperCaseFirst ( type ) ,
204
+ } ,
205
+ } ;
205
206
}
206
207
207
208
/**
@@ -223,25 +224,20 @@ module.exports = {
223
224
allowAliases
224
225
)
225
226
) {
226
- context . report ( {
227
- node,
228
- message : getMessage (
229
- compositionType ,
230
- isTopLevel ,
231
- "aliases"
232
- ) ,
233
- } ) ;
227
+ context . report (
228
+ getMessage ( node , compositionType , isTopLevel , "aliases" )
229
+ ) ;
234
230
}
235
231
} else if ( isCallback ( node ) ) {
236
232
if ( allowCallbacks === "never" ) {
237
- context . report ( {
238
- node ,
239
- message : getMessage (
233
+ context . report (
234
+ getMessage (
235
+ node ,
240
236
compositionType ,
241
237
isTopLevel ,
242
238
"callbacks"
243
- ) ,
244
- } ) ;
239
+ )
240
+ ) ;
245
241
}
246
242
} else if ( isLiteral ( node ) ) {
247
243
if (
@@ -252,14 +248,14 @@ module.exports = {
252
248
allowLiterals
253
249
)
254
250
) {
255
- context . report ( {
256
- node ,
257
- message : getMessage (
251
+ context . report (
252
+ getMessage (
253
+ node ,
258
254
compositionType ,
259
255
isTopLevel ,
260
256
"literals"
261
- ) ,
262
- } ) ;
257
+ )
258
+ ) ;
263
259
}
264
260
} else if ( isMappedType ( node ) ) {
265
261
if (
@@ -270,20 +266,17 @@ module.exports = {
270
266
allowMappedTypes
271
267
)
272
268
) {
273
- context . report ( {
274
- node ,
275
- message : getMessage (
269
+ context . report (
270
+ getMessage (
271
+ node ,
276
272
compositionType ,
277
273
isTopLevel ,
278
274
"mapped types"
279
- ) ,
280
- } ) ;
275
+ )
276
+ ) ;
281
277
}
282
278
} else {
283
- context . report ( {
284
- node,
285
- message : getMessage ( compositionType , isTopLevel ) ,
286
- } ) ;
279
+ context . report ( getMessage ( node , compositionType , isTopLevel ) ) ;
287
280
}
288
281
}
289
282
0 commit comments