Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Jul 7, 2023
1 parent 5593918 commit 6448b4f
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
function wrapper(fn) {
return function () {
var _arguments = arguments;
var _ret,
_loop = function () {
var _loop = function () {
var val = fn(..._arguments);
return {
v: val.test(function () {
console.log(val);
})
};
};
},
_ret;
while (someCondition) {
_ret = _loop();
if (_ret) return _ret.v;
Expand Down
5 changes: 1 addition & 4 deletions packages/babel-plugin-transform-block-scoping/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ export default declare((api, opts: Options) => {
transformBlockScopedVariable(headPath, state, tdzEnabled);
}

const declarations = varPath.get("declarations");
declarations[declarations.length - 1]
.get("init")
.unwrapFunctionEnvironment();
varPath.get("declarations.0.init").unwrapFunctionEnvironment();
}
},

Expand Down
5 changes: 3 additions & 2 deletions packages/babel-plugin-transform-block-scoping/src/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export function wrapLoopBody(
}
}
if (varNames.length) {
varPath.unshiftContainer(
varPath.pushContainer(
"declarations",
varNames.map(name => t.variableDeclarator(t.identifier(name))),
);
Expand Down Expand Up @@ -290,7 +290,7 @@ export function wrapLoopBody(
const completionId = loopPath.scope.generateUid("ret");

if (varPath.isVariableDeclaration()) {
varPath.unshiftContainer("declarations", [
varPath.pushContainer("declarations", [
t.variableDeclarator(t.identifier(completionId)),
]);
bodyStmts.push(
Expand Down Expand Up @@ -338,6 +338,7 @@ export function wrapLoopBody(
`,
);
}

if (returnNum) {
for (const path of state.returns) {
const arg = path.node.argument || path.scope.buildUndefinedNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ expect(j).toBe(9);

// it should work with loops nested within switch
j = 0;
var k,
_loop3 = function () {
var _loop3 = function () {
switch (i) {
case 0:
var _loop4 = function () {
Expand All @@ -56,7 +55,8 @@ var k,
(function () {
return z;
});
};
},
k;
for (i = 0; i < 10; i++) {
_loop3();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
function* a() {
var fn,
_loop = function* () {
var _loop = function* () {
var x = yield "iteration";
fn = function () {
return x;
};
};
},
fn;
do {
yield* _loop();
} while (false);
return fn;
}
async function b() {
var fn,
_loop2 = async function () {
var _loop2 = async function () {
var x = await "iteration";
fn = function () {
return x;
};
};
},
fn;
do {
await _loop2();
} while (false);
return fn;
}
async function* c() {
var fn,
_loop3 = async function* () {
var _loop3 = async function* () {
var x = yield "iteration";
fn = function () {
return x;
};
};
},
fn;
do {
yield* _loop3();
} while (false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(function () {
var _ret,
_loop = function (i) {
var _loop = function (i) {
fns.push(function () {
return i;
});
Expand All @@ -13,7 +12,8 @@
v: i
};
}
};
},
_ret;
for (var i in nums) {
_ret = _loop(i);
if (_ret === 0) continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(function () {
var _ret,
_loop = function (i) {
var _loop = function (i) {
fns.push(function () {
return i;
});
return {
v: void 0
};
};
},
_ret;
for (var i in nums) {
_ret = _loop(i);
if (_ret) return _ret.v;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(function () {
var _ret,
_loop = function (i) {
var _loop = function (i) {
fns.push(function () {
return i;
});
return {
v: i
};
};
},
_ret;
for (var i in nums) {
_ret = _loop(i);
if (_ret) return _ret.v;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var x,
f,
_loop = function (i) {
var _loop = function (i) {
x = 5;
({
f
Expand All @@ -10,7 +8,9 @@ var x,
fns.push(function () {
return i * x;
});
};
},
x,
f;
var _iterator = babelHelpers.createForOfIteratorHelper(nums),
_step;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
outer: while (t) {
inner: while (t2) {
var _ret,
_loop = function (i) {
var _loop = function (i) {
later(function () {
return i;
});
Expand All @@ -11,7 +10,8 @@ outer: while (t) {
else if (test4) return 3; // continue inner
else if (test5) return 4; // break
else if (test6) return 5; // continue
};
},
_ret;
for (var i = 0; i < 3; i++) {
_ret = _loop(i);
if (_ret === 0) break outer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
function foo() {
var _ret,
_loop = function () {
var _loop = function () {
switch (2) {
case 0:
{
Expand All @@ -17,7 +16,8 @@ function foo() {
break;
}
}
};
},
_ret;
while (true) {
_ret = _loop();
if (_ret) return _ret.v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ expect(j).toBe(9);

// it should work with loops nested within switch
j = 0;
var k,
_loop3 = function () {
var _loop3 = function () {
switch (i) {
case 0:
var _loop4 = function () {
Expand All @@ -56,7 +55,8 @@ var k,
(function () {
return z;
});
};
},
k;
for (i = 0; i < 10; i++) {
_loop3();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ expect(foo2({
bar: 3
}).next().value).toBe(3);
function foo3() {
var _ret, _loop;
var _loop, _ret;
return babelHelpers.regeneratorRuntime().wrap(function foo3$(_context4) {
while (1) switch (_context4.prev = _context4.next) {
case 0:
Expand Down

0 comments on commit 6448b4f

Please sign in to comment.