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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539 |
1×
1×
1×
20×
16×
4×
2×
2×
1×
123×
45×
78×
40×
78×
1×
3×
2×
3×
1×
146×
1×
63×
1×
757×
1×
1230×
1×
99×
1×
973×
1×
380×
1×
861×
1×
6×
1×
4×
1×
37×
1×
1067×
1×
8×
1×
157×
1×
50×
1×
141×
1×
81×
1×
69×
1×
36×
1×
25×
1×
23×
1×
31×
9×
9×
22×
1×
24×
9×
9×
15×
1×
6×
6×
234×
6×
6×
4×
6×
1×
16×
16×
9×
7×
2×
2×
1×
1×
5×
5×
1×
7×
7×
| 'use strict';
var scopeProperties = [
'$id',
'$parent',
'$root',
'$destroy',
'$broadcast',
'$emit',
'$on',
'$applyAsync',
'$apply',
'$evalAsync',
'$eval',
'$digest',
'$watchCollection',
'$watchGroup',
'$watch',
'$new'
];
module.exports = {
// Properties
scopeProperties: scopeProperties,
// Functions
convertPrefixToRegex: convertPrefixToRegex,
convertStringToRegex: convertStringToRegex,
isTypeOfStatement: isTypeOfStatement,
isToStringStatement: isToStringStatement,
isArrayType: isArrayType,
isFunctionType: isFunctionType,
isNamedInlineFunction: isNamedInlineFunction,
isIdentifierType: isIdentifierType,
isMemberExpression: isMemberExpression,
isLiteralType: isLiteralType,
isEmptyFunction: isEmptyFunction,
isStringRegexp: isStringRegexp,
isAngularComponent: isAngularComponent,
isAngularComponentDeclaration: isAngularComponentDeclaration,
isAngularControllerDeclaration: isAngularControllerDeclaration,
isAngularFilterDeclaration: isAngularFilterDeclaration,
isAngularDirectiveDeclaration: isAngularDirectiveDeclaration,
isAngularServiceDeclaration: isAngularServiceDeclaration,
isAngularModuleDeclaration: isAngularModuleDeclaration,
isAngularModuleGetter: isAngularModuleGetter,
isAngularRunSection: isAngularRunSection,
isAngularConfigSection: isAngularConfigSection,
isRouteDefinition: isRouteDefinition,
isUIRouterStateDefinition: isUIRouterStateDefinition,
findIdentiferInScope: findIdentiferInScope,
getControllerDefinition: getControllerDefinition,
isAngularServiceImport: isAngularServiceImport
};
/**
* Recursively grab the callee until an Identifier is found.
*
* @todo Needs better documentation.
*/
function getCallingIdentifier(calleeObject) {
if (calleeObject.type && calleeObject.type === 'Identifier') {
return calleeObject;
}
if (calleeObject.callee && calleeObject.callee.object) {
return getCallingIdentifier(calleeObject.callee.object);
}
return null;
}
/**
* Convert a prefix string to a RegExp.
*
* `'/app/'` → `/app.*\/`
*
* @param {string} prefix
* @returns {RegExp}
*/
function convertPrefixToRegex(prefix) {
if (typeof prefix !== 'string') {
return prefix;
}
if (prefix[0] === '/' && prefix[prefix.length - 1] === '/') {
prefix = prefix.substring(1, prefix.length - 2);
}
return new RegExp(prefix + '.*');
}
/**
* Convert a string to a RegExp.
*
* `'app'` → `/app/`
* `'/app/'` → `/app/`
*
* @param {string} prefix
* @returns {RegExp}
*/
function convertStringToRegex(string) {
if (string[0] === '/' && string[string.length - 1] === '/') {
string = string.substring(1, string.length - 2);
}
return new RegExp(string);
}
/**
* @todo Missing documentation
*/
function isTypeOfStatement(node) {
return node.type === 'Identifier' || (node.type === 'UnaryExpression' && node.operator === 'typeof');
}
/**
* @todo Missing documentation
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is a `toString` statement.
*/
function isToStringStatement(node) {
return node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'MemberExpression' &&
node.callee.object.property.name === 'toString' &&
node.callee.property.name === 'call' &&
node.callee.object.object.type === 'MemberExpression' &&
node.callee.object.object.object.name === 'Object' &&
node.callee.object.object.property.name === 'prototype';
}
/**
* 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.
*/
function isArrayType(node) {
return node !== undefined && node.type === 'ArrayExpression';
}
/**
* 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.
*/
function isFunctionType(node) {
return node !== undefined && (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression');
}
/**
* Check whether or not a node is an named FunctionExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an named FunctionExpression.
*/
function isNamedInlineFunction(node) {
return this.isFunctionType(node) && node.id && node.id.name && node.id.name.length > 0;
}
/**
* 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.
*/
function isIdentifierType(node) {
return node !== undefined && node.type === 'Identifier';
}
/**
* Check whether or not a node is an MemberExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an MemberExpression.
*/
function isMemberExpression(node) {
return node !== undefined && node.type === 'MemberExpression';
}
/**
* 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.
*/
function isLiteralType(node) {
return node !== undefined && node.type === 'Literal';
}
/**
* Check whether or not a node is a CallExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is a CallExpression.
*/
function isCallExpression(node) {
return node !== undefined && node.type === 'CallExpression';
}
/**
* Check whether or not a node is an isEmptyFunction.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an isEmptyFunction.
*/
function isEmptyFunction(fn) {
return fn.body.body.length === 0;
}
/**
* Check whether or not a string resembles a regular expression.
*
* A string is considered a regular expression if it starts and ends with `/`.
*
* @param {string} The string to check.
* @returns {boolean} Whether or not a string resembles a regular expression.
*/
function isStringRegexp(string) {
return string[0] === '/' && string[string.length - 1] === '/';
}
/**
* Check if a CallExpression node somewhat resembles an Angular component.
*
* The following are considered Angular components
* ```js
* app.factory('kittenService', function() {})
* ^^^^^^^
* app.factory('kittenService', kittenService)
* ^^^^^^^
* app.factory('kittenService', [])
* ^^^^^^^
* app.factory('kittenService', require(""))
* ^^^^^^^
* asyncFn('value', callback)
* ^^^^^^^
* ```
*
* @todo FIXME
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node somewhat resembles an Angular component.
*/
function isAngularComponent(node) {
return node.arguments !== undefined &&
node.arguments.length === 2 &&
isLiteralType(node.arguments[0]) &&
(isIdentifierType(node.arguments[1]) ||
isFunctionType(node.arguments[1]) ||
isArrayType(node.arguments[1]) ||
isCallExpression(node.arguments[1]));
}
/**
* Check whether a CallExpression node defines an Angular component.
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node defines an Angular component.
*/
function isAngularComponentDeclaration(node) {
return isAngularComponent(node) &&
isMemberExpression(node.callee) &&
node.callee.property.name === 'component';
}
/**
* Check whether a CallExpression node defines an Angular controller.
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node defines an Angular controller.
*/
function isAngularControllerDeclaration(node) {
return isAngularComponent(node) &&
isMemberExpression(node.callee) &&
node.callee.property.name === 'controller';
}
/**
* Check whether a CallExpression node defines an Angular filter.
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node defines an Angular filter.
*/
function isAngularFilterDeclaration(node) {
return isAngularComponent(node) &&
isMemberExpression(node.callee) &&
node.callee.property.name === 'filter';
}
/**
* Check whether a CallExpression node defines an Angular directive.
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node defines an Angular directive.
*/
function isAngularDirectiveDeclaration(node) {
return isAngularComponent(node) &&
isMemberExpression(node.callee) &&
node.callee.property.name === 'directive';
}
/**
* Check whether a node defines an Angular service.
*
* The following are considered services
* ```js
* app.provider('kittenServiceProvider', function() {})
* ^^^^^^^^
* app.factory('kittenService', function() {})
* ^^^^^^^
* app.service('kittenService', function() {})
* ^^^^^^^
* app.constant('KITTENS', function() {})
* ^^^^^^^^
* app.value('KITTENS', function() {})
* ^^^^^
* ```
*
* The following are not considered services
* ```js
* $provide.factory('kittenService', function() {})
* app.constant('KITTENS', 'meow')
* app.value('KITTENS', 'purr')
* this.$get = function() {}
* ```
*
* @todo FIXME
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node defines an Angular controller.
*/
function isAngularServiceDeclaration(node) {
return isAngularComponent(node) &&
isMemberExpression(node.callee) &&
node.callee.object.name !== '$provide' &&
(node.callee.property.name === 'provider' ||
node.callee.property.name === 'service' ||
node.callee.property.name === 'factory' ||
node.callee.property.name === 'constant' ||
node.callee.property.name === 'value');
}
/**
* Check whether a CallExpression node declares an Angular module.
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node declares an Angular module.
*/
function isAngularModuleDeclaration(node) {
return isAngularComponent(node) &&
isMemberExpression(node.callee) &&
node.callee.property.name === 'module';
}
/**
* Check whether a CallExpression node gets or declares an Angular module.
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node gets or declares an Angular module.
*/
function isAngularModuleGetter(node) {
return node.arguments !== undefined &&
node.arguments.length > 0 &&
(isLiteralType(node.arguments[0]) || isIdentifierType(node.arguments[0])) &&
node.callee.type === 'MemberExpression' &&
node.callee.property.name === 'module';
}
/**
* Check whether a CallExpression node defines an Angular run function.
*
* The following are considered run functions
* ```js
* app.run()
* ^^^
* app.run(function() {})
* ^^^
* ```
*
* The following are not considered run functions
* ```js
* angular.module('myApp').run(function() {})
* angular.module('myApp', []).run(function() {})
* mocha.run()
* ```
*
* @todo FIXME
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node defines an Angular run function.
*/
function isAngularRunSection(node) {
return isMemberExpression(node.callee) &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'run' &&
(node.callee.object.type === 'Identifier' &&
node.callee.object.name !== 'mocha');
}
/**
* Check whether a CallExpression node defines an Angular config function.
*
* The following are considered config functions
* ```js
* app.config()
* ^^^^^^
* app.config(function() {})
* ^^^^^^
* ```
*
* The following are not considered run functions
* ```js
* angular.module('myApp').config(function() {})
* angular.module('myApp', []).config(function() {})
* ```
*
* @todo FIXME
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node defines an Angular config function.
*/
function isAngularConfigSection(node) {
return isMemberExpression(node.callee) &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'config';
}
/**
* Check whether a CallExpression node defines a route using $routeProvider.
*
* The following are considered routes:
* ```js
* $routeProvider.when()
* ^^^^
* ```
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node defines a route.
*/
function isRouteDefinition(node) {
// the route def function is .when(), so when we find that, go up through the chain and make sure
// $routeProvider is the calling object
if (node.callee.property && node.callee.property.name === 'when') {
var callObject = getCallingIdentifier(node.callee.object);
return callObject && callObject.name === '$routeProvider';
}
return false;
}
/**
* Check whether a CallExpression node defines a state using $stateProvider.
*
* The following are considered states:
* ```js
* $stateProvider.state()
* ^^^^^
* ```
*
* @param {Object} node The CallExpression node to check.
* @returns {boolean} Whether or not the node defines a state.
*/
function isUIRouterStateDefinition(node) {
// the state def function is .state(), so when we find that, go up through the chain and make sure
// $stateProvider is the calling object
if (node.callee.property && node.callee.property.name === 'state') {
var callObject = getCallingIdentifier(node.callee.object);
return callObject && callObject.name === '$stateProvider';
}
return false;
}
/**
* Find an identifier node in the current scope.
*
* @param {Object} context The context to use to get the scope.
* @param {Object} identifier The identifier node to look up.
*
* @returns {Object} The node declaring the identifier.
*/
function findIdentiferInScope(context, identifier) {
var identifierNode = null;
context.getScope().variables.forEach(function(variable) {
if (variable.name === identifier.name) {
identifierNode = variable.defs[0].node;
if (identifierNode.type === 'VariableDeclarator') {
identifierNode = identifierNode.init;
}
}
});
return identifierNode;
}
/**
* Find the function definition of a controller in the current context.
*
* @param {Object} context The context to use to find the controller declaration.
* @param {Object} node The Angular controller call to look up the declaration for.
*
* @returns {Object} The identifier declaring the controller function.
*/
function getControllerDefinition(context, node) {
var controllerArg = node.arguments[1];
// Three ways of creating a controller function: function expression,
// variable name that references a function, and an array with a function
// as the last item
if (isFunctionType(controllerArg)) {
return controllerArg;
}
if (isArrayType(controllerArg)) {
controllerArg = controllerArg.elements[controllerArg.elements.length - 1];
if (isIdentifierType(controllerArg)) {
return findIdentiferInScope(context, controllerArg);
}
return controllerArg;
}
Eif (isIdentifierType(controllerArg)) {
return findIdentiferInScope(context, controllerArg);
}
}
/**
* Check if the imported service support these two syntaxes : serviceName and _serviceName_
*
* @param {string} parameterName The label of the parameter.
* @param {string} serviceName The name of the service.
*
* @returns {boolean} True if the service use on of these previous syntaxes.
*/
function isAngularServiceImport(parameterName, serviceName) {
var r = new RegExp('^\_?' + serviceName.replace(/[!@#$%^&*()+=\-[\]\\';,./{}|":<>?~_]/g, '\\$&') + '\_?$', 'i');
return r.test(parameterName);
}
|