Skip to content

Commit

Permalink
Instrument: Move check for frozen binding when hoisting sloppy functi…
Browse files Browse the repository at this point in the history
…ons earlier [refactor]
  • Loading branch information
overlookmotel committed Dec 21, 2023
1 parent d3dca21 commit 651fbc0
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/instrument/visitors/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down

0 comments on commit 651fbc0

Please sign in to comment.