This repository has been archived by the owner on Feb 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
used-exports.js
149 lines (118 loc) · 3.14 KB
/
used-exports.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
const {parse} = require("babylon");
const traverse = require("@babel/traverse").default;
const t = require("@babel/types");
function identEq(l, r) {
return l.name === r.name;
}
function parseSource(source) {
return parse(source, {
sourceType: "module",
plugins: [
"jsx",
]
});
}
/**
* We found a local binding from the wasm binary.
*
* `import x from 'module.wasm'`
* ^
*/
function onLocalModuleBinding(ident, ast, acc) {
traverse(ast, {
CallExpression({node: callExpression}) {
// left must be a member expression
if (t.isMemberExpression(callExpression.callee) === false) {
return;
}
const memberExpression = callExpression.callee;
/**
* Search for `makeX().then(...)`
*/
if (
t.isCallExpression(memberExpression.object)
&& memberExpression.object.callee.name === ident.name
&& memberExpression.property.name === "then"
) {
const [thenFnBody] = callExpression.arguments;
if (typeof thenFnBody === "undefined") {
return;
}
onInstanceThenFn(thenFnBody, acc);
}
}
});
}
/**
* We found the function handling the module instance
*
* `makeX().then(...)`
*/
function onInstanceThenFn(fn, acc) {
if (t.isArrowFunctionExpression(fn) === false) {
throw new Error("Unsupported function type: " + fn.type);
}
let [localIdent] = fn.params;
/**
* `then(({exports}) => ...)`
*
* We need to resolve the identifier (binding) from the ObjectPattern.
*
* TODO(sven): handle renaming the binding here
*/
if (t.isObjectPattern(localIdent) === true) {
// ModuleInstance has the exports prop by spec
localIdent = t.identifier('exports');
}
traverse(fn.body, {
noScope: true,
MemberExpression(path) {
const {object, property} = path.node;
/**
* Search for `localIdent.exports`
*/
if (
identEq(object, localIdent) === true
&& t.isIdentifier(property, {name: 'exports'})
) {
/**
* We are looking for the right branch of the parent MemberExpression:
* `(localIdent.exports).x`
* ^
*/
let {property} = path.parentPath.node;
// Found an usage of an export
acc.push(property.name);
path.stop();
}
/**
* `exports` might be a local binding (from destructuring)
*/
else if (identEq(object, localIdent) === true) {
// Found an usage of an export
acc.push(property.name);
path.stop();
}
}
});
}
module.exports = function (source) {
const usedExportsByModuleName = {};
const ast = parseSource(source);
traverse(ast, {
ImportDeclaration(path) {
const sourceValue = path.node.source.value;
const [specifier] = path.node.specifiers;
usedExportsByModuleName[sourceValue] = [];
if (t.isImportDefaultSpecifier(specifier)) {
onLocalModuleBinding(
specifier.local,
ast,
usedExportsByModuleName[sourceValue]
);
path.stop();
}
}
});
return usedExportsByModuleName;
};