InnerGraph.js
9.0 KB
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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Sergey Melyukov @smelukov
*/
"use strict";
const { UsageState } = require("../ExportsInfo");
/** @typedef {import("estree").Node} AnyNode */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
/** @typedef {import("../Parser").ParserState} ParserState */
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
/** @typedef {Map<TopLevelSymbol | null, Set<string | TopLevelSymbol> | true>} InnerGraph */
/** @typedef {function(boolean | Set<string> | undefined): void} UsageCallback */
/**
* @typedef {Object} StateObject
* @property {InnerGraph} innerGraph
* @property {TopLevelSymbol=} currentTopLevelSymbol
* @property {Map<TopLevelSymbol, Set<UsageCallback>>} usageCallbackMap
*/
/** @typedef {false|StateObject} State */
/** @type {WeakMap<ParserState, State>} */
const parserStateMap = new WeakMap();
const topLevelSymbolTag = Symbol("top level symbol");
/**
* @param {ParserState} parserState parser state
* @returns {State} state
*/
function getState(parserState) {
return parserStateMap.get(parserState);
}
/**
* @param {ParserState} parserState parser state
* @returns {void}
*/
exports.bailout = parserState => {
parserStateMap.set(parserState, false);
};
/**
* @param {ParserState} parserState parser state
* @returns {void}
*/
exports.enable = parserState => {
const state = parserStateMap.get(parserState);
if (state === false) {
return;
}
parserStateMap.set(parserState, {
innerGraph: new Map(),
currentTopLevelSymbol: undefined,
usageCallbackMap: new Map()
});
};
/**
* @param {ParserState} parserState parser state
* @returns {boolean} true, when enabled
*/
exports.isEnabled = parserState => {
const state = parserStateMap.get(parserState);
return !!state;
};
/**
* @param {ParserState} state parser state
* @param {TopLevelSymbol | null} symbol the symbol, or null for all symbols
* @param {string | TopLevelSymbol | true} usage usage data
* @returns {void}
*/
exports.addUsage = (state, symbol, usage) => {
const innerGraphState = getState(state);
if (innerGraphState) {
const { innerGraph } = innerGraphState;
const info = innerGraph.get(symbol);
if (usage === true) {
innerGraph.set(symbol, true);
} else if (info === undefined) {
innerGraph.set(symbol, new Set([usage]));
} else if (info !== true) {
info.add(usage);
}
}
};
/**
* @param {JavascriptParser} parser the parser
* @param {string} name name of variable
* @param {string | TopLevelSymbol | true} usage usage data
* @returns {void}
*/
exports.addVariableUsage = (parser, name, usage) => {
const symbol =
/** @type {TopLevelSymbol} */ (
parser.getTagData(name, topLevelSymbolTag)
) || exports.tagTopLevelSymbol(parser, name);
if (symbol) {
exports.addUsage(parser.state, symbol, usage);
}
};
/**
* @param {ParserState} state parser state
* @returns {void}
*/
exports.inferDependencyUsage = state => {
const innerGraphState = getState(state);
if (!innerGraphState) {
return;
}
const { innerGraph, usageCallbackMap } = innerGraphState;
const processed = new Map();
// flatten graph to terminal nodes (string, undefined or true)
const nonTerminal = new Set(innerGraph.keys());
while (nonTerminal.size > 0) {
for (const key of nonTerminal) {
/** @type {Set<string|TopLevelSymbol> | true} */
let newSet = new Set();
let isTerminal = true;
const value = innerGraph.get(key);
let alreadyProcessed = processed.get(key);
if (alreadyProcessed === undefined) {
alreadyProcessed = new Set();
processed.set(key, alreadyProcessed);
}
if (value !== true && value !== undefined) {
for (const item of value) {
alreadyProcessed.add(item);
}
for (const item of value) {
if (typeof item === "string") {
newSet.add(item);
} else {
const itemValue = innerGraph.get(item);
if (itemValue === true) {
newSet = true;
break;
}
if (itemValue !== undefined) {
for (const i of itemValue) {
if (i === key) continue;
if (alreadyProcessed.has(i)) continue;
newSet.add(i);
if (typeof i !== "string") {
isTerminal = false;
}
}
}
}
}
if (newSet === true) {
innerGraph.set(key, true);
} else if (newSet.size === 0) {
innerGraph.set(key, undefined);
} else {
innerGraph.set(key, newSet);
}
}
if (isTerminal) {
nonTerminal.delete(key);
// For the global key, merge with all other keys
if (key === null) {
const globalValue = innerGraph.get(null);
if (globalValue) {
for (const [key, value] of innerGraph) {
if (key !== null && value !== true) {
if (globalValue === true) {
innerGraph.set(key, true);
} else {
const newSet = new Set(value);
for (const item of globalValue) {
newSet.add(item);
}
innerGraph.set(key, newSet);
}
}
}
}
}
}
}
}
/** @type {Map<Dependency, true | Set<string>>} */
for (const [symbol, callbacks] of usageCallbackMap) {
const usage = /** @type {true | Set<string> | undefined} */ (
innerGraph.get(symbol)
);
for (const callback of callbacks) {
callback(usage === undefined ? false : usage);
}
}
};
/**
* @param {ParserState} state parser state
* @param {UsageCallback} onUsageCallback on usage callback
*/
exports.onUsage = (state, onUsageCallback) => {
const innerGraphState = getState(state);
if (innerGraphState) {
const { usageCallbackMap, currentTopLevelSymbol } = innerGraphState;
if (currentTopLevelSymbol) {
let callbacks = usageCallbackMap.get(currentTopLevelSymbol);
if (callbacks === undefined) {
callbacks = new Set();
usageCallbackMap.set(currentTopLevelSymbol, callbacks);
}
callbacks.add(onUsageCallback);
} else {
onUsageCallback(true);
}
} else {
onUsageCallback(undefined);
}
};
/**
* @param {ParserState} state parser state
* @param {TopLevelSymbol} symbol the symbol
*/
exports.setTopLevelSymbol = (state, symbol) => {
const innerGraphState = getState(state);
if (innerGraphState) {
innerGraphState.currentTopLevelSymbol = symbol;
}
};
/**
* @param {ParserState} state parser state
* @returns {TopLevelSymbol|void} usage data
*/
exports.getTopLevelSymbol = state => {
const innerGraphState = getState(state);
if (innerGraphState) {
return innerGraphState.currentTopLevelSymbol;
}
};
/**
* @param {JavascriptParser} parser parser
* @param {string} name name of variable
* @returns {TopLevelSymbol} symbol
*/
exports.tagTopLevelSymbol = (parser, name) => {
const innerGraphState = getState(parser.state);
if (!innerGraphState) return;
parser.defineVariable(name);
const existingTag = /** @type {TopLevelSymbol} */ (
parser.getTagData(name, topLevelSymbolTag)
);
if (existingTag) {
return existingTag;
}
const fn = new TopLevelSymbol(name);
parser.tagVariable(name, topLevelSymbolTag, fn);
return fn;
};
/**
* @param {Dependency} dependency the dependency
* @param {Set<string> | boolean} usedByExports usedByExports info
* @param {ModuleGraph} moduleGraph moduleGraph
* @param {RuntimeSpec} runtime runtime
* @returns {boolean} false, when unused. Otherwise true
*/
exports.isDependencyUsedByExports = (
dependency,
usedByExports,
moduleGraph,
runtime
) => {
if (usedByExports === false) return false;
if (usedByExports !== true && usedByExports !== undefined) {
const selfModule = moduleGraph.getParentModule(dependency);
const exportsInfo = moduleGraph.getExportsInfo(selfModule);
let used = false;
for (const exportName of usedByExports) {
if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused)
used = true;
}
if (!used) return false;
}
return true;
};
/**
* @param {Dependency} dependency the dependency
* @param {Set<string> | boolean} usedByExports usedByExports info
* @param {ModuleGraph} moduleGraph moduleGraph
* @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
*/
exports.getDependencyUsedByExportsCondition = (
dependency,
usedByExports,
moduleGraph
) => {
if (usedByExports === false) return false;
if (usedByExports !== true && usedByExports !== undefined) {
const selfModule = moduleGraph.getParentModule(dependency);
const exportsInfo = moduleGraph.getExportsInfo(selfModule);
return (connections, runtime) => {
for (const exportName of usedByExports) {
if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused)
return true;
}
return false;
};
}
return null;
};
class TopLevelSymbol {
/**
* @param {string} name name of the variable
*/
constructor(name) {
this.name = name;
}
}
exports.TopLevelSymbol = TopLevelSymbol;
exports.topLevelSymbolTag = topLevelSymbolTag;