File size: 12,250 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeAllScopeAndVariableAndReference = removeAllScopeAndVariableAndReference;
exports.getScopeFromNode = getScopeFromNode;
exports.findVariable = findVariable;
exports.getProgramScope = getProgramScope;
exports.removeIdentifierVariable = removeIdentifierVariable;
exports.getAllReferences = getAllReferences;
exports.removeIdentifierReference = removeIdentifierReference;
exports.removeReference = removeReference;
exports.removeScope = removeScope;
exports.replaceScope = replaceScope;
exports.addVariable = addVariable;
exports.addReference = addReference;
exports.addAllReferences = addAllReferences;
exports.simplifyScope = simplifyScope;
const traverse_1 = require("../traverse");
const utils_1 = require("../utils");
/** Remove all scope, variable, and reference */
function removeAllScopeAndVariableAndReference(target, info) {
    const targetScopes = new Set();
    (0, traverse_1.traverseNodes)(target, {
        visitorKeys: info.visitorKeys,
        enterNode(node) {
            const scope = info.scopeManager.acquire(node);
            if (scope) {
                targetScopes.add(scope);
                return;
            }
            if (node.type === "Identifier") {
                let scope = getScopeFromNode(info.scopeManager, node);
                while (scope &&
                    scope.block.type !== "Program" &&
                    target.range[0] <= scope.block.range[0] &&
                    scope.block.range[1] <= target.range[1]) {
                    scope = scope.upper;
                }
                if (targetScopes.has(scope)) {
                    return;
                }
                removeIdentifierVariable(node, scope);
                removeIdentifierReference(node, scope);
            }
        },
        leaveNode() {
            // noop
        },
    });
    for (const scope of targetScopes) {
        removeScope(info.scopeManager, scope);
    }
}
/**
 * Gets the scope for the current node
 */
function getScopeFromNode(scopeManager, currentNode) {
    let node = currentNode;
    for (; node; node = node.parent || null) {
        const scope = scopeManager.acquire(node, false);
        if (scope) {
            if (scope.type === "function-expression-name") {
                return scope.childScopes[0];
            }
            if (scope.type === "global" &&
                node.type === "Program" &&
                node.sourceType === "module") {
                return scope.childScopes.find((s) => s.type === "module") || scope;
            }
            return scope;
        }
    }
    const global = scopeManager.globalScope;
    return global;
}
/**
 * Find the variable of a given identifier.
 */
function findVariable(scopeManager, node) {
    let scope = getScopeFromNode(scopeManager, node);
    while (scope != null) {
        const variable = scope.set.get(node.name);
        if (variable != null) {
            return variable;
        }
        scope = scope.upper;
    }
    return null;
}
/**
 * Gets the scope for the Program node
 */
function getProgramScope(scopeManager) {
    const globalScope = scopeManager.globalScope;
    return (globalScope.childScopes.find((s) => s.type === "module") || globalScope);
}
/** Remove variable */
function removeIdentifierVariable(node, scope) {
    if (node.type === "ObjectPattern") {
        for (const prop of node.properties) {
            if (prop.type === "Property") {
                removeIdentifierVariable(prop.value, scope);
            }
            else if (prop.type === "RestElement") {
                removeIdentifierVariable(prop, scope);
            }
        }
        return;
    }
    if (node.type === "ArrayPattern") {
        for (const element of node.elements) {
            if (!element)
                continue;
            removeIdentifierVariable(element, scope);
        }
        return;
    }
    if (node.type === "AssignmentPattern") {
        removeIdentifierVariable(node.left, scope);
        return;
    }
    if (node.type === "RestElement") {
        removeIdentifierVariable(node.argument, scope);
        return;
    }
    if (node.type === "MemberExpression") {
        return;
    }
    if (node.type !== "Identifier") {
        return;
    }
    for (let varIndex = 0; varIndex < scope.variables.length; varIndex++) {
        const variable = scope.variables[varIndex];
        const defIndex = variable.defs.findIndex((def) => def.name === node);
        if (defIndex < 0) {
            continue;
        }
        variable.defs.splice(defIndex, 1);
        if (variable.defs.length === 0) {
            // Remove variable
            referencesToThrough(variable.references, scope);
            variable.references.forEach((r) => {
                if (r.init)
                    r.init = false;
                r.resolved = null;
            });
            scope.variables.splice(varIndex, 1);
            const name = node.name;
            if (variable === scope.set.get(name)) {
                scope.set.delete(name);
            }
        }
        else {
            const idIndex = variable.identifiers.indexOf(node);
            if (idIndex >= 0) {
                variable.identifiers.splice(idIndex, 1);
            }
        }
        return;
    }
}
/** Get all references */
function* getAllReferences(node, scope) {
    if (node.type === "ObjectPattern") {
        for (const prop of node.properties) {
            if (prop.type === "Property") {
                yield* getAllReferences(prop.value, scope);
            }
            else if (prop.type === "RestElement") {
                yield* getAllReferences(prop, scope);
            }
        }
        return;
    }
    if (node.type === "ArrayPattern") {
        for (const element of node.elements) {
            if (!element)
                continue;
            yield* getAllReferences(element, scope);
        }
        return;
    }
    if (node.type === "AssignmentPattern") {
        yield* getAllReferences(node.left, scope);
        return;
    }
    if (node.type === "RestElement") {
        yield* getAllReferences(node.argument, scope);
        return;
    }
    if (node.type === "MemberExpression") {
        return;
    }
    if (node.type !== "Identifier") {
        return;
    }
    const ref = scope.references.find((ref) => ref.identifier === node);
    if (ref)
        yield ref;
}
/** Remove reference */
function removeIdentifierReference(node, scope) {
    const reference = scope.references.find((ref) => ref.identifier === node);
    if (reference) {
        removeReference(reference, scope);
        return true;
    }
    const location = node.range[0];
    const pendingScopes = [];
    for (const childScope of scope.childScopes) {
        const range = childScope.block.range;
        if (range[0] <= location && location < range[1]) {
            if (removeIdentifierReference(node, childScope)) {
                return true;
            }
        }
        else {
            pendingScopes.push(childScope);
        }
    }
    for (const childScope of pendingScopes) {
        if (removeIdentifierReference(node, childScope)) {
            return true;
        }
    }
    return false;
}
/** Remove reference */
function removeReference(reference, baseScope) {
    if (reference.resolved) {
        if (reference.resolved.defs.some((d) => d.name === reference.identifier)) {
            // remove var
            const varIndex = baseScope.variables.indexOf(reference.resolved);
            if (varIndex >= 0) {
                baseScope.variables.splice(varIndex, 1);
            }
            const name = reference.identifier.name;
            if (reference.resolved === baseScope.set.get(name)) {
                baseScope.set.delete(name);
            }
        }
        else {
            const refIndex = reference.resolved.references.indexOf(reference);
            if (refIndex >= 0) {
                reference.resolved.references.splice(refIndex, 1);
            }
        }
    }
    let scope = baseScope;
    while (scope) {
        const refIndex = scope.references.indexOf(reference);
        if (refIndex >= 0) {
            scope.references.splice(refIndex, 1);
        }
        const throughIndex = scope.through.indexOf(reference);
        if (throughIndex >= 0) {
            scope.through.splice(throughIndex, 1);
        }
        scope = scope.upper;
    }
}
/** Move reference to through */
function referencesToThrough(references, baseScope) {
    let scope = baseScope;
    while (scope) {
        addAllReferences(scope.through, references);
        scope = scope.upper;
    }
}
/** Remove scope */
function removeScope(scopeManager, scope) {
    while (scope.childScopes[0]) {
        removeScope(scopeManager, scope.childScopes[0]);
    }
    while (scope.references[0]) {
        removeReference(scope.references[0], scope);
    }
    const upper = scope.upper;
    if (upper) {
        const index = upper.childScopes.indexOf(scope);
        if (index >= 0) {
            upper.childScopes.splice(index, 1);
        }
    }
    const index = scopeManager.scopes.indexOf(scope);
    if (index >= 0) {
        scopeManager.scopes.splice(index, 1);
    }
}
/** Replace scope */
function replaceScope(scopeManager, scope, newChildScopes = []) {
    // remove scope from scopeManager
    scopeManager.scopes = scopeManager.scopes.filter((s) => s !== scope);
    const upper = scope.upper;
    if (upper) {
        // remove scope from upper and marge childScopes
        upper.childScopes.splice(upper.childScopes.indexOf(scope), 1, ...newChildScopes);
        for (const child of newChildScopes) {
            child.upper = upper;
            replaceVariableScope(child, scope);
        }
    }
    /** Replace variableScope  */
    function replaceVariableScope(child, replaceTarget) {
        if (child.variableScope === replaceTarget) {
            child.variableScope = child.upper.variableScope;
            for (const c of child.childScopes) {
                replaceVariableScope(c, replaceTarget);
            }
        }
    }
}
/**
 * Add variable to array
 */
function addVariable(list, variable) {
    (0, utils_1.addElementToSortedArray)(list, variable, (a, b) => {
        const idA = getFirstId(a);
        const idB = getFirstId(b);
        return idA.range[0] - idB.range[0];
    });
    /** Get first id from give variable */
    function getFirstId(v) {
        var _a, _b;
        return v.identifiers[0] || ((_a = v.defs[0]) === null || _a === void 0 ? void 0 : _a.name) || ((_b = v.references[0]) === null || _b === void 0 ? void 0 : _b.identifier);
    }
}
/**
 * Add reference to array
 */
function addReference(list, reference) {
    (0, utils_1.addElementToSortedArray)(list, reference, (a, b) => a.identifier.range[0] - b.identifier.range[0]);
}
/**
 * Add all references to array
 */
function addAllReferences(list, elements) {
    (0, utils_1.addElementsToSortedArray)(list, elements, (a, b) => a.identifier.range[0] - b.identifier.range[0]);
}
/**
 * Simplify scope data.
 * @deprecated For Debug
 */
function simplifyScope(scope) {
    return {
        type: scope.type,
        childScopes: scope.childScopes.map(simplifyScope),
        block: {
            type: scope.block.type,
            loc: JSON.stringify(scope.block.loc),
        },
        variables: scope.type === "global" ? null : simplifyVariables(scope.variables),
        references: scope.references.map(simplifyReference),
        through: scope.through.map(simplifyReference),
        get original() {
            return scope;
        },
    };
}
/**
 * Simplify variables data.
 * @deprecated For Debug
 */
function simplifyVariables(variables) {
    return Object.fromEntries(variables.map((v) => {
        var _a;
        return [
            v.name,
            {
                loc: JSON.stringify((_a = v.defs[0]) === null || _a === void 0 ? void 0 : _a.node.loc),
            },
        ];
    }));
}
/**
 * Simplify reference data.
 * @deprecated For Debug
 */
function simplifyReference(reference) {
    return {
        name: reference.identifier.name,
        loc: JSON.stringify(reference.identifier.loc),
    };
}