Porting babel-plugin-transform-for-of-as-array into transform-for-of as an option (#6914)
This commit is contained in:
parent
d8bbaaae0a
commit
a992d06c41
@ -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;
|
||||
|
||||
4
packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/actual.js
vendored
Normal file
4
packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/actual.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
let elm;
|
||||
for (elm of array) {
|
||||
console.log(elm);
|
||||
}
|
||||
4
packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/options.json
vendored
Normal file
4
packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/options.json
vendored
Normal 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"
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
let elm;
|
||||
for ([elm] of array) {
|
||||
console.log(elm);
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
let elm;
|
||||
|
||||
for (let _i = 0, _array = array; _i < _array.length; _i++) {
|
||||
[elm] = _array[_i];
|
||||
console.log(elm);
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
for (const [elm] of array) {
|
||||
console.log(elm);
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
for (let _i = 0, _array = array; _i < _array.length; _i++) {
|
||||
const [elm] = _array[_i];
|
||||
console.log(elm);
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
for (const elm of array) {
|
||||
console.log(elm);
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
for (let _i = 0, _array = array; _i < _array.length; _i++) {
|
||||
const elm = _array[_i];
|
||||
console.log(elm);
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
for (const i of items) i;
|
||||
@ -0,0 +1,4 @@
|
||||
for (let _i = 0, _items = items; _i < _items.length; _i++) {
|
||||
const i = _items[_i];
|
||||
i;
|
||||
}
|
||||
@ -0,0 +1,2 @@
|
||||
let i;
|
||||
for (i of items) i;
|
||||
@ -0,0 +1,6 @@
|
||||
let i;
|
||||
|
||||
for (let _i = 0, _items = items; _i < _items.length; _i++) {
|
||||
i = _items[_i];
|
||||
i;
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
import { array } from "foo";
|
||||
|
||||
for (const elm of array) {
|
||||
console.log(elm);
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,7 @@
|
||||
{
|
||||
"plugins": [["transform-for-of", {
|
||||
"assumeArray": true
|
||||
}],
|
||||
["transform-modules-amd"]
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
import { array } from "foo";
|
||||
|
||||
for (const elm of array) {
|
||||
console.log(elm);
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
{
|
||||
"plugins": [["transform-for-of", {
|
||||
"assumeArray": true
|
||||
}],
|
||||
["transform-modules-commonjs"]
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
import { array } from "foo";
|
||||
|
||||
for (const elm of array) {
|
||||
console.log(elm);
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
import { array } from "foo";
|
||||
|
||||
for (let _i = 0; _i < array.length; _i++) {
|
||||
const elm = array[_i];
|
||||
console.log(elm);
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
const array = [];
|
||||
|
||||
for (const elm of array) {
|
||||
console.log(elm);
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
const array = [];
|
||||
|
||||
for (let _i = 0; _i < array.length; _i++) {
|
||||
const elm = array[_i];
|
||||
console.log(elm);
|
||||
}
|
||||
6
packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static/actual.js
vendored
Normal file
6
packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static/actual.js
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
const array = [];
|
||||
let elm;
|
||||
|
||||
for (elm of array) {
|
||||
console.log(elm);
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
const array = [];
|
||||
let elm;
|
||||
|
||||
for (let _i = 0; _i < array.length; _i++) {
|
||||
elm = array[_i];
|
||||
console.log(elm);
|
||||
}
|
||||
5
packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/actual.js
vendored
Normal file
5
packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/actual.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
let elm;
|
||||
|
||||
for (elm of array) {
|
||||
console.log(elm);
|
||||
}
|
||||
6
packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/expected.js
vendored
Normal file
6
packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/expected.js
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
let elm;
|
||||
|
||||
for (let _i = 0, _array = array; _i < _array.length; _i++) {
|
||||
elm = _array[_i];
|
||||
console.log(elm);
|
||||
}
|
||||
6
packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/options.json
vendored
Normal file
6
packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"plugins": [["transform-for-of", {
|
||||
"assumeArray": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user