From 7dbde208effdfb663c750343044d8a32b813a89c Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 21 May 2015 18:43:55 +0100 Subject: [PATCH] spec.functionName transformer: hasBinding(name) returns `true` for built-in globals, so we attempt to rename the binding but it doesn't exist so exits early, add a check that doesn't perform the renaming and instead uses the wrapper - fixes #1598 --- src/babel/transformation/helpers/name-method.js | 2 +- .../spec.function-name/global/actual.js | 5 +++++ .../spec.function-name/global/expected.js | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/core/fixtures/transformation/spec.function-name/global/actual.js create mode 100644 test/core/fixtures/transformation/spec.function-name/global/expected.js diff --git a/src/babel/transformation/helpers/name-method.js b/src/babel/transformation/helpers/name-method.js index 508f135c5a..97af78f66b 100644 --- a/src/babel/transformation/helpers/name-method.js +++ b/src/babel/transformation/helpers/name-method.js @@ -20,7 +20,7 @@ var visitor = { var wrap = function (state, method, id, scope) { if (state.selfReference) { - if (scope.hasBinding(id.name)) { + if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) { // we can just munge the local binding scope.rename(id.name); } else { diff --git a/test/core/fixtures/transformation/spec.function-name/global/actual.js b/test/core/fixtures/transformation/spec.function-name/global/actual.js new file mode 100644 index 0000000000..abd0e6f67b --- /dev/null +++ b/test/core/fixtures/transformation/spec.function-name/global/actual.js @@ -0,0 +1,5 @@ +var test = { + setInterval: function(fn, ms) { + setInterval(fn, ms); + } +}; diff --git a/test/core/fixtures/transformation/spec.function-name/global/expected.js b/test/core/fixtures/transformation/spec.function-name/global/expected.js new file mode 100644 index 0000000000..fc059a98e6 --- /dev/null +++ b/test/core/fixtures/transformation/spec.function-name/global/expected.js @@ -0,0 +1,17 @@ +"use strict"; + +var test = { + setInterval: (function (_setInterval) { + function setInterval(_x, _x2) { + return _setInterval.apply(this, arguments); + } + + setInterval.toString = function () { + return _setInterval.toString(); + }; + + return setInterval; + })(function (fn, ms) { + setInterval(fn, ms); + }) +};