| 1 |
import { parse } from "@babel/parser"; |
| 2 |
|
| 3 |
import _traverse from "../lib/index.js"; |
| 4 |
const traverse = _traverse.default || _traverse; |
| 5 |
|
| 6 |
function getPath(code) { |
| 7 |
const ast = parse(code); |
| 8 |
let path; |
| 9 |
traverse(ast, { |
| 10 |
Program: function (_path) { |
| 11 |
path = _path; |
| 12 |
_path.stop(); |
| 13 |
}, |
| 14 |
}); |
| 15 |
return path; |
| 16 |
} |
| 17 |
|
| 18 |
function addDeoptTest(code, type, expectedType) { |
| 19 |
it(type + " deopt: " + code, function () { |
| 20 |
const visitor = {}; |
| 21 |
|
| 22 |
visitor[type] = function (path) { |
| 23 |
const evaluate = path.evaluate(); |
| 24 |
expect(evaluate.confident).toBeFalsy(); |
| 25 |
expect(evaluate.deopt.type).toEqual(expectedType); |
| 26 |
}; |
| 27 |
|
| 28 |
traverse( |
| 29 |
parse(code, { |
| 30 |
plugins: ["*"], |
| 31 |
}), |
| 32 |
visitor, |
| 33 |
); |
| 34 |
}); |
| 35 |
} |
| 36 |
|
| 37 |
describe("evaluation", function () { |
| 38 |
describe("evaluateTruthy", function () { |
| 39 |
it("should work with null", function () { |
| 40 |
expect( |
| 41 |
getPath("false || a.length === 0;").get("body")[0].evaluateTruthy(), |
| 42 |
).toBeUndefined(); |
| 43 |
}); |
| 44 |
|
| 45 |
it("should not mistake lack of confidence for falsy", function () { |
| 46 |
expect( |
| 47 |
getPath("foo || 'bar'").get("body")[0].evaluate().value, |
| 48 |
).toBeUndefined(); |
| 49 |
}); |
| 50 |
}); |
| 51 |
|
| 52 |
it("should bail out on recursive evaluation", function () { |
| 53 |
expect( |
| 54 |
getPath("function fn(a) { var g = a ? 1 : 2, a = g * this.foo; }") |
| 55 |
.get("body.0.body.body.0.declarations.1.init") |
| 56 |
.evaluate().confident, |
| 57 |
).toBe(false); |
| 58 |
}); |
| 59 |
|
| 60 |
it("should short-circuit && and ||", function () { |
| 61 |
expect(getPath("x === 'y' || 42").get("body")[0].evaluate().confident).toBe( |
| 62 |
false, |
| 63 |
); |
| 64 |
expect(getPath("x === 'y' && 0").get("body")[0].evaluate().confident).toBe( |
| 65 |
false, |
| 66 |
); |
| 67 |
expect(getPath("42 || x === 'y'").get("body")[0].evaluate().value).toBe(42); |
| 68 |
expect(getPath("0 && x === 'y'").get("body")[0].evaluate().value).toBe(0); |
| 69 |
}); |
| 70 |
|
| 71 |
it("should handle ??", function () { |
| 72 |
expect(getPath("null ?? 42").get("body")[0].evaluate().value).toBe(42); |
| 73 |
expect(getPath("void 0 ?? 42").get("body")[0].evaluate().value).toBe(42); |
| 74 |
expect(getPath("0 ?? 42").get("body")[0].evaluate().value).toBe(0); |
| 75 |
expect(getPath("x ?? 42").get("body")[0].evaluate().confident).toBe(false); |
| 76 |
expect(getPath("42 ?? x === 'y'").get("body")[0].evaluate().value).toBe(42); |
| 77 |
}); |
| 78 |
|
| 79 |
it("should work with repeated, indeterminate identifiers", function () { |
| 80 |
expect( |
| 81 |
getPath("var num = foo(); (num > 0 && num < 100);") |
| 82 |
.get("body")[1] |
| 83 |
.evaluateTruthy(), |
| 84 |
).toBeUndefined(); |
| 85 |
}); |
| 86 |
|
| 87 |
it("should work with repeated, determinate identifiers", function () { |
| 88 |
expect( |
| 89 |
getPath("var num = 5; (num > 0 && num < 100);") |
| 90 |
.get("body")[1] |
| 91 |
.evaluateTruthy(), |
| 92 |
).toBe(true); |
| 93 |
}); |
| 94 |
|
| 95 |
it("should deopt when var is redeclared in the same scope", function () { |
| 96 |
expect( |
| 97 |
getPath("var x = 2; var y = x + 2; { var x = 3 }") |
| 98 |
.get("body.1.declarations.0.init") |
| 99 |
.evaluate().confident, |
| 100 |
).toBe(false); |
| 101 |
}); |
| 102 |
|
| 103 |
describe("template literals", function () { |
| 104 |
it("should evaluate template literals", function () { |
| 105 |
expect( |
| 106 |
getPath("var x = 8; var y = 1; var z = `value is ${x >>> y}`") |
| 107 |
.get("body.2.declarations.0.init") |
| 108 |
.evaluate().value, |
| 109 |
).toBe("value is 4"); |
| 110 |
}); |
| 111 |
|
| 112 |
it("shold evaluate String.raw tags", function () { |
| 113 |
expect( |
| 114 |
getPath("String.raw`a\\n${1}\\u`;").get("body.0.expression").evaluate() |
| 115 |
.value, |
| 116 |
).toBe("a\\n1\\u"); |
| 117 |
}); |
| 118 |
|
| 119 |
addDeoptTest( |
| 120 |
"a`x${b}y`", |
| 121 |
"TaggedTemplateExpression", |
| 122 |
"TaggedTemplateExpression", |
| 123 |
); |
| 124 |
}); |
| 125 |
|
| 126 |
it("should evaluate member expressions", function () { |
| 127 |
expect( |
| 128 |
getPath("var x = 'foo'.length") |
| 129 |
.get("body.0.declarations.0.init") |
| 130 |
.evaluate().value, |
| 131 |
).toBe(3); |
| 132 |
expect( |
| 133 |
getPath("var x = 'hello world'[6]") |
| 134 |
.get("body.0.declarations.0.init") |
| 135 |
.evaluate().value, |
| 136 |
).toBe("w"); |
| 137 |
expect( |
| 138 |
getPath("var length = 1; var x = 'abc'[length];") |
| 139 |
.get("body.1.declarations.0.init") |
| 140 |
.evaluate().value, |
| 141 |
).toBe("b"); |
| 142 |
const member_expr = getPath( |
| 143 |
"var x = Math.min(2,Math.max(3,4));var y = Math.random();", |
| 144 |
); |
| 145 |
const eval_member_expr = member_expr |
| 146 |
.get("body.0.declarations.0.init") |
| 147 |
.evaluate(); |
| 148 |
const eval_invalid_call = member_expr |
| 149 |
.get("body.1.declarations.0.init") |
| 150 |
.evaluate(); |
| 151 |
expect(eval_member_expr.value).toBe(2); |
| 152 |
expect(eval_invalid_call.confident).toBe(false); |
| 153 |
}); |
| 154 |
|
| •155 |
if("should not evaluate inherited methods", function () { |
| 156 |
const path = getPath("Math.hasOwnProperty('min')"); |
| 157 |
const evalResult = path.get("body.0.expression").evaluate(); |
| 158 |
expect(evalResult.confident).toBe(false); |
| 159 |
}); |
| 160 |
|
| 161 |
it("should not deopt vars in different scope", function () { |
| 162 |
const input = |
| 163 |
"var a = 5; function x() { var a = 5; var b = a + 1; } var b = a + 2"; |
| 164 |
expect( |
| 165 |
getPath(input).get("body.1.body.body.1.declarations.0.init").evaluate() |
| 166 |
.value, |
| 167 |
).toBe(6); |
| 168 |
expect( |
| 169 |
getPath(input).get("body.2.declarations.0.init").evaluate().value, |
| 170 |
).toBe(7); |
| 171 |
}); |
| 172 |
|
| 173 |
it("should not deopt let/const inside blocks", function () { |
| 174 |
expect( |
| 175 |
getPath("let x = 5; { let x = 1; } let y = x + 5") |
| 176 |
.get("body.2.declarations.0.init") |
| 177 |
.evaluate().value, |
| 178 |
).toBe(10); |
| 179 |
const constExample = |
| 180 |
"const d = true; if (d && true || false) { const d = false; d && 5; }"; |
| 181 |
expect(getPath(constExample).get("body.1.test").evaluate().value).toBe( |
| 182 |
true, |
| 183 |
); |
| 184 |
expect( |
| 185 |
getPath(constExample).get("body.1.consequent.body.1").evaluate().value, |
| 186 |
).toBe(false); |
| 187 |
const test_alternate = "var y = (3 < 4)? 3 + 4: 3 + 4;"; |
| 188 |
expect( |
| 189 |
getPath(test_alternate) |
| 190 |
.get("body.0.declarations.0.init.alternate") |
| 191 |
.evaluate().value, |
| 192 |
).toBe(7); |
| 193 |
}); |
| 194 |
|
| 195 |
it("should deopt ids that are referenced before the bindings", function () { |
| 196 |
expect( |
| 197 |
getPath("let x = y + 5; let y = 5;") |
| 198 |
.get("body.0.declarations.0.init") |
| 199 |
.evaluate().confident, |
| 200 |
).toBe(false); |
| 201 |
expect( |
| 202 |
getPath("if (typeof x === 'undefined') var x = {}") |
| 203 |
.get("body.0.test") |
| 204 |
.evaluate().confident, |
| 205 |
).toBe(false); |
| 206 |
}); |
| 207 |
|
| 208 |
it("should evaluate undefined, NaN and Infinity", () => { |
| 209 |
expect( |
| 210 |
getPath("undefined").get("body.0.expression").evaluate().confident, |
| 211 |
).toBe(true); |
| 212 |
expect(getPath("NaN").get("body.0.expression").evaluate().confident).toBe( |
| 213 |
true, |
| 214 |
); |
| 215 |
expect( |
| 216 |
getPath("Infinity").get("body.0.expression").evaluate().confident, |
| 217 |
).toBe(true); |
| 218 |
}); |
| 219 |
|
| 220 |
it("should deopt redefined primitives - undefined, NaN and Infinity", () => { |
| 221 |
const eval_undef = getPath("let undefined; undefined;") |
| 222 |
.get("body.1.expression") |
| 223 |
.evaluate(); |
| 224 |
const eval_nan = getPath("let NaN; NaN;") |
| 225 |
.get("body.1.expression") |
| 226 |
.evaluate(); |
| 227 |
const eval_inf = getPath("let Infinity; Infinity;") |
| 228 |
.get("body.1.expression") |
| 229 |
.evaluate(); |
| 230 |
expect(eval_undef.confident).toBe(false); |
| 231 |
expect(eval_nan.confident).toBe(false); |
| 232 |
expect(eval_inf.confident).toBe(false); |
| 233 |
|
| 234 |
expect(eval_undef.deopt.type).toBe("VariableDeclarator"); |
| 235 |
expect(eval_undef.deopt.parentPath.node.kind).toBe("let"); |
| 236 |
}); |
| 237 |
|
| 238 |
it("should work with String.raw", function () { |
| 239 |
expect(getPath("String.raw`\\d`").get("body")[0].evaluate().value).toBe( |
| 240 |
"\\d", |
| 241 |
); |
| 242 |
|
| 243 |
expect( |
| 244 |
getPath("`${String.raw`\\d`}`").get("body")[0].evaluate().value, |
| 245 |
).toBe("\\d"); |
| 246 |
}); |
| 247 |
|
| 248 |
it("sets deopt properly when not confident after evaluating multiple expressions", () => { |
| 249 |
const ast = parse(` |
| 250 |
const parts = [foo, bar]; |
| 251 |
console.log(parts.join('-')); |
| 252 |
`); |
| 253 |
|
| 254 |
let result; |
| 255 |
|
| 256 |
traverse(ast, { |
| 257 |
MemberExpression: { |
| 258 |
enter(path) { |
| 259 |
result = path.get("object").evaluate(); |
| 260 |
}, |
| 261 |
}, |
| 262 |
}); |
| 263 |
|
| 264 |
expect(result.confident).toBe(false); |
| 265 |
expect(result.deopt.type).toBe("Identifier"); |
| 266 |
expect(result.deopt.node.name).toBe("foo"); |
| 267 |
}); |
| 268 |
|
| 269 |
it("sets deopt properly when confident after evaluating multiple expressions", () => { |
| 270 |
const ast = parse(` |
| 271 |
const foo = 'foo'; |
| 272 |
const bar = 'bar'; |
| 273 |
const parts = [foo, bar]; |
| 274 |
console.log(parts.join('-')) |
| 275 |
`); |
| 276 |
|
| 277 |
let result; |
| 278 |
|
| 279 |
traverse(ast, { |
| 280 |
MemberExpression: { |
| 281 |
enter(path) { |
| 282 |
result = path.get("object").evaluate(); |
| 283 |
}, |
| 284 |
}, |
| 285 |
}); |
| 286 |
|
| 287 |
expect(result.confident).toBe(true); |
| 288 |
expect(result.deopt).toBeNull(); |
| 289 |
expect(result.value).toEqual(["foo", "bar"]); |
| 290 |
}); |
| 291 |
|
| 292 |
addDeoptTest("({a:{b}})", "ObjectExpression", "Identifier"); |
| 293 |
addDeoptTest("({[a + 'b']: 1})", "ObjectExpression", "Identifier"); |
| 294 |
addDeoptTest("[{a}]", "ArrayExpression", "Identifier"); |
| 295 |
}); |