Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Oct 24, 2023
1 parent 046b18a commit 7c673d2
Show file tree
Hide file tree
Showing 21 changed files with 81 additions and 48 deletions.
23 changes: 10 additions & 13 deletions packages/babel-plugin-transform-for-of/src/index.ts
Expand Up @@ -18,13 +18,11 @@ function buildLoopBody(
let block;
const bodyPath = path.get("body");
const body = newBody ?? bodyPath.node;
const right = path.node.right;
if (
t.isBlockStatement(body) &&
(Object.keys(path.getBindingIdentifiers()).some(id =>
Object.keys(path.getBindingIdentifiers()).some(id =>
bodyPath.scope.hasOwnBinding(id),
) ||
(t.isIdentifier(right) && bodyPath.scope.hasOwnBinding(right.name)))
)
) {
block = t.blockStatement([declar, body]);
} else {
Expand Down Expand Up @@ -92,15 +90,14 @@ export default declare((api, options: Options) => {
return;
}
const i = scope.generateUidIdentifier("i");
let array: t.Identifier | t.ThisExpression =
scope.maybeGenerateMemoised(right, true);

const inits = [t.variableDeclarator(i, t.numericLiteral(0))];
if (array) {
inits.push(t.variableDeclarator(array, right));
} else {
array = right as t.Identifier | t.ThisExpression;
}
const array: t.Identifier =
scope.maybeGenerateMemoised(right, true) ||
scope.generateUidIdentifier("arr");

const inits = [
t.variableDeclarator(i, t.numericLiteral(0)),
t.variableDeclarator(array, right),
];

const item = t.memberExpression(
t.cloneNode(array),
Expand Down
@@ -1,8 +1,8 @@
define(["foo"], function (_foo) {
"use strict";

for (let _i = 0; _i < _foo.array.length; _i++) {
const elm = _foo.array[_i];
for (let _i = 0, _arr = _foo.array; _i < _arr.length; _i++) {
const elm = _arr[_i];
console.log(elm);
}
});
@@ -1,7 +1,7 @@
"use strict";

var _foo = require("foo");
for (let _i = 0; _i < _foo.array.length; _i++) {
const elm = _foo.array[_i];
for (let _i = 0, _arr = _foo.array; _i < _arr.length; _i++) {
const elm = _arr[_i];
console.log(elm);
}
@@ -1,5 +1,5 @@
import { array } from "foo";
for (let _i = 0; _i < array.length; _i++) {
const elm = array[_i];
for (let _i = 0, _arr = array; _i < _arr.length; _i++) {
const elm = _arr[_i];
console.log(elm);
}
@@ -0,0 +1,3 @@
for (let o of arr) {
arr = null;
}
@@ -0,0 +1,4 @@
for (let _i = 0, _arr = arr; _i < _arr.length; _i++) {
let o = _arr[_i];
arr = null;
}
@@ -1,6 +1,4 @@
for (let _i = 0, _arr = arr; _i < _arr.length; _i++) {
let o = _arr[_i];
{
const arr = o;
}
const arr = o;
}
@@ -1,5 +1,5 @@
const array = [];
for (let _i = 0; _i < array.length; _i++) {
const elm = array[_i];
for (let _i = 0, _arr = array; _i < _arr.length; _i++) {
const elm = _arr[_i];
console.log(elm);
}
@@ -1,6 +1,6 @@
const array = [];
let elm;
for (let _i = 0; _i < array.length; _i++) {
elm = array[_i];
for (let _i = 0, _arr = array; _i < _arr.length; _i++) {
elm = _arr[_i];
console.log(elm);
}
@@ -1,8 +1,8 @@
define(["foo"], function (_foo) {
"use strict";

for (let _i = 0; _i < _foo.array.length; _i++) {
const elm = _foo.array[_i];
for (let _i = 0, _arr = _foo.array; _i < _arr.length; _i++) {
const elm = _arr[_i];
console.log(elm);
}
});
@@ -1,7 +1,7 @@
"use strict";

var _foo = require("foo");
for (let _i = 0; _i < _foo.array.length; _i++) {
const elm = _foo.array[_i];
for (let _i = 0, _arr = _foo.array; _i < _arr.length; _i++) {
const elm = _arr[_i];
console.log(elm);
}
@@ -1,5 +1,5 @@
import { array } from "foo";
for (let _i = 0; _i < array.length; _i++) {
const elm = array[_i];
for (let _i = 0, _arr = array; _i < _arr.length; _i++) {
const elm = _arr[_i];
console.log(elm);
}
@@ -1,6 +1,4 @@
for (let _i = 0, _arr = arr; _i < _arr.length; _i++) {
let o = _arr[_i];
{
const arr = o;
}
const arr = o;
}
@@ -1,5 +1,5 @@
const array = [];
for (let _i = 0; _i < array.length; _i++) {
const elm = array[_i];
for (let _i = 0, _arr = array; _i < _arr.length; _i++) {
const elm = _arr[_i];
console.log(elm);
}
@@ -1,6 +1,6 @@
const array = [];
let elm;
for (let _i = 0; _i < array.length; _i++) {
elm = array[_i];
for (let _i = 0, _arr = array; _i < _arr.length; _i++) {
elm = _arr[_i];
console.log(elm);
}
@@ -1,8 +1,6 @@
function f(...t) {
for (var _i = 0, _t = t; _i < _t.length; _i++) {
let o = _t[_i];
{
const t = o;
}
const t = o;
}
}
@@ -0,0 +1,10 @@
function x(e){
const r = [];
for (const s of e) {
e = null;
r.push(s);
}
return r;
}

expect(x([1, 2, 3])).toEqual([1, 2, 3]);
@@ -0,0 +1,10 @@
function x(e){
const r = [];
for (const s of e) {
e = null;
r.push(s);
}
return r;
}

expect(x([1, 2, 3])).toEqual([1, 2, 3]);
@@ -0,0 +1,7 @@
{
"assumptions": {
"iterableIsArray": true
},
"targets": "node 4.0",
"presets": ["env"]
}
@@ -0,0 +1,10 @@
function x(e) {
var r = [];
for (var _i2 = 0, _e2 = e; _i2 < _e2.length; _i2++) {
var s = _e2[_i2];
e = null;
r.push(s);
}
return r;
}
expect(x([1, 2, 3])).toEqual([1, 2, 3]);
@@ -1,11 +1,9 @@
function x(e) {
var r = [];
for (var _i2 = 0; _i2 < e.length; _i2++) {
var s = e[_i2];
{
var _e = s;
r.push(_e);
}
for (var _i2 = 0, _arr2 = e; _i2 < _arr2.length; _i2++) {
var s = _arr2[_i2];
var _e = s;
r.push(_e);
}
return r;
}
Expand Down

0 comments on commit 7c673d2

Please sign in to comment.