-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
exportParser.js
328 lines (288 loc) · 8.4 KB
/
exportParser.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
import debugModule from 'debug';
const debug = debugModule('requireExportJsdoc');
const createNode = function () {
return {
props: {}
};
};
const getSymbolValue = function (symbol) {
if (!symbol) {
return null;
}
if (symbol.type === 'literal') {
return symbol.value.value;
}
return null;
};
const getIdentifier = function (node, globals, scope, opts) {
if (opts.simpleIdentifier) {
// Type is Identier for noncomputed properties
const identifierLiteral = createNode();
identifierLiteral.type = 'literal';
identifierLiteral.value = {value: node.name};
return identifierLiteral;
}
const block = scope || globals;
// As scopes are not currently supported, they are not traversed upwards recursively
if (block.props[node.name]) {
return block.props[node.name];
}
if (globals.props[node.name]) {
return globals.props[node.name];
}
return null;
};
let createSymbol = null;
const getSymbol = function (node, globals, scope, opt) {
const opts = opt || {};
let block = scope;
switch (node.type) {
case 'Identifier': {
return getIdentifier(node, globals, scope, opts);
} case 'MemberExpression': {
const obj = getSymbol(node.object, globals, scope, opts);
const propertySymbol = getSymbol(node.property, globals, scope, {simpleIdentifier: !node.computed});
const propertyValue = getSymbolValue(propertySymbol);
if (obj && propertyValue && obj.props[propertyValue]) {
block = obj.props[propertyValue];
return block;
}
if (opts.createMissingProps && propertyValue) {
obj.props[propertyValue] = createNode();
return obj.props[propertyValue];
}
debug('MemberExpression: Missing property ' + node.property.name);
return null;
} case 'ClassDeclaration': case 'FunctionExpression': case 'FunctionDeclaration': case 'ArrowFunctionExpression': {
const val = createNode();
val.props.prototype = createNode();
val.props.prototype.type = 'object';
val.type = 'object';
val.value = node;
return val;
} case 'AssignmentExpression': {
return createSymbol(node.left, globals, node.right, scope, opts);
} case 'ClassBody': {
const val = createNode();
node.body.forEach((method) => {
val.props[method.key.name] = createNode();
val.props[method.key.name].type = 'object';
val.props[method.key.name].value = method.value;
});
val.type = 'object';
val.value = node;
return val;
} case 'ObjectExpression': {
const val = createNode();
val.type = 'object';
node.properties.forEach((prop) => {
const propVal = getSymbol(prop.value, globals, scope, opts);
if (propVal) {
val.props[prop.key.name] = propVal;
}
});
return val;
} case 'Literal': {
const val = createNode();
val.type = 'literal';
val.value = node;
return val;
}
}
return null;
};
createSymbol = function (node, globals, value, scope) {
const block = scope || globals;
let symbol;
switch (node.type) {
case 'ClassDeclaration': {
if (node.id.type === 'Identifier') {
return createSymbol(node.id, globals, node, globals);
}
break;
} case 'Identifier': {
if (value) {
const valueSymbol = getSymbol(value, globals, block);
if (valueSymbol) {
block.props[node.name] = valueSymbol;
return block.props[node.name];
}
debug('Identifier: Missing value symbol for %s', node.name);
} else {
block.props[node.name] = createNode();
return block.props[node.name];
}
break;
} case 'MemberExpression': {
symbol = getSymbol(node.object, globals, block);
const propertySymbol = getSymbol(node.property, globals, block, {simpleIdentifier: !node.computed});
const propertyValue = getSymbolValue(propertySymbol);
if (symbol && propertyValue) {
symbol.props[propertyValue] = getSymbol(value, globals, block);
return symbol.props[propertyValue];
}
debug('MemberExpression: Missing symbol: %s', node.property.name);
break;
} case 'FunctionDeclaration': {
if (node.id.type === 'Identifier') {
return createSymbol(node.id, globals, node, globals);
}
break;
}
}
return null;
};
// Creates variables from variable definitions
const initVariables = function (node, globals) {
switch (node.type) {
case 'Program': {
node.body.forEach((childNode) => {
initVariables(childNode, globals);
});
break;
} case 'ExpressionStatement': {
initVariables(node.expression, globals);
break;
} case 'VariableDeclaration': {
node.declarations.forEach((declaration) => {
// let and const
const symbol = createSymbol(declaration.id, globals, null, globals);
if (node.kind === 'var' && globals.props.window) {
// If var, also add to window
globals.props.window.props[declaration.id.name] = symbol;
}
});
break;
}
}
};
// Populates variable maps using AST
const mapVariables = function (node, globals) {
switch (node.type) {
case 'Program': {
node.body.forEach((childNode) => {
mapVariables(childNode, globals);
});
break;
} case 'ExpressionStatement': {
mapVariables(node.expression, globals);
break;
} case 'AssignmentExpression': {
createSymbol(node.left, globals, node.right);
break;
} case 'VariableDeclaration': {
node.declarations.forEach((declaration) => {
createSymbol(declaration.id, globals, declaration.init);
});
break;
} case 'FunctionDeclaration': {
if (node.id.type === 'Identifier') {
createSymbol(node.id, globals, node, globals);
}
break;
} case 'ExportDefaultDeclaration': {
const symbol = createSymbol(node.declaration, globals, node.declaration);
if (symbol) {
symbol.exported = true;
}
break;
} case 'ExportNamedDeclaration': {
if (node.declaration) {
const symbol = createSymbol(node.declaration, globals, node.declaration);
symbol.exported = true;
}
node.specifiers.forEach((specifier) => {
mapVariables(specifier, globals);
});
break;
} case 'ExportSpecifier': {
const symbol = getSymbol(node.local, globals, globals);
if (symbol) {
symbol.exported = true;
}
break;
} case 'ClassDeclaration': {
createSymbol(node.id, globals, node.body, globals);
break;
}
}
};
const findNode = function (node, block, cache) {
let blockCache = cache || [];
if (blockCache.includes(block)) {
return false;
}
blockCache = blockCache.slice();
blockCache.push(block);
if (block.type === 'object') {
if (block.value === node) {
return true;
}
}
for (const prop in block.props) {
if (Object.prototype.hasOwnProperty.call(block.props, prop)) {
const propval = block.props[prop];
// Only check node if it had resolvable value
if (propval && findNode(node, propval, blockCache)) {
return true;
}
}
}
return false;
};
const findExportedNode = function (block, node, cache) {
const blockCache = cache || [];
for (const key in block.props) {
if (Object.prototype.hasOwnProperty.call(block.props, key)) {
blockCache.push(block.props[key]);
if (block.props[key].exported) {
if (findNode(node, block)) {
return true;
}
}
if (!blockCache.includes(block.props[key]) && findExportedNode(block.props[key], node, blockCache)) {
return true;
}
}
}
return false;
};
const isNodeExported = function (node, globals, opt) {
if (opt.initModuleExports && globals.props.module && globals.props.module.props.exports) {
if (findNode(node, globals.props.module.props.exports)) {
return true;
}
}
if (opt.exports && findExportedNode(globals, node)) {
return true;
}
return false;
};
const parse = function (ast, opt) {
const opts = opt || {
exports: true,
initModuleExports: true,
initWindow: true
};
const globalVars = createNode();
if (opts.initModuleExports) {
globalVars.props.module = createNode();
globalVars.props.module.props.exports = createNode();
globalVars.props.exports = globalVars.props.module.props.exports;
}
if (opts.initWindow) {
globalVars.props.window = globalVars;
}
initVariables(ast, globalVars);
mapVariables(ast, globalVars);
return {
globalVars
};
};
const isExported = function (node, parseResult, opt) {
return isNodeExported(node, parseResult.globalVars, opt);
};
export default {
isExported,
parse
};