Skip to content

Commit

Permalink
Fix path.evaluate for references before declarations (#4875)
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi authored and hzoo committed Nov 21, 2016
1 parent 3a27f49 commit 7e02027
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/babel-traverse/src/path/evaluation.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ export function evaluate(): { confident: boolean; value: any } {
return deopt(binding.path);
}

if (binding && path.node.start < binding.path.node.end) {
return deopt(binding.path);
}

if (binding && binding.hasValue) {
return binding.value;
} else {
Expand Down
12 changes: 12 additions & 0 deletions packages/babel-traverse/test/evaluation.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,16 @@ describe("evaluation", function () {
false
);
});

it("should deopt ids that are referenced before the bindings", function () {
assert.strictEqual(
getPath("let x = y + 5; let y = 5;").get("body.0.declarations.0.init").evaluate().confident,
false
);
assert.strictEqual(
getPath("if (typeof x === 'undefined') var x = {}")
.get("body.0.test").evaluate().confident,
false
);
});
});

0 comments on commit 7e02027

Please sign in to comment.