From b4cd2df7454c7cd8d841d141bb8c592c564183af Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 18 May 2015 22:41:27 +0100 Subject: [PATCH] ignore this and arguments when performing TCO on shadowed functions - fixes #1564 --- .../transformation/transformers/es6/tail-call.js | 13 ++++++++----- .../transformation/es6.tail-call/shadow/actual.js | 8 ++++++++ .../transformation/es6.tail-call/shadow/expected.js | 13 +++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 test/core/fixtures/transformation/es6.tail-call/shadow/actual.js create mode 100644 test/core/fixtures/transformation/es6.tail-call/shadow/expected.js diff --git a/src/babel/transformation/transformers/es6/tail-call.js b/src/babel/transformation/transformers/es6/tail-call.js index 00bce9ffff..247d408754 100644 --- a/src/babel/transformation/transformers/es6/tail-call.js +++ b/src/babel/transformation/transformers/es6/tail-call.js @@ -44,12 +44,14 @@ var visitor = { }, ThisExpression(node, parent, scope, state) { - state.needsThis = true; - state.thisPaths.push(this); + if (!state.isShadowed) { + state.needsThis = true; + state.thisPaths.push(this); + } }, ReferencedIdentifier(node, parent, scope, state) { - if (node.name === "arguments") { + if (node.name === "arguments" && !state.isShadowed) { state.needsArguments = true; state.argumentsPaths.push(this); } @@ -67,8 +69,9 @@ class TailCallTransformer { this.needsThis = false; this.thisPaths = []; - this.ownerId = path.node.id; - this.vars = []; + this.isShadowed = path.isArrowFunctionExpression() || path.is("shadow"); + this.ownerId = path.node.id; + this.vars = []; this.scope = scope; this.path = path; diff --git a/test/core/fixtures/transformation/es6.tail-call/shadow/actual.js b/test/core/fixtures/transformation/es6.tail-call/shadow/actual.js new file mode 100644 index 0000000000..2fa228077d --- /dev/null +++ b/test/core/fixtures/transformation/es6.tail-call/shadow/actual.js @@ -0,0 +1,8 @@ +(function(){ + var foo = () => { + this; + arguments; + foo(); + }; + foo(); +}); diff --git a/test/core/fixtures/transformation/es6.tail-call/shadow/expected.js b/test/core/fixtures/transformation/es6.tail-call/shadow/expected.js new file mode 100644 index 0000000000..82f39882d9 --- /dev/null +++ b/test/core/fixtures/transformation/es6.tail-call/shadow/expected.js @@ -0,0 +1,13 @@ +"use strict"; + +(function () { + var _this = this, + _arguments = arguments; + + var foo = function foo() { + _this; + _arguments; + foo(); + }; + foo(); +});