forked from schmod/babel-plugin-angularjs-annotate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
babel-ng-annotate.js
159 lines (145 loc) · 4.22 KB
/
babel-ng-annotate.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
'use strict';
var ngAnnotate = require('./ng-annotate-main');
const ngInject = require('./nginject');
const is = require('simple-is');
const match = ngAnnotate.match;
const addModuleContextDependentSuspect = ngAnnotate.addModuleContextDependentSuspect;
const addModuleContextIndependentSuspect = ngAnnotate.addModuleContextIndependentSuspect;
const judgeSuspects = ngAnnotate.judgeSuspects;
const matchDirectiveReturnObject = ngAnnotate.matchDirectiveReturnObject;
const matchProviderGet = ngAnnotate.matchProviderGet;
module.exports = function() {
var options = {};
const re = (options.regexp ? new RegExp(options.regexp) : /^[a-zA-Z0-9_\$\.\s]+$/);
// suspects is built up with suspect nodes by match.
// A suspect node will get annotations added / removed if it
// fulfills the arrayexpression or functionexpression look,
// and if it is in the correct context (inside an angular
// module definition)
const suspects = [];
// blocked is an array of blocked suspects. Any target node
// (final, i.e. IIFE-jumped, reference-followed and such) included
// in blocked will be ignored by judgeSuspects
const blocked = [];
const ctx = {
re: re,
suspects: suspects,
blocked: blocked,
addModuleContextDependentSuspect: addModuleContextDependentSuspect,
addModuleContextIndependentSuspect: addModuleContextIndependentSuspect
};
var addTargets = function(targets) {
if (!targets) {
return;
}
if (!is.array(targets)) {
targets = [targets];
}
for (let i = 0; i < targets.length; i++) {
addModuleContextDependentSuspect(targets[i], ctx);
}
};
return {
visitor: {
AssignmentExpression: {
enter(path) {
ngInject.inspectAssignment(path, ctx);
},
exit(path, state) {
if(!state.opts.explicitOnly){
let targets = matchProviderGet(path);
addTargets(targets);
}
}
},
VariableDeclarator: {
enter(path) {
ngInject.inspectDeclarator(path, ctx);
}
},
ClassDeclaration: {
enter(path) {
ngInject.inspectClassDeclaration(path, ctx);
}
},
ClassMethod: {
enter(path) {
ngInject.inspectClassMethod(path, ctx);
}
},
ObjectExpression: {
enter(path) {
ngInject.inspectObjectExpression(path, ctx);
},
exit(path, state) {
if(!state.opts.explicitOnly){
let targets = matchProviderGet(path);
addTargets(targets);
}
}
},
ReturnStatement: {
exit(path, state) {
if(!state.opts.explicitOnly){
let targets = matchDirectiveReturnObject(path);
addTargets(targets);
}
}
},
FunctionExpression: {
enter(path) {
ngInject.inspectFunction(path, ctx);
}
},
ArrowFunctionExpression: {
enter(path) {
ngInject.inspectFunction(path, ctx);
},
exit(path, state) {
if(!state.opts.explicitOnly){
let targets = matchDirectiveReturnObject(path);
addTargets(targets);
}
}
},
FunctionDeclaration: {
enter(path) {
ngInject.inspectFunction(path, ctx);
}
},
ObjectMethod: {
enter(path) {
ngInject.inspectFunction(path, ctx);
}
},
CallExpression: {
enter(path) {
ngInject.inspectCallExpression(path, ctx);
},
exit(path, state) {
let targets = match(path, ctx, state.opts.explicitOnly);
addTargets(targets);
}
},
ExportDeclaration: {
enter(path) {
ngInject.inspectExportDeclaration(path, ctx);
}
},
Program: {
enter(path, file) {
file.opts.explicitOnly = file.opts.explicitOnly || false;
ctx.suspects = [];
ctx.blocked = [];
ctx.fragments = [];
ctx.srcForRange = function(node) {
return file.file.code.slice(node.start, node.end);
};
},
exit() {
judgeSuspects(ctx);
}
}
}
};
}