diff --git a/lib/instrument/visitors/function.js b/lib/instrument/visitors/function.js index a7d135e8..fa48a0ab 100644 --- a/lib/instrument/visitors/function.js +++ b/lib/instrument/visitors/function.js @@ -566,9 +566,11 @@ function hoistSloppyFunctionDeclarations(state) { function hoistSloppyFunctionDeclaration(declaration) { const {varName, hoistBlock} = declaration; - // Do not hoist if existing const, let or class declaration binding in hoist block + // Do not hoist if existing const, let or class declaration binding in hoist block. + // Also exit if binding exists and already has frozen name. In that case, function may or may not be + // hoisted, but it doesn't matter either way, as only implication of hoisting is to freeze var name. const hoistBlockBinding = hoistBlock.bindings.get(varName); - if (hoistBlockBinding && !hoistBlockBinding.isVar) return; + if (hoistBlockBinding && (!hoistBlockBinding.isVar || hoistBlockBinding.isFrozenName)) return; // If parent function's params include var with same name, do not hoist. // NB: `hoistBlock.parent` here is either parent function params block or file block. @@ -589,7 +591,7 @@ function hoistSloppyFunctionDeclaration(declaration) { // Can be hoisted if (!hoistBlockBinding) { hoistBlock.bindings.set(varName, {...declaration.binding, isVar: true}); - } else if (!hoistBlockBinding.isFrozenName) { + } else { // Existing binding is a `var` declaration. // Flag binding as frozen so `var` declarations' identifiers don't get renamed. hoistBlockBinding.isFrozenName = true;