/
graphBuilder.ts
209 lines (181 loc) · 6.24 KB
/
graphBuilder.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
import { types as t } from '@babel/core';
import type { AssignmentExpression, Node, VisitorKeys } from '@babel/types';
import { isNode, getVisitorKeys } from '@linaria/babel-preset';
import DepsGraph from './DepsGraph';
import GraphBuilderState from './GraphBuilderState';
import { getVisitors } from './Visitors';
import type { VisitorAction } from './types';
import ScopeManager from './scope';
const isVoid = (node: Node): boolean =>
t.isUnaryExpression(node) && node.operator === 'void';
class GraphBuilder extends GraphBuilderState {
static build(root: Node): DepsGraph {
return new GraphBuilder(root).graph;
}
constructor(rootNode: Node) {
super();
this.visit(rootNode, null, null, null);
}
private isExportsIdentifier(node: Node) {
if (t.isIdentifier(node)) {
return (
this.scope.getDeclaration(node) === ScopeManager.globalExportsIdentifier
);
}
if (t.isMemberExpression(node)) {
return (
t.isIdentifier(node.property) &&
node.property.name === 'exports' &&
t.isIdentifier(node.object) &&
this.scope.getDeclaration(node.object) ===
ScopeManager.globalModuleIdentifier
);
}
return false;
}
private isExportsAssigment(node: Node): node is AssignmentExpression {
if (
node &&
t.isAssignmentExpression(node) &&
t.isMemberExpression(node.left)
) {
if (this.isExportsIdentifier(node.left)) {
// This is a default export like `module.exports = 42`
return true;
}
if (this.isExportsIdentifier(node.left.object)) {
// This is a named export like `module.exports.a = 42` or `exports.a = 42`
return true;
}
}
return false;
}
private isTSExporterCall(
node: Node
): node is t.CallExpression & { arguments: [t.StringLiteral, t.Identifier] } {
if (!t.isCallExpression(node) || node.arguments.length !== 2) {
return false;
}
// FIXME: more precisely check
return !(!t.isIdentifier(node.callee) || node.callee.name !== 'exporter');
}
/*
* Implements a default behaviour for AST-nodes:
* • visits every child;
* • if the current node is an Expression node, adds all its children as dependencies.
*
* eg. BinaryExpression has children `left` and `right`,
* both of them are required for evaluating the value of the expression
*/
baseVisit<TNode extends Node>(node: TNode, ignoreDeps = false) {
const dependencies = [];
const isExpression = t.isExpression(node);
const keys = getVisitorKeys(node);
for (const key of keys) {
// Ignore all types
if (key === 'typeArguments' || key === 'typeParameters') {
continue;
}
const subNode = node[key as keyof TNode];
if (Array.isArray(subNode)) {
for (let i = 0; i < subNode.length; i++) {
const child = subNode[i];
if (child && this.visit(child, node, key, i) !== 'ignore') {
dependencies.push(child);
}
}
} else if (
isNode(subNode) &&
this.visit(subNode, node, key) !== 'ignore'
) {
dependencies.push(subNode);
}
}
if (isExpression && !ignoreDeps) {
dependencies.forEach((dep) => this.graph.addEdge(node, dep));
}
this.callbacks.forEach((callback) => callback(node));
}
visit<TNode extends Node, TParent extends Node>(
node: TNode,
parent: TParent | null,
parentKey: VisitorKeys[TParent['type']] | null,
listIdx: number | null = null
): VisitorAction {
if (parent) {
this.graph.addParent(node, parent);
}
if (
this.isExportsAssigment(node) &&
!this.isExportsAssigment(node.right) &&
!isVoid(node.right)
) {
if (
t.isMemberExpression(node.left) &&
(t.isIdentifier(node.left.property) ||
t.isStringLiteral(node.left.property))
) {
if (
t.isIdentifier(node.left.object) &&
node.left.object.name === 'module'
) {
// It's a batch or default export
if (t.isObjectExpression(node.right)) {
// Batch export is a very particular case.
// Each property of the assigned object is independent named export.
// We also need to specify all dependencies and call `visit` for every value.
this.visit(node.left, node, 'left');
node.right.properties.forEach((prop) => {
if (t.isObjectProperty(prop) && t.isIdentifier(prop.key)) {
this.visit(prop.value, prop, 'value');
this.graph.addExport(prop.key.name, prop);
this.graph.addEdge(prop, node.right);
this.graph.addEdge(prop, prop.key);
this.graph.addEdge(prop.key, prop.value);
}
});
this.graph.addEdge(node.right, node);
this.graph.addEdge(node, node.left);
// We have done all the required work, so stop here
return;
} else {
this.graph.addExport('default', node);
}
} else {
// it can be either `exports.name` or `exports["name"]`
const nameNode = node.left.property;
this.graph.addExport(
t.isStringLiteral(nameNode) ? nameNode.value : nameNode.name,
node
);
}
}
} else if (this.isTSExporterCall(node)) {
const [name, identifier] = node.arguments;
this.graph.addExport(name.value, node);
this.graph.addEdge(node, identifier);
}
const isScopable = t.isScopable(node);
const isFunction = t.isFunction(node);
if (isScopable) this.scope.new(t.isProgram(node) || t.isFunction(node));
if (isFunction) this.fnStack.push(node);
const visitors = getVisitors(node);
let action: VisitorAction;
if (visitors.length > 0) {
let visitor;
while (!action && (visitor = visitors.shift())) {
action = visitor.call(this, node, parent, parentKey, listIdx);
}
} else {
this.baseVisit(node);
}
if (parent && action !== 'ignore') {
// Node always depends on its parent
this.graph.addEdge(node, parent);
}
if (isFunction) this.fnStack.pop();
if (isScopable) this.scope.dispose();
return action;
}
}
export default GraphBuilder.build;