Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26e4d3a

Browse files
committed
fix
1 parent 76a9178 commit 26e4d3a

16 files changed

Lines changed: 145 additions & 132 deletions

File tree

packages/babel-plugin-transform-block-scoped-functions/src/index.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,21 @@ export default declare(api => {
88
parentPath: NodePath,
99
paths: NodePath<t.Statement>[],
1010
) {
11-
// eslint-disable-next-line no-var
12-
var isInStrictMode = parentPath.isInStrictMode();
11+
const isInStrictMode = parentPath.isInStrictMode();
1312

1413
for (const path of paths) {
1514
if (!path.isFunctionDeclaration()) continue;
1615

17-
if (
18-
!isInStrictMode &&
19-
!(
20-
path.node.async ||
21-
path.node.generator ||
22-
path.getData(
23-
"@babel/plugin-transform-async-generator-functions/async_generator_function",
24-
)
25-
)
26-
) {
27-
continue;
28-
}
16+
const useLet =
17+
isInStrictMode ||
18+
path.node.async ||
19+
path.node.generator ||
20+
path.getData(
21+
"@babel/plugin-transform-async-generator-functions/async_generator_function",
22+
);
2923

3024
const func = path.node;
31-
const declar = t.variableDeclaration("let", [
25+
const declar = t.variableDeclaration(useLet ? "let" : "var", [
3226
t.variableDeclarator(func.id, t.toExpression(func)),
3327
]);
3428

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function strictMode() {
2+
"use strict";
3+
4+
// https://github.com/compat-table/compat-table/blob/f12538fe11c48c4fd35d6cdc7789653e10871b90/data-es6.js#L14429-L14446
5+
6+
console.assert(a() === 1);
7+
function a() {
8+
return 1;
9+
}
10+
console.assert(a() === 1);
11+
{
12+
console.assert(a() === 2);
13+
function a() {
14+
return 2;
15+
}
16+
console.assert(a() === 2);
17+
}
18+
console.assert(a() === 1);
19+
}
20+
function nonStrictMode() {
21+
// https://github.com/compat-table/compat-table/blob/f12538fe11c48c4fd35d6cdc7789653e10871b90/data-es6.js#L4269-L4286
22+
23+
console.assert(typeof a === "undefined");
24+
{
25+
function a() {
26+
return 1;
27+
}
28+
}
29+
console.assert(a() === 1);
30+
31+
console.assert(b() === 2);
32+
{
33+
function b() {
34+
return 1;
35+
}
36+
}
37+
function b() {
38+
return 2;
39+
}
40+
console.assert(b() === 1);
41+
42+
function c() {
43+
return 1;
44+
}
45+
switch (0) {
46+
case 1:
47+
function c() {
48+
return 2;
49+
}
50+
}
51+
console.assert(c() === 1);
52+
}
53+
strictMode();
54+
nonStrictMode();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"plugins": [
3+
"transform-block-scoped-functions",
4+
"transform-block-scoping"
5+
]
6+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function strictMode() {
2+
"use strict";
3+
4+
// https://github.com/compat-table/compat-table/blob/f12538fe11c48c4fd35d6cdc7789653e10871b90/data-es6.js#L14429-L14446
5+
console.assert(a() === 1);
6+
function a() {
7+
return 1;
8+
}
9+
console.assert(a() === 1);
10+
{
11+
var _a = function () {
12+
return 2;
13+
};
14+
console.assert(_a() === 2);
15+
console.assert(_a() === 2);
16+
}
17+
console.assert(a() === 1);
18+
}
19+
function nonStrictMode() {
20+
// https://github.com/compat-table/compat-table/blob/f12538fe11c48c4fd35d6cdc7789653e10871b90/data-es6.js#L4269-L4286
21+
22+
console.assert(typeof a === "undefined");
23+
{
24+
var a = function () {
25+
return 1;
26+
};
27+
}
28+
console.assert(a() === 1);
29+
console.assert(b() === 2);
30+
{
31+
var b = function () {
32+
return 1;
33+
};
34+
}
35+
function b() {
36+
return 2;
37+
}
38+
console.assert(b() === 1);
39+
function c() {
40+
return 1;
41+
}
42+
switch (0) {
43+
case 1:
44+
var c = function () {
45+
return 2;
46+
};
47+
}
48+
console.assert(c() === 1);
49+
}
50+
strictMode();
51+
nonStrictMode();

packages/babel-plugin-transform-block-scoped-functions/test/fixtures/block-scoped-functions/scope-lex-async-generator/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ expect(() => {
2525
var _x2 = function () {
2626
return _x.apply(this, arguments);
2727
};
28-
function _x() {
28+
var _x = function () {
2929
_x = babelHelpers.wrapAsyncGenerator(function* () {});
3030
return _x.apply(this, arguments);
31-
}
31+
};
3232
}
3333
x;
3434
}).toThrow(ReferenceError);

packages/babel-plugin-transform-block-scoping/src/annex-B_3_3.ts

Lines changed: 0 additions & 93 deletions
This file was deleted.

packages/babel-plugin-transform-block-scoping/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
wrapLoopBody,
1010
} from "./loop.ts";
1111
import { validateUsage } from "./validation.ts";
12-
import { annexB33FunctionsVisitor, isVarScope } from "./annex-B_3_3.ts";
1312

1413
export interface Options {
1514
tdz?: boolean;
@@ -31,8 +30,6 @@ export default declare((api, opts: Options) => {
3130
name: "transform-block-scoping",
3231

3332
visitor: traverse.visitors.merge<PluginPass>([
34-
// TODO: Consider adding an option to control Annex B behavior.
35-
annexB33FunctionsVisitor,
3633
{
3734
Loop(path: NodePath<t.Loop>, state) {
3835
const isForStatement = path.isForStatement();
@@ -272,3 +269,7 @@ function isBlockScoped(
272269

273270
return true;
274271
}
272+
273+
export function isVarScope(scope: Scope) {
274+
return scope.path.isFunctionParent() || scope.path.isProgram();
275+
}

packages/babel-plugin-transform-block-scoping/test/fixtures/general/annex-B_3_3-collision-const/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ var run = function () {
22
return false;
33
};
44
if (true) {
5-
function _run() {
5+
var _run = function () {
66
return true;
7-
}
7+
};
88
}
99
function test() {
1010
return run();

packages/babel-plugin-transform-block-scoping/test/fixtures/general/annex-B_3_3-collision-let/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ var run = function () {
22
return false;
33
};
44
if (true) {
5-
function _run() {
5+
var _run = function () {
66
return true;
7-
}
7+
};
88
}
99
function test() {
1010
return run();

packages/babel-plugin-transform-block-scoping/test/fixtures/general/annex-B_3_3/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
if (true) {
2-
function run() {
2+
var run = function () {
33
return true;
4-
}
4+
};
55
}
66
function test() {
77
return run();

0 commit comments

Comments
 (0)