-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathresolveProxyVariables.js
31 lines (29 loc) · 1.03 KB
/
resolveProxyVariables.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
import {areReferencesModified} from '../utils/areReferencesModified.js';
/**
* Replace proxied variables with their intended target.
* E.g.
* const a2b = atob; // This line will be removed
* console.log(a2b('NDI=')); // This will be replaced with `console.log(atob('NDI='));`
* @param {Arborist} arb
* @param {Function} candidateFilter (optional) a filter to apply on the candidates list
* @return {Arborist}
*/
function resolveProxyVariables(arb, candidateFilter = () => true) {
const relevantNodes = [
...(arb.ast[0].typeMap.VariableDeclarator || []),
];
for (let i = 0; i < relevantNodes.length; i++) {
const n = relevantNodes[i];
if (n?.init?.type === 'Identifier' && candidateFilter(n)) {
const refs = n.id.references || [];
// Remove proxy assignments if there are no more references
if (!refs.length) arb.markNode(n);
else if (areReferencesModified(arb.ast, refs)) continue;
else for (const ref of refs) {
arb.markNode(ref, n.init);
}
}
}
return arb;
}
export default resolveProxyVariables;