forked from olov/ng-annotate
-
Notifications
You must be signed in to change notification settings - Fork 27
/
nginject.js
411 lines (351 loc) · 11.8 KB
/
nginject.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
// nginject.js
// MIT licensed, see LICENSE file
// Copyright (c) 2013-2016 Olov Lassus <[email protected]>
"use strict";
const is = require("simple-is");
const t = require('@babel/types');
const codeFrame = require("@babel/code-frame");
module.exports = {
inspectComment: inspectComment,
inspectCallExpression: inspectCallExpression,
inspectFunction: inspectFunction,
inspectObjectExpression: inspectObjectExpression,
inspectAssignment: inspectAssignment,
inspectDeclarator: inspectDeclarator,
inspectClassDeclaration: inspectClassDeclaration,
inspectClassMethod: inspectClassMethod,
inspectExportDeclaration: inspectExportDeclaration
};
function inspectCallExpression(path, ctx) {
const node = path.node;
const name = node.callee.name;
if(inspectComment(path, ctx)){
return false;
}
if (t.isIdentifier(node.callee) && (name === "ngInject" || name === "ngNoInject") && node.arguments.length === 1) {
const block = (name === "ngNoInject");
addSuspect(path.get("arguments")[0], ctx, block);
}
path.get("arguments").forEach(arg => {
let annotation = getAnnotation(arg.node);
if(!t.isIdentifier(arg) || annotation === null){
return;
}
let binding = path.scope.getBinding(arg.node.name);
if(binding){
addSuspect(binding.path, ctx, !annotation);
}
});
}
function inspectFunction(path, ctx) {
const node = path.node;
if(t.isVariableDeclarator(path.parent) && t.isVariableDeclaration(path.parentPath.parent)){
let annotation = getAnnotation(path.parentPath.parent);
if(annotation === null){
annotation = getAnnotation(node);
}
if(annotation !== null){
addSuspect(path.parentPath.parentPath, ctx, !annotation);
return;
}
}
if(inspectComment(path, ctx)){
return;
}
const annotate = matchPrologueDirectives(path);
if (annotate === null) {
return;
}
// now add the correct suspect
// for function declarations, it is always the function declaration node itself
if (t.isFunctionDeclaration(node)) {
addSuspect(path, ctx, !annotate);
return;
}
// node is a function expression below
// case 1: a function expression which is the rhs of a variable declarator, such as
// var f1 = function(a) {
// "ngInject"
// };
// in this case we can mark the declarator, same as saying var /*@ngInject*/ f1 = function(a) ..
// or /*@ngInject*/ var f1 = function(a) ..
// f1.$inject = ["a"]; will be added (or rebuilt/removed)
if (t.isVariableDeclarator(path.parent)) {
addSuspect(path.parentPath, ctx, !annotate);
return;
}
// case 2: an anonymous function expression, such as
// g(function(a) {
// "ngInject"
// });
//
// the suspect is now its parent annotated array (if any), otherwise itself
// there is a risk of false suspects here, in case the parent annotated array has nothing to do
// with annotations. the risk should be very low and hopefully easy to workaround
//
// added/rebuilt/removed => g(["a", function(a) {
// "ngInject"
// }]);
const maybeArrayExpression = path.parentPath;
if (isAnnotatedArray(maybeArrayExpression)) {
addSuspect(maybeArrayExpression, ctx, !annotate);
} else {
addSuspect(path, ctx, !annotate);
}
}
function inspectComment(path, ctx) {
const node = path.node;
let annotation = getAnnotation(node);
if(annotation !== null){
addSuspect(path, ctx, !annotation);
return true;
}
}
function getAnnotation(node){
if(!node.leadingComments){
return null;
}
for(var i=0; i<node.leadingComments.length; i++){
for(const line of node.leadingComments[i].value.split("\n")) {
let value = line
.replace(/^[\s\*]*/, '')
.replace(/[\s\*]*$/, '')
.trim();
if(value === "@ngInject"){
return true;
} else if (value === "@ngNoInject") {
return false;
}
}
}
return null;
}
function getAnnotations(nodes){
for (var i = 0; i < nodes.length; i++){
let annotation = getAnnotation(nodes[i]);
if(annotation !== null){
return annotation;
}
}
return null;
}
function inspectObjectExpression(path, ctx) {
const node = path.node;
// to pick up annotations that should apply to all properties
// ie. /*@ngAnnotate*/ {}
var candidates = [node];
if(t.isAssignmentExpression(path.parent)){
candidates.unshift(path.parent);
if(t.isExpressionStatement(path.parentPath.parent)){
candidates.unshift(path.parentPath.parent);
}
}
if(t.isVariableDeclarator(path.parent) && t.isVariableDeclaration(path.parentPath.parent)){
candidates.unshift(path.parentPath.parent);
}
let annotateEverything = getAnnotations(candidates);
if(annotateEverything !== null){
addSuspect(path, ctx, !annotateEverything);
} else {
path.get("properties")
.filter(prop => isFunctionExpressionOrArrow(prop.node.value))
.forEach(prop => inspectComment(prop, ctx));
}
// path.get("properties").forEach(prop => {
// if(t.isObjectExpression(prop.node.value)){
// inspectObjectExpression(prop.get("value"), ctx);
// return;
// }
// let annotation = getAnnotation(prop.node);
// if(annotation !== null || annotateEverything !== null){
// let effectiveAnnotation = annotation === null ? annotateEverything : annotation;
// addSuspect(prop.get("value"), ctx, !effectiveAnnotation);
// }
// });
}
function matchPrologueDirectives(path) {
const prologueDirectives = ["ngInject", "ngNoInject"];
const directives = path.node.body.directives || [];
let matches = directives.map(dir => dir.value.value)
.filter(val => prologueDirectives.indexOf(val) !== -1);
if(matches.length){
let match = matches[0].trim();
if(match === "ngInject") return true;
if(match === "ngNoInject") return false;
}
return null;
}
function inspectAssignment(path, ctx){
const node = path.node;
if(!isFunctionExpressionOrArrow(node.right)){
return;
}
var candidates = [path.node, node.right];
if(t.isExpressionStatement(path.parent)){
candidates.unshift(path.parent);
path = path.parentPath;
}
let annotation = getAnnotations(candidates);
if(annotation !== null){
addSuspect(path, ctx, !annotation);
}
}
function inspectDeclarator(path, ctx){
const node = path.node;
if(!isFunctionExpressionOrArrow(node.init)){
return;
}
var candidates = [node, node.init];
if(t.isVariableDeclaration(path.parent)){
path = path.parentPath;
} else {
console.error("not a variable declaration");
}
let annotation = getAnnotations(candidates);
if(annotation !== null){
addSuspect(path, ctx, !annotation);
}
}
function inspectClassDeclaration(path, ctx){
const node = path.node;
let annotation = getAnnotation(node);
if(annotation !== null){
addSuspect(path, ctx, !annotation);
}
}
function inspectClassMethod(path, ctx){
const node = path.node;
if(node.kind !== 'constructor'){
return;
}
let annotation = getAnnotation(path.node);
if(annotation === null){
annotation = matchPrologueDirectives(path);
if(annotation === null) {
return;
}
}
const ancestry = path.getAncestry();
for(var i=0; i < ancestry.length; i++){
let ancestor = ancestry[i];
if(ancestor.isClassDeclaration()){
addSuspect(ancestor, ctx, !annotation);
return;
}
}
}
function inspectExportDeclaration(path, ctx){
let annotation = getAnnotation(path.node);
if(annotation === null){
return;
}
addSuspect(path.get('declaration'), ctx, !annotation);
}
function isStringArray(node) {
if (!t.isArrayExpression(node)) {
return false;
}
return node.elements.length >= 1 && node.elements.every(function(n) {
return t.isLiteral(n) && is.string(n.value);
});
}
function findNextStatement(path) {
const body = path.parentPath.get("body");
for (let i = 0; i < body.length; i++) {
if (body[i].path === path.node) {
return body[i + 1] || null;
}
}
return null;
}
function addSuspect(path, ctx, block) {
const target = path.node;
if (t.isExpressionStatement(target) && t.isAssignmentExpression(target.expression) && isStringArray(target.expression.right)) {
// /*@ngInject*/
// FooBar.$inject = ["$a", "$b"];
// function FooBar($a, $b) {}
const adjustedTarget = findNextStatement(path);
if (adjustedTarget) {
return addSuspect(adjustedTarget, ctx, block);
}
}
if (t.isObjectExpression(path)) {
// /*@ngInject*/ {f1: function(a), .., {f2: function(b)}}
addObjectExpression(path, ctx);
} else if (t.isAssignmentExpression(target) && t.isObjectExpression(target.right)) {
// /*@ngInject*/ f(x.y = {f1: function(a), .., {f2: function(b)}})
addObjectExpression(target.get("right"), ctx);
} else if (t.isExpressionStatement(target) && t.isAssignmentExpression(target.expression) && t.isObjectExpression(target.expression.right)) {
// /*@ngInject*/ x.y = {f1: function(a), .., {f2: function(b)}}
addObjectExpression(target.get("expression.right"), ctx);
} else if (t.isVariableDeclaration(target) && target.declarations.length === 1 && target.declarations[0].init && t.isObjectExpression(target.declarations[0].init)) {
// /*@ngInject*/ var x = {f1: function(a), .., {f2: function(b)}}
addObjectExpression(target.get("declarations")[0].get("init"), ctx);
} else if (t.isProperty(target)) {
// {/*@ngInject*/ justthisone: function(a), ..}
let value = path.get("value");
value.$limitToMethodName = "*never*";
addOrBlock(value, ctx);
} else {
// /*@ngInject*/ function(a) {}
path.$limitToMethodName = "*never*";
addOrBlock(path, ctx);
}
function addObjectExpression(path, ctx) {
nestedObjectValues(path).forEach(function(n) {
n.$limitToMethodName = "*never*";
addOrBlock(n, ctx);
});
}
function addOrBlock(path, ctx) {
if (block) {
ctx.blocked.push(path);
} else {
ctx.addModuleContextIndependentSuspect(path, ctx)
}
}
}
function nestedObjectValues(path, res) {
res = res || [];
path.get("properties").forEach(function(prop) {
const v = prop.get("value");
if (isFunctionExpressionOrArrow(v) || t.isArrayExpression(v)) {
res.push(v);
} else if (t.isObjectExpression(v)) {
nestedObjectValues(v, res);
}
});
return res;
}
function isAnnotatedArray(path) {
if (!t.isArrayExpression(path)) {
return false;
}
const elements = path.get('elements');
// last should be a function expression
let fn = elements.slice(-1)[0];
if (elements.length === 0 || !isFunctionExpressionOrArrow(fn)) {
return false;
}
var fnParams = fn.node.params.map(param => param.name);
if(fnParams.length > elements.length - 1){
throw path.buildCodeFrameError("[angularjs-annotate] ERROR: Function parameters do not match existing annotations.");
}
var warnedOnce = false;
// all but last should be string literals
for (let i = 0; i < elements.length - 1; i++) {
const n = elements[i];
if (!t.isLiteral(n) || !is.string(n.node.value)) {
return false;
}
if (!warnedOnce && fnParams[i] && n.node.value !== fnParams[i]) {
warnedOnce = true;
var frame = codeFrame(n.hub.file.code, n.node.loc.start.line, n.node.loc.start.column + 2);
console.warn("[angularjs-annotate] WARN: Function parameters do not match existing annotations.\n" + frame);
}
}
return true;
}
function isFunctionExpressionOrArrow(node) {
return t.isFunctionExpression(node) || t.isArrowFunctionExpression(node);
}