diff --git a/lib/instrument/visitors/function.js b/lib/instrument/visitors/function.js index 67fcb644..7186aedb 100644 --- a/lib/instrument/visitors/function.js +++ b/lib/instrument/visitors/function.js @@ -86,11 +86,12 @@ function FunctionDeclaration(node, state, parent, key) { // Existing binding must be a `var` declaration or another function declaration binding.isFunction = true; } else { - binding = createBinding(block, fnName, {isVar: true, isFunction: true}, state); + const isTopLevel = block === hoistBlock; + binding = createBinding(block, fnName, {isVar: isTopLevel, isFunction: true}, state); // If is a sloppy-mode function declaration which may need to be hoisted, record that. // Determine whether to hoist after 1st pass is complete, once all other bindings created. - if (block !== hoistBlock && !state.isStrict && !node.async && !node.generator) { + if (!isTopLevel && !state.isStrict && !node.async && !node.generator) { state.sloppyFunctionDeclarations.push({varName: fnName, binding, block, hoistBlock}); } } @@ -564,7 +565,7 @@ function hoistSloppyFunctionDeclaration(declaration) { if (hoistBlockBinding) { hoistBlockBinding.isFunction = true; } else { - hoistBlock.bindings[varName] = declaration.binding; + hoistBlock.bindings[varName] = {...declaration.binding, isVar: true}; } }