-
Notifications
You must be signed in to change notification settings - Fork 84
/
equality_rewriter.js
39 lines (32 loc) · 1.19 KB
/
equality_rewriter.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
35
36
37
38
39
const escodegen = require("escodegen");
// Rewrite "a == b" to '((a) == "CURRENT_SCRIPT_IN_FAKED_DIR.js") ||
// ((b) == "CURRENT_SCRIPT_IN_FAKED_DIR.js") ? true : a == b'. This
// makes checks against the faked up script file name always evaluate
// to true.
function rewriteScriptCheck(key, val) {
if (!val) return;
// Binary expression?
if (val.type != "BinaryExpression") return;
// == check?
if (val.operator != "==") return;
// Got a == expression. Pull out the left and right hand
// expressions.
const lhs = val.left;
const rhs = val.right;
// Don't rewrite this if it is already checking for the fake
// box-js script name.
const lhsCode = escodegen.generate(lhs);
const rhsCode = escodegen.generate(rhs);
if ((lhsCode == "'CURRENT_SCRIPT_IN_FAKED_DIR.js'") ||
(rhsCode == "'CURRENT_SCRIPT_IN_FAKED_DIR.js'")) return
//console.log("----");
//console.log(JSON.stringify(val, null, 2));
r = require("./patches/equality_op.js")(lhs, rhs);
//console.log("REWRITE EQUALITY!!");
//console.log(JSON.stringify(r, null, 2));
//console.log(escodegen.generate(r));
return r;
}
module.exports = {
rewriteScriptCheck,
};