-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathnormalizeRedundantNotOperator.js
34 lines (31 loc) · 1.15 KB
/
normalizeRedundantNotOperator.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
import {badValue} from '../config.js';
import {Sandbox} from '../utils/sandbox.js';
import {evalInVm} from '../utils/evalInVm.js';
import {canUnaryExpressionBeResolved} from '../utils/canUnaryExpressionBeResolved.js';
const relevantNodeTypes = ['Literal', 'ArrayExpression', 'ObjectExpression', 'UnaryExpression'];
/**
* Replace redundant not operators with actual value (e.g. !true -> false)
* @param {Arborist} arb
* @param {Function} candidateFilter (optional) a filter to apply on the candidates list
* @return {Arborist}
*/
function normalizeRedundantNotOperator(arb, candidateFilter = () => true) {
let sharedSB;
const relevantNodes = [
...(arb.ast[0].typeMap.UnaryExpression || []),
];
for (let i = 0; i < relevantNodes.length; i++) {
const n = relevantNodes[i];
if (n.operator === '!' &&
relevantNodeTypes.includes(n.argument.type) &&
candidateFilter(n)) {
if (canUnaryExpressionBeResolved(n.argument)) {
sharedSB = sharedSB || new Sandbox();
const replacementNode = evalInVm(n.src, sharedSB);
if (replacementNode !== badValue) arb.markNode(n, replacementNode);
}
}
}
return arb;
}
export default normalizeRedundantNotOperator;