Skip to content

Commit

Permalink
Review by JLHwung
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Dec 7, 2022
1 parent 8e38136 commit 1073242
Show file tree
Hide file tree
Showing 32 changed files with 108 additions and 27 deletions.
11 changes: 7 additions & 4 deletions packages/babel-plugin-transform-block-scoping/src/annex-B_3_3.ts
Expand Up @@ -51,6 +51,8 @@ export const annexB33FunctionsVisitor: Visitor = {
function transformStatementList(paths: NodePath<t.Statement>[]) {
outer: for (const path of paths) {
if (!path.isFunctionDeclaration()) continue;
// Annex B.3.3 only applies to plain functions.
if (path.node.async || path.node.generator) return;

const { scope } = path.parentPath;
if (isVarScope(scope)) return;
Expand All @@ -74,9 +76,6 @@ function maybeTransformBlockScopedFunction(
parentPath: { scope },
} = path;

// Annex B.3.3 only applies to plain functions.
if (node.async || node.generator) return;

const { id } = node;
scope.removeOwnBinding(id.name);
node.id = null;
Expand Down Expand Up @@ -115,7 +114,11 @@ function isStrict(path: NodePath) {
return !!path.find(({ node }) => {
if (t.isProgram(node)) {
if (node.sourceType === "module") return true;
} else if (!t.isBlockStatement(node)) return false;
} else if (t.isClass(node)) {
return true;
} else if (!t.isBlockStatement(node)) {
return false;
}

return node.directives?.some(
directive => directive.value.value === "use strict",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-transform-block-scoping/src/index.ts
Expand Up @@ -93,7 +93,7 @@ export default declare((api, opts: Options) => {

if (headPath?.isVariableDeclaration<t.Node>()) {
// If we wrap the loop body, we transform the var
// declaration in the loop head now, to avoid having
// declaration in the loop head now, to avoid
// invalid references that break other plugins:
//
// for (let head of x) {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-transform-block-scoping/src/loop.ts
Expand Up @@ -75,7 +75,7 @@ function relativeLoopLocation(path: NodePath, loopPath: NodePath<t.Loop>) {
let inClosure = false;

for (let currPath = path; currPath; currPath = currPath.parentPath) {
if (currPath.isFunction()) inClosure = true;
if (currPath.isFunction() || currPath.isClass()) inClosure = true;
if (currPath === bodyPath) {
return { inBody: true, inClosure };
} else if (currPath === loopPath) {
Expand Down
@@ -0,0 +1,17 @@
class A {
m() {
if (true) {
function f() {
return true;
}
}

function g() {
return f();
}

return g();
}
}

expect(new A().m).toThrow(ReferenceError);
@@ -0,0 +1,17 @@
class A {
m() {
if (true) {
function f() {
return true;
}
}

function g() {
return f();
}

return g();
}
}

expect(new A().m).toThrow(ReferenceError);
@@ -0,0 +1,14 @@
class A {
m() {
if (true) {
var _f = function () {
return true;
};
}
function g() {
return f();
}
return g();
}
}
expect(new A().m).toThrow(ReferenceError);
@@ -0,0 +1,11 @@
const m = module {
if (true) {
function f() {
return true;
}
}

function g() {
return f();
}
};
@@ -0,0 +1,7 @@
{
"plugins": [
"transform-block-scoping",
"transform-block-scoped-functions",
"syntax-module-blocks"
]
}
@@ -0,0 +1,10 @@
var m = module {
if (true) {
var _f = function () {
return true;
};
}
function g() {
return f();
}
};

This file was deleted.

This file was deleted.

@@ -0,0 +1,10 @@
let run = [];

for (let i = 0; i < 2; i++) {
run.push(class C {
x = i;
});
}

expect(new run[0]().x).toBe(0);
expect(new run[1]().x).toBe(1);
@@ -0,0 +1,5 @@
for (let i = 0; i < 2; i++) {
class C {
x = i;
}
}
@@ -0,0 +1,8 @@
var _loop = function (i) {
class C {
x = i;
}
};
for (var i = 0; i < 2; i++) {
_loop(i);
}

0 comments on commit 1073242

Please sign in to comment.