-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionStatement.ts
396 lines (363 loc) · 10.3 KB
/
ExpressionStatement.ts
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
/*
* Copyright 2020-2023 Delft University of Technology and SynTest contributors
*
* This file is part of SynTest Framework - SynTest JavaScript.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NodePath } from "@babel/core";
import * as t from "@babel/types";
import { getLogger } from "@syntest/logging";
import { Export } from "./Export";
import { ExportVisitor } from "./ExportVisitor";
export type PartialExport = PartialDefaultExport | PartialNonDefaultExport;
type PartialDefaultExport = {
default: true;
};
type PartialNonDefaultExport = {
default: false;
renamedTo: string;
};
export function extractExportsFromAssignmentExpression(
visitor: ExportVisitor,
filePath: string,
path_: NodePath<t.AssignmentExpression>
): Export[] {
const left = path_.get("left");
const right = path_.get("right");
let partialExport: PartialExport | false = checkExportAndDefault(
visitor,
left
);
if (partialExport) {
return extractExportsFromLeftAssignmentExpression(
visitor,
filePath,
partialExport,
right
);
}
partialExport = checkExportAndDefault(visitor, right);
if (partialExport) {
return extractExportsFromRightAssignmentExpression(
visitor,
filePath,
left,
partialExport
);
}
return [];
}
export function extractExportsFromRightAssignmentExpression(
visitor: ExportVisitor,
filePath: string,
left: NodePath<t.LVal | t.Identifier>,
right: PartialExport
): Export[] {
const exports: Export[] = [];
const module = true;
const id = visitor._getBindingId(left);
const name = getName(left);
if (right.default) {
exports.push({
id: id,
filePath: filePath,
name: name,
renamedTo: name, // actually renamed to nothing aka default but we keep the name
default: right.default,
module: module,
});
} else {
exports.push({
id: id,
filePath: filePath,
name:
name === "default" ? (<PartialNonDefaultExport>right).renamedTo : name,
renamedTo: (<PartialNonDefaultExport>right).renamedTo,
default: right.default,
module: module,
});
}
return exports;
}
export function extractExportsFromLeftAssignmentExpression(
visitor: ExportVisitor,
filePath: string,
left: PartialExport,
right: NodePath<t.Expression>
): Export[] {
const exports: Export[] = [];
const module = true;
if (left.default && right.isObjectExpression()) {
// module.exports = {...}
// exports = {...}
// so not default actually
// extract the stuff from the object
const properties = right.get("properties");
exports.push(
...extractObjectProperties(right, properties, visitor, filePath, module)
);
} else if (left.default) {
// module.exports = ?
// exports = ?
// but ? is not an object expression
const id = visitor._getBindingId(right);
const name = getName(right);
exports.push({
id: id,
filePath: filePath,
name: name,
renamedTo: name, // actually renamed to nothing aka default but we keep the name
default: left.default,
module: module,
});
} else {
// module.exports.? = ?
// exports.? = ?
// ? can be object but we dont care since it is not a default export
const id = visitor._getBindingId(right);
const name = getName(right);
exports.push({
id: id,
filePath: filePath,
name:
name === "default" ? (<PartialNonDefaultExport>left).renamedTo : name,
renamedTo: (<PartialNonDefaultExport>left).renamedTo,
default: left.default,
module: module,
});
}
return exports;
}
export function extractObjectProperties(
path: NodePath<t.Node>,
properties: NodePath<t.ObjectMethod | t.ObjectProperty | t.SpreadElement>[],
visitor: ExportVisitor,
filePath: string,
module: boolean
): Export[] {
const default_ = false;
const exports: Export[] = [];
for (const property of properties) {
if (property.isObjectMethod()) {
// {a () {}}
const key = property.get("key");
if (!key.isIdentifier()) {
// e.g. exports = { () {} }
// unsupported
// not possible i think
throw new Error("Unsupported export declaration");
}
exports.push({
id: visitor._getNodeId(property),
filePath: filePath,
name: key.node.name,
renamedTo: key.node.name,
default: default_,
module: module,
});
} else if (property.isObjectProperty()) {
// {a: b}
const key = property.get("key");
const value = property.get("value");
let keyName: string;
if (
key.isStringLiteral() ||
key.isNumericLiteral() ||
key.isBooleanLiteral() ||
key.isBigIntLiteral()
) {
// e.g. exports = { "a": ? }
// e.g. exports = { 1: ? }
// e.g. exports = { true: ? }
keyName = String(key.node.value);
} else if (key.isIdentifier()) {
// e.g. exports = { a: ? }
keyName = key.node.name;
} else {
// e.g. exports = { ?: ? }
// unsupported
throw new Error("Unsupported export declaration");
}
if (value.isIdentifier()) {
// e.g. exports = { a: b }
exports.push({
id: visitor._getBindingId(value),
filePath,
name: value.node.name,
renamedTo: keyName,
default: default_,
module: module,
});
} else {
// e.g. exports = { a: 1 }
exports.push({
id: visitor._getBindingId(value),
filePath,
name: keyName,
renamedTo: keyName,
default: default_,
module: module,
});
}
} else {
// {...a}
// unsupported
if (visitor.syntaxForgiving) {
// Log it
getLogger("ExportVisitor").warn(
`Unsupported export declaration at ${visitor._getNodeId(path)}`
);
} else {
throw new Error(
`Unsupported export declaration at ${visitor._getNodeId(path)}`
);
}
}
}
return exports;
}
export function getName(
expression: NodePath<t.Expression | t.LVal | t.Identifier>
): string {
if (expression.isIdentifier()) {
return expression.node.name;
}
if (
expression.isLiteral() ||
expression.isArrayExpression() ||
expression.isObjectExpression()
) {
return expression.type;
}
if (expression.isFunction() || expression.isClass()) {
if (!expression.has("id")) {
return expression.isFunction() ? "anonymousFunction" : "anonymousClass";
}
const id = expression.get("id");
// must be identifier type if it is a nodepath
return (<NodePath<t.Identifier>>id).node.name;
}
return "default";
}
export function checkExportAndDefault(
visitor: ExportVisitor,
expression: NodePath<t.LVal | t.Expression>
): false | PartialExport {
if (expression.isIdentifier() && expression.node.name === "exports") {
// exports
return { default: true };
} else if (expression.isMemberExpression()) {
// ?.?
const object = expression.get("object");
const property = expression.get("property");
if (object.isIdentifier()) {
// ?.?
if (object.node.name === "module") {
if (
(!expression.node.computed &&
property.isIdentifier() &&
property.node.name === "exports") ||
(expression.node.computed &&
property.isStringLiteral() &&
property.node.value === "exports")
) {
// module.exports
// module['exports']
return { default: true };
} else {
// module[exports]
// TODO replace by logger
console.warn(`Unsupported syntax 'module[x] = ?'`);
}
} else if (object.node.name === "exports") {
// exports.?
const name = _getNameOfProperty(
visitor,
property,
expression.node.computed
);
if (!name) {
return false;
}
return {
default: false,
renamedTo: name,
};
}
} else if (object.isMemberExpression()) {
// ?.?.?
const subObject = object.get("object");
const subProperty = object.get("property");
if (
subObject.isIdentifier() &&
subObject.node.name === "module" &&
((!object.node.computed &&
subProperty.isIdentifier() &&
subProperty.node.name === "exports") ||
(object.node.computed &&
subProperty.isStringLiteral() &&
subProperty.node.value === "exports"))
) {
// module.exports.?
// module['exports'].?
// module.exports[?]
const name = _getNameOfProperty(
visitor,
property,
expression.node.computed
);
if (!name) {
return false;
}
return {
default: false,
renamedTo: name,
};
}
}
}
return false;
}
function _getNameOfProperty(
visitor: ExportVisitor,
property: NodePath<t.PrivateName | t.Expression>,
computed: boolean
): string | undefined {
if (computed) {
// module.exports[?] = ?
if (
property.isStringLiteral() ||
property.isNumericLiteral() ||
property.isBooleanLiteral() ||
property.isBigIntLiteral()
) {
// module.exports['x'] = ?
return String(property.node.value);
} else {
// module.exports[a] = ?
getLogger("ExportVisitor").warn(
`This tool does not support computed export statements. Found one at ${visitor._getNodeId(
property
)}`
);
return undefined;
}
} else if (property.isIdentifier()) {
// module.exports.x = ?
return property.node.name;
} else {
// module.exports.? = ?
throw new Error('Unsupported syntax "module.exports.? = ?"');
}
}