Fix annex B block function hoisting semantics (#12512)

This commit is contained in:
Nicolò Ribaudo
2020-12-16 18:42:00 +01:00
committed by GitHub
parent 70fa1b3d49
commit bfb51362c7
13 changed files with 158 additions and 1 deletions

View File

@@ -338,6 +338,18 @@ const loopVisitor = {
},
};
function isStrict(path) {
return !!path.find(({ node }) => {
if (t.isProgram(node)) {
if (node.sourceType === "module") return true;
} else if (!t.isBlockStatement(node)) return false;
return node.directives.some(
directive => directive.value.value === "use strict",
);
});
}
class BlockScoping {
constructor(
loopPath?: NodePath,
@@ -486,7 +498,10 @@ class BlockScoping {
const parentBinding = scope.parent.getOwnBinding(key);
if (
binding.kind === "hoisted" &&
(!parentBinding || isVar(parentBinding.path.parent))
!binding.path.node.async &&
!binding.path.node.generator &&
(!parentBinding || isVar(parentBinding.path.parent)) &&
!isStrict(binding.path.parentPath)
) {
continue;
}