Fix bug where path.evaluate treats repeated identifiers as undefined (#3557)

* babel-traverse: add test to demonstrate repeated identifiers being evaluated to `false`

* fix typo: `value` -> `val`

* path.evaluate: only mark item as resolved if we're confident
This commit is contained in:
Erik Desjardins 2016-08-04 16:58:41 -04:00 committed by Henry Zhu
parent d0c82f0d8a
commit aeb42c2228
2 changed files with 18 additions and 2 deletions

View File

@ -90,8 +90,10 @@ export function evaluate(): { confident: boolean; value: any } {
seen.set(node, item);
let val = _evaluate(path);
item.resolved = true;
item.value = value;
if (confident) {
item.resolved = true;
item.value = val;
}
return val;
}
}

View File

@ -37,4 +37,18 @@ suite("evaluation", function () {
false
);
});
test("should work with repeated, indeterminate identifiers", function () {
assert.strictEqual(
getPath("var num = foo(); (num > 0 && num < 100);").get("body")[1].evaluateTruthy(),
undefined
);
});
test("should work with repeated, determinate identifiers", function () {
assert.strictEqual(
getPath("var num = 5; (num > 0 && num < 100);").get("body")[1].evaluateTruthy(),
true
);
});
});