From a096f6b1c539c793c9a62a91703c531a09bc5b87 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 24 Jun 2015 13:45:14 +0100 Subject: [PATCH] fix noOptimise state being incorrect when recursing into multiple nested functions - fixes #1815 --- .../transformers/es6/parameters.rest.js | 11 +++++------ .../actual.js | 9 +++++++++ .../expected.js | 13 +++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/babel/transformation/transformers/es6/parameters.rest.js b/src/babel/transformation/transformers/es6/parameters.rest.js index 2c337f2c61..1ccb6572c3 100644 --- a/src/babel/transformation/transformers/es6/parameters.rest.js +++ b/src/babel/transformation/transformers/es6/parameters.rest.js @@ -12,9 +12,10 @@ var memberExpressionOptimisationVisitor = { Function(node, parent, scope, state) { // skip over functions as whatever `arguments` we reference inside will refer // to the wrong function + var oldNoOptimise = state.noOptimise; state.noOptimise = true; this.traverse(memberExpressionOptimisationVisitor, state); - state.noOptimise = false; + state.noOptimise = oldNoOptimise; this.skip(); }, @@ -27,7 +28,9 @@ var memberExpressionOptimisationVisitor = { // is this a referenced identifier and is it referencing the rest parameter? if (node.name !== state.name) return; - if (!state.noOptimise) { + if (state.noOptimise) { + state.deopted = true; + } else { if (this.parentPath.isMemberExpression({ computed: true, object: node })) { // if we know that this member expression is referencing a number then we can safely // optimise it @@ -46,11 +49,7 @@ var memberExpressionOptimisationVisitor = { return; } } - } - if (state.noOptimise) { - state.deopted = true; - } else { state.references.push(this); } } diff --git a/test/core/fixtures/transformation/es6.parameters.rest/deepest-common-ancestor-earliest-child/actual.js b/test/core/fixtures/transformation/es6.parameters.rest/deepest-common-ancestor-earliest-child/actual.js index 44301799b2..9b3b52c1f7 100644 --- a/test/core/fixtures/transformation/es6.parameters.rest/deepest-common-ancestor-earliest-child/actual.js +++ b/test/core/fixtures/transformation/es6.parameters.rest/deepest-common-ancestor-earliest-child/actual.js @@ -41,3 +41,12 @@ function r(...rest){ if (lol) rest; rest; } + +// nested functions +function a(...args) { + return function() { + function b() {} + + console.log("Shouldn't args be from a's scope?", args); + }; +} diff --git a/test/core/fixtures/transformation/es6.parameters.rest/deepest-common-ancestor-earliest-child/expected.js b/test/core/fixtures/transformation/es6.parameters.rest/deepest-common-ancestor-earliest-child/expected.js index 189f13dfea..0af3b93483 100644 --- a/test/core/fixtures/transformation/es6.parameters.rest/deepest-common-ancestor-earliest-child/expected.js +++ b/test/core/fixtures/transformation/es6.parameters.rest/deepest-common-ancestor-earliest-child/expected.js @@ -64,3 +64,16 @@ function r() { if (lol) rest; rest; } + +// nested functions +function a() { + for (var _len6 = arguments.length, args = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) { + args[_key6] = arguments[_key6]; + } + + return function () { + function b() {} + + console.log("Shouldn't args be from a's scope?", args); + }; +}