Porting babel-plugin-transform-for-of-as-array into transform-for-of as an option (#6914)

This commit is contained in:
Raja Sekar 2017-12-01 04:18:56 +05:30 committed by Henry Zhu
parent d8bbaaae0a
commit a992d06c41
28 changed files with 192 additions and 1 deletions

View File

@ -1,7 +1,61 @@
import { template, types as t } from "@babel/core";
export default function(api, options) {
const { loose } = options;
const { loose, assumeArray } = options;
if (loose === true && assumeArray === true) {
throw new Error(
`The loose and assumeArray options cannot be used together in @babel/plugin-transform-for-of`,
);
}
if (assumeArray) {
return {
visitor: {
ForOfStatement(path) {
const { scope } = path;
const { left, right, body } = path.node;
const i = scope.generateUidIdentifier("i");
let array = scope.maybeGenerateMemoised(right, true);
const inits = [t.variableDeclarator(i, t.numericLiteral(0))];
if (array) {
inits.push(t.variableDeclarator(array, right));
} else {
array = right;
}
const item = t.memberExpression(array, t.clone(i), true);
let assignment;
if (t.isVariableDeclaration(left)) {
assignment = left;
assignment.declarations[0].init = item;
} else {
assignment = t.expressionStatement(
t.assignmentExpression("=", left, item),
);
}
const block = t.toBlock(body);
block.body.unshift(assignment);
path.replaceWith(
t.forStatement(
t.variableDeclaration("let", inits),
t.binaryExpression(
"<",
t.clone(i),
t.memberExpression(t.clone(array), t.identifier("length")),
),
t.updateExpression("++", t.clone(i)),
block,
),
);
},
},
};
}
const pushComputedProps = loose
? pushComputedPropsLoose
: pushComputedPropsSpec;

View File

@ -0,0 +1,4 @@
let elm;
for (elm of array) {
console.log(elm);
}

View File

@ -0,0 +1,4 @@
{
"plugins": [["transform-for-of", { "assumeArray": true, "loose": true}]],
"throws": "The loose and assumeArray options cannot be used together in @babel/plugin-transform-for-of"
}

View File

@ -0,0 +1,4 @@
let elm;
for ([elm] of array) {
console.log(elm);
}

View File

@ -0,0 +1,6 @@
let elm;
for (let _i = 0, _array = array; _i < _array.length; _i++) {
[elm] = _array[_i];
console.log(elm);
}

View File

@ -0,0 +1,3 @@
for (const [elm] of array) {
console.log(elm);
}

View File

@ -0,0 +1,4 @@
for (let _i = 0, _array = array; _i < _array.length; _i++) {
const [elm] = _array[_i];
console.log(elm);
}

View File

@ -0,0 +1,3 @@
for (const elm of array) {
console.log(elm);
}

View File

@ -0,0 +1,4 @@
for (let _i = 0, _array = array; _i < _array.length; _i++) {
const elm = _array[_i];
console.log(elm);
}

View File

@ -0,0 +1 @@
for (const i of items) i;

View File

@ -0,0 +1,4 @@
for (let _i = 0, _items = items; _i < _items.length; _i++) {
const i = _items[_i];
i;
}

View File

@ -0,0 +1,2 @@
let i;
for (i of items) i;

View File

@ -0,0 +1,6 @@
let i;
for (let _i = 0, _items = items; _i < _items.length; _i++) {
i = _items[_i];
i;
}

View File

@ -0,0 +1,5 @@
import { array } from "foo";
for (const elm of array) {
console.log(elm);
}

View File

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

View File

@ -0,0 +1,7 @@
{
"plugins": [["transform-for-of", {
"assumeArray": true
}],
["transform-modules-amd"]
]
}

View File

@ -0,0 +1,5 @@
import { array } from "foo";
for (const elm of array) {
console.log(elm);
}

View File

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

View File

@ -0,0 +1,7 @@
{
"plugins": [["transform-for-of", {
"assumeArray": true
}],
["transform-modules-commonjs"]
]
}

View File

@ -0,0 +1,5 @@
import { array } from "foo";
for (const elm of array) {
console.log(elm);
}

View File

@ -0,0 +1,6 @@
import { array } from "foo";
for (let _i = 0; _i < array.length; _i++) {
const elm = array[_i];
console.log(elm);
}

View File

@ -0,0 +1,5 @@
const array = [];
for (const elm of array) {
console.log(elm);
}

View File

@ -0,0 +1,6 @@
const array = [];
for (let _i = 0; _i < array.length; _i++) {
const elm = array[_i];
console.log(elm);
}

View File

@ -0,0 +1,6 @@
const array = [];
let elm;
for (elm of array) {
console.log(elm);
}

View File

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

View File

@ -0,0 +1,5 @@
let elm;
for (elm of array) {
console.log(elm);
}

View File

@ -0,0 +1,6 @@
let elm;
for (let _i = 0, _array = array; _i < _array.length; _i++) {
elm = _array[_i];
console.log(elm);
}

View File

@ -0,0 +1,6 @@
{
"plugins": [["transform-for-of", {
"assumeArray": true
}]
]
}