From 036e90e9cc35cdfe6ed69af2a65d629bd084ca97 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Sun, 6 Dec 2015 07:56:17 -0800 Subject: [PATCH 1/2] Fix bug with evaluating an expression in it's own binding --- .../multiple-definition-evaluation/exec.js | 18 ++++++++++++++++++ .../babel-traverse/src/path/introspection.js | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 packages/babel-core/test/fixtures/plugins/multiple-definition-evaluation/exec.js diff --git a/packages/babel-core/test/fixtures/plugins/multiple-definition-evaluation/exec.js b/packages/babel-core/test/fixtures/plugins/multiple-definition-evaluation/exec.js new file mode 100644 index 0000000000..de27da3c0b --- /dev/null +++ b/packages/babel-core/test/fixtures/plugins/multiple-definition-evaluation/exec.js @@ -0,0 +1,18 @@ +var code = multiline([ + "function foo() {", + " var a = a ? a : a;", + "}", +]); + +transform(code, { + plugins: function (b) { + var t = b.types; + return { + visitor: { + ConditionalExpression: function(path) { + path.get("test").evaluateTruthy(); + } + } + } + } +}); diff --git a/packages/babel-traverse/src/path/introspection.js b/packages/babel-traverse/src/path/introspection.js index 8760cfc5dd..61764a3bba 100644 --- a/packages/babel-traverse/src/path/introspection.js +++ b/packages/babel-traverse/src/path/introspection.js @@ -360,7 +360,9 @@ export function _resolve(dangerous?, resolved?): ?NodePath { if (binding.kind === "module") return; if (binding.path !== this) { - return binding.path.resolve(dangerous, resolved); + let ret = binding.path.resolve(dangerous, resolved); + if (this.find(parent => parent.node === ret.node)) return; + return ret; } } else if (this.isTypeCastExpression()) { return this.get("expression").resolve(dangerous, resolved); From 350ee82a83a831c441da92ea9c54dc14fff46ab3 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Sun, 6 Dec 2015 08:00:15 -0800 Subject: [PATCH 2/2] add comment --- packages/babel-traverse/src/path/introspection.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-traverse/src/path/introspection.js b/packages/babel-traverse/src/path/introspection.js index 61764a3bba..8b70dd1d54 100644 --- a/packages/babel-traverse/src/path/introspection.js +++ b/packages/babel-traverse/src/path/introspection.js @@ -361,6 +361,7 @@ export function _resolve(dangerous?, resolved?): ?NodePath { if (binding.path !== this) { let ret = binding.path.resolve(dangerous, resolved); + // If the identifier resolves to parent node then we can't really resolve it. if (this.find(parent => parent.node === ret.node)) return; return ret; }