Merge pull request #3192 from babel/eval-bug
Evaluation may succeed even when not confident
This commit is contained in:
commit
319838bd14
@ -99,11 +99,14 @@ export function evaluate(): { confident: boolean; value: any } {
|
||||
if (expr) str += String(evaluate(expr));
|
||||
}
|
||||
|
||||
if (confident) return str;
|
||||
if (!confident) return;
|
||||
return str;
|
||||
}
|
||||
|
||||
if (path.isConditionalExpression()) {
|
||||
if (evaluate(path.get("test"))) {
|
||||
let testResult = evaluate(path.get("test"));
|
||||
if (!confident) return;
|
||||
if (testResult) {
|
||||
return evaluate(path.get("consequent"));
|
||||
} else {
|
||||
return evaluate(path.get("alternate"));
|
||||
@ -162,6 +165,7 @@ export function evaluate(): { confident: boolean; value: any } {
|
||||
}
|
||||
|
||||
let arg = evaluate(argument);
|
||||
if (!confident) return;
|
||||
switch (node.operator) {
|
||||
case "!": return !arg;
|
||||
case "+": return +arg;
|
||||
@ -218,7 +222,9 @@ export function evaluate(): { confident: boolean; value: any } {
|
||||
|
||||
if (path.isBinaryExpression()) {
|
||||
let left = evaluate(path.get("left"));
|
||||
if (!confident) return;
|
||||
let right = evaluate(path.get("right"));
|
||||
if (!confident) return;
|
||||
|
||||
switch (node.operator) {
|
||||
case "-": return left - right;
|
||||
|
||||
26
packages/babel-traverse/test/evaluation.js
Normal file
26
packages/babel-traverse/test/evaluation.js
Normal file
@ -0,0 +1,26 @@
|
||||
var traverse = require("../lib").default;
|
||||
var assert = require("assert");
|
||||
var parse = require("babylon").parse;
|
||||
|
||||
function getPath(code) {
|
||||
var ast = parse(code);
|
||||
var path;
|
||||
traverse(ast, {
|
||||
Program: function (_path) {
|
||||
path = _path;
|
||||
_path.stop();
|
||||
}
|
||||
});
|
||||
return path;
|
||||
}
|
||||
|
||||
suite("evaluation", function () {
|
||||
suite("evaluateTruthy", function () {
|
||||
test("it should work with null", function () {
|
||||
assert.strictEqual(
|
||||
getPath("false || a.length === 0;").get("body")[0].evaluateTruthy(),
|
||||
undefined
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user