forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.js
455 lines (415 loc) · 14.5 KB
/
types.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
'use strict';
/**
* Trivial helpers in this file that only check a node's existence and/or type are deprecated in favor of inlining that check.
* We don't need a function for every type of node.
* And as written, these functions won't correctly narrow the type of the node, which we would need if we incorporate TypeScript: https://github.com/ember-cli/eslint-plugin-ember/issues/1613
* TODO: we should inline these trivial checks and only check for node existence when it's actually a possibility a node might not exist.
*/
module.exports = {
isAnyFunctionExpression,
isArrayExpression,
isArrowFunctionExpression,
isAssignmentExpression,
isBinaryExpression,
isCallExpression,
isCallWithFunctionExpression,
isClassDeclaration,
isClassPropertyOrPropertyDefinition,
isCommaToken,
isConciseArrowFunctionWithCallExpression,
isConditionalExpression,
isDecorator,
isExpressionStatement,
isFunctionDeclaration,
isFunctionExpression,
isIdentifier,
isImportDeclaration,
isImportDefaultSpecifier,
isLiteral,
isLogicalExpression,
isMemberExpression,
isMethodDefinition,
isNewExpression,
isObjectExpression,
isObjectPattern,
isOptionalCallExpression,
isOptionalMemberExpression,
isProperty,
isReturnStatement,
isSpreadElement,
isString,
isStringLiteral,
isTaggedTemplateExpression,
isTemplateLiteral,
isThisExpression,
isUnaryExpression,
isVariableDeclarator,
};
/**
* Check whether or not a node is an ArrowFunctionExpression or FunctionExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an ArrowFunctionExpression or FunctionExpression.
*/
function isAnyFunctionExpression(node) {
return isArrowFunctionExpression(node) || isFunctionExpression(node);
}
/**
* Check whether or not a node is an ArrayExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an ArrayExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isArrayExpression(node) {
return node !== undefined && node.type === 'ArrayExpression';
}
/**
* Check whether or not a node is an ArrowFunctionExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an ArrowFunctionExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isArrowFunctionExpression(node) {
return node !== undefined && node.type === 'ArrowFunctionExpression';
}
/**
* Check whether or not a node is an AssignmentExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an AssignmentExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isAssignmentExpression(node) {
return node.type === 'AssignmentExpression';
}
/**
* Check whether or not a node is an BinaryExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an BinaryExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isBinaryExpression(node) {
return node !== undefined && node.type === 'BinaryExpression';
}
/**
* Check whether or not a node is an CallExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an CallExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isCallExpression(node) {
return node !== undefined && node.type === 'CallExpression';
}
/**
* Check whether or not a node is a CallExpression that has a FunctionExpression
* as first argument, eg.:
* tSomeAction: mysteriousFnc(function(){})
*
* @param {Object} node The node to check
* @return {boolean} Whether or not the node is a call with a function expression as the first argument
*/
function isCallWithFunctionExpression(node) {
if (!isCallExpression(node)) {
return false;
}
const callObj = isMemberExpression(node.callee) ? node.callee.object : node;
const firstArg = callObj.arguments ? callObj.arguments[0] : null;
return (
callObj !== undefined && isCallExpression(callObj) && firstArg && isFunctionExpression(firstArg)
);
}
/**
* Check whether or not a node is a ClassDeclaration.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is a ClassDeclaration.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isClassDeclaration(node) {
return node !== undefined && node.type === 'ClassDeclaration';
}
/**
* Check whether or not a node is a ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8).
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is a ClassProperty or PropertyDefinition.
*/
function isClassPropertyOrPropertyDefinition(node) {
return (
node !== undefined && (node.type === 'ClassProperty' || node.type === 'PropertyDefinition')
);
}
function isCommaToken(token) {
return token.type === 'Punctuator' && token.value === ',';
}
/**
* Check whether or not a node is an ArrowFunctionExpression with concise body
* that contains a call expression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an ArrowFunctionExpression
* with concise body.
*/
function isConciseArrowFunctionWithCallExpression(node) {
return isArrowFunctionExpression(node) && node.expression && isCallExpression(node.body);
}
/**
* Check whether or not a node is a ConditionalExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is a ConditionalExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isConditionalExpression(node) {
return node !== undefined && node.type === 'ConditionalExpression';
}
/**
* Check whether or not a node is a Decorator.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is a Decorator.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isDecorator(node) {
return node !== undefined && node.type === 'Decorator';
}
/**
* Check whether or not a node is an ExpressionStatement.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an ExpressionStatement.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isExpressionStatement(node) {
return node !== undefined && node.type === 'ExpressionStatement';
}
/**
* Check whether or not a node is a FunctionDeclaration
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is a FunctionDeclaration
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isFunctionDeclaration(node) {
return node !== undefined && node.type === 'FunctionDeclaration';
}
/**
* Check whether or not a node is an FunctionExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an FunctionExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isFunctionExpression(node) {
return node !== undefined && node.type === 'FunctionExpression';
}
/**
* Check whether or not a node is an Identifier.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an Identifier.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isIdentifier(node) {
return node !== undefined && node.type === 'Identifier';
}
/**
* Check whether or not a node is an ImportDeclaration.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an ImportDeclaration.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isImportDeclaration(node) {
return node !== undefined && node.type === 'ImportDeclaration';
}
/**
* Check whether or not a node is an ImportDefaultSpecifier.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an ImportDefaultSpecifier.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isImportDefaultSpecifier(node) {
return node !== undefined && node.type === 'ImportDefaultSpecifier';
}
/**
* Check whether or not a node is an Literal.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an Literal.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isLiteral(node) {
return node !== undefined && node.type === 'Literal';
}
/**
* Check whether or not a node is an LogicalExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an LogicalExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isLogicalExpression(node) {
return node !== undefined && node.type === 'LogicalExpression';
}
/**
* Check whether or not a node is an MemberExpression.
*
* @param {Object} node The node to check.
* @return {boolean} Whether or not the node is an MemberExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isMemberExpression(node) {
return node !== undefined && node.type === 'MemberExpression';
}
/**
* Check whether or not a node is a MethodDefinition.
*
* @param {Object} node The node to check.
* @return {boolean} Whether or not the node is a MethodDefinition.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isMethodDefinition(node) {
return node !== undefined && node.type === 'MethodDefinition';
}
/**
* Check whether or not a node is an NewExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an NewExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isNewExpression(node) {
return node !== undefined && node.type === 'NewExpression';
}
/**
* Check whether or not a node is an ObjectExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an ObjectExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isObjectExpression(node) {
return node !== undefined && node.type === 'ObjectExpression';
}
/**
* Check whether or not a node is an ObjectPattern.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an ObjectPattern.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isObjectPattern(node) {
return node !== undefined && node.type === 'ObjectPattern';
}
/**
* Check whether or not a node is an OptionalCallExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an OptionalCallExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isOptionalCallExpression(node) {
return node.type === 'OptionalCallExpression';
}
/**
* Check whether or not a node is an OptionalMemberExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an OptionalMemberExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isOptionalMemberExpression(node) {
return node.type === 'OptionalMemberExpression';
}
/**
* Check whether or not a node is an Property.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an Property.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isProperty(node) {
return node !== undefined && node.type === 'Property';
}
/**
* Check whether or not a node is a ReturnStatement
*
* @param {Object} node The node to check.
* @return {Boolean} Whether or not the node is a ReturnStatement.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isReturnStatement(node) {
return node !== undefined && node.type && node.type === 'ReturnStatement';
}
/**
* Check whether or not a node is a SpreadElement
*
* @param {Object} node The node to check.
* @return {Boolean} Whether or not the node is a SpreadElement.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isSpreadElement(node) {
return node !== undefined && node.type === 'SpreadElement';
}
function isString(node) {
return isTemplateLiteral(node) || (isLiteral(node) && typeof node.value === 'string');
}
function isStringLiteral(node) {
return isLiteral(node) && typeof node.value === 'string';
}
/**
* Check whether or not a node is a TaggedTemplateExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is a TaggedTemplateExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isTaggedTemplateExpression(node) {
return node !== undefined && node.type === 'TaggedTemplateExpression';
}
/**
* Check whether or not a node is a TemplateLiteral.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is a TemplateLiteral.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isTemplateLiteral(node) {
return node !== undefined && node.type === 'TemplateLiteral';
}
/**
* Check whether or not a node is an ThisExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an ThisExpression.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isThisExpression(node) {
return node !== undefined && node.type === 'ThisExpression';
}
/**
* Check whether or not a node is an UnaryExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an Literal.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isUnaryExpression(node) {
return node !== undefined && node.type === 'UnaryExpression';
}
/**
* Check whether or not a node is a VariableDeclarator.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is a VariableDeclarator.
* @deprecated trivial helpers are deprecated in favor of inlining the type check
*/
function isVariableDeclarator(node) {
return node !== undefined && node.type === 'VariableDeclarator';
}