From a992d06c41dd55c4fcdad5e7dadeeeea28bfcb2f Mon Sep 17 00:00:00 2001 From: Raja Sekar Date: Fri, 1 Dec 2017 04:18:56 +0530 Subject: [PATCH] Porting babel-plugin-transform-for-of-as-array into transform-for-of as an option (#6914) --- .../src/index.js | 56 ++++++++++++++++++- .../fixtures/error/invalid-option/actual.js | 4 ++ .../error/invalid-option/options.json | 4 ++ .../for-of-array-pattern/actual.js | 4 ++ .../for-of-array-pattern/expected.js | 6 ++ .../actual.js | 3 + .../expected.js | 4 ++ .../for-of-declaration/actual.js | 3 + .../for-of-declaration/expected.js | 4 ++ .../for-of-expression-declaration/actual.js | 1 + .../for-of-expression-declaration/expected.js | 4 ++ .../for-of-expression/actual.js | 2 + .../for-of-expression/expected.js | 6 ++ .../for-of-import-amd/actual.js | 5 ++ .../for-of-import-amd/expected.js | 8 +++ .../for-of-import-amd/options.json | 7 +++ .../for-of-import-commonjs/actual.js | 5 ++ .../for-of-import-commonjs/expected.js | 8 +++ .../for-of-import-commonjs/options.json | 7 +++ .../for-of-import-es2015/actual.js | 5 ++ .../for-of-import-es2015/expected.js | 6 ++ .../for-of-static-declaration/actual.js | 5 ++ .../for-of-static-declaration/expected.js | 6 ++ .../for-of-as-array/for-of-static/actual.js | 6 ++ .../for-of-as-array/for-of-static/expected.js | 7 +++ .../fixtures/for-of-as-array/for-of/actual.js | 5 ++ .../for-of-as-array/for-of/expected.js | 6 ++ .../fixtures/for-of-as-array/options.json | 6 ++ 28 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/options.json create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-array-pattern/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-array-pattern/expected.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration-array-pattern/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration-array-pattern/expected.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration/expected.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression-declaration/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression-declaration/expected.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression/expected.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/expected.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/options.json create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/expected.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/options.json create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-es2015/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-es2015/expected.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static-declaration/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static-declaration/expected.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static/expected.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/actual.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/expected.js create mode 100644 packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/options.json diff --git a/packages/babel-plugin-transform-for-of/src/index.js b/packages/babel-plugin-transform-for-of/src/index.js index 81b2e453a6..4e3cc698d3 100644 --- a/packages/babel-plugin-transform-for-of/src/index.js +++ b/packages/babel-plugin-transform-for-of/src/index.js @@ -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; diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/actual.js new file mode 100644 index 0000000000..e974cd83e3 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/actual.js @@ -0,0 +1,4 @@ +let elm; +for (elm of array) { + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/options.json b/packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/options.json new file mode 100644 index 0000000000..bdd6772e84 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/error/invalid-option/options.json @@ -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" +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-array-pattern/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-array-pattern/actual.js new file mode 100644 index 0000000000..8ffcd7ff80 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-array-pattern/actual.js @@ -0,0 +1,4 @@ +let elm; +for ([elm] of array) { + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-array-pattern/expected.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-array-pattern/expected.js new file mode 100644 index 0000000000..f405987d23 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-array-pattern/expected.js @@ -0,0 +1,6 @@ +let elm; + +for (let _i = 0, _array = array; _i < _array.length; _i++) { + [elm] = _array[_i]; + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration-array-pattern/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration-array-pattern/actual.js new file mode 100644 index 0000000000..2de13b3e47 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration-array-pattern/actual.js @@ -0,0 +1,3 @@ +for (const [elm] of array) { + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration-array-pattern/expected.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration-array-pattern/expected.js new file mode 100644 index 0000000000..97fa884dc9 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration-array-pattern/expected.js @@ -0,0 +1,4 @@ +for (let _i = 0, _array = array; _i < _array.length; _i++) { + const [elm] = _array[_i]; + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration/actual.js new file mode 100644 index 0000000000..9f773dd633 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration/actual.js @@ -0,0 +1,3 @@ +for (const elm of array) { + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration/expected.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration/expected.js new file mode 100644 index 0000000000..3794b43c99 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-declaration/expected.js @@ -0,0 +1,4 @@ +for (let _i = 0, _array = array; _i < _array.length; _i++) { + const elm = _array[_i]; + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression-declaration/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression-declaration/actual.js new file mode 100644 index 0000000000..f47de4cad5 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression-declaration/actual.js @@ -0,0 +1 @@ +for (const i of items) i; diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression-declaration/expected.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression-declaration/expected.js new file mode 100644 index 0000000000..935f3004f5 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression-declaration/expected.js @@ -0,0 +1,4 @@ +for (let _i = 0, _items = items; _i < _items.length; _i++) { + const i = _items[_i]; + i; +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression/actual.js new file mode 100644 index 0000000000..794f63b280 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression/actual.js @@ -0,0 +1,2 @@ +let i; +for (i of items) i; diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression/expected.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression/expected.js new file mode 100644 index 0000000000..eef987e35c --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-expression/expected.js @@ -0,0 +1,6 @@ +let i; + +for (let _i = 0, _items = items; _i < _items.length; _i++) { + i = _items[_i]; + i; +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/actual.js new file mode 100644 index 0000000000..bd895e71c7 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/actual.js @@ -0,0 +1,5 @@ +import { array } from "foo"; + +for (const elm of array) { + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/expected.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/expected.js new file mode 100644 index 0000000000..0e9d9e0152 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/expected.js @@ -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); + } +}); diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/options.json b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/options.json new file mode 100644 index 0000000000..9277b9005c --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-amd/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [["transform-for-of", { + "assumeArray": true + }], + ["transform-modules-amd"] + ] +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/actual.js new file mode 100644 index 0000000000..bd895e71c7 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/actual.js @@ -0,0 +1,5 @@ +import { array } from "foo"; + +for (const elm of array) { + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/expected.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/expected.js new file mode 100644 index 0000000000..9cf9ad09b7 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/expected.js @@ -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); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/options.json b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/options.json new file mode 100644 index 0000000000..867a058d6a --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-commonjs/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [["transform-for-of", { + "assumeArray": true + }], + ["transform-modules-commonjs"] + ] +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-es2015/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-es2015/actual.js new file mode 100644 index 0000000000..bd895e71c7 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-es2015/actual.js @@ -0,0 +1,5 @@ +import { array } from "foo"; + +for (const elm of array) { + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-es2015/expected.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-es2015/expected.js new file mode 100644 index 0000000000..62a2980913 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-import-es2015/expected.js @@ -0,0 +1,6 @@ +import { array } from "foo"; + +for (let _i = 0; _i < array.length; _i++) { + const elm = array[_i]; + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static-declaration/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static-declaration/actual.js new file mode 100644 index 0000000000..b84065c48d --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static-declaration/actual.js @@ -0,0 +1,5 @@ +const array = []; + +for (const elm of array) { + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static-declaration/expected.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static-declaration/expected.js new file mode 100644 index 0000000000..072dd5498d --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static-declaration/expected.js @@ -0,0 +1,6 @@ +const array = []; + +for (let _i = 0; _i < array.length; _i++) { + const elm = array[_i]; + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static/actual.js new file mode 100644 index 0000000000..d4de87d351 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static/actual.js @@ -0,0 +1,6 @@ +const array = []; +let elm; + +for (elm of array) { + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static/expected.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static/expected.js new file mode 100644 index 0000000000..fc47bc82ed --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of-static/expected.js @@ -0,0 +1,7 @@ +const array = []; +let elm; + +for (let _i = 0; _i < array.length; _i++) { + elm = array[_i]; + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/actual.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/actual.js new file mode 100644 index 0000000000..4b77690e9e --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/actual.js @@ -0,0 +1,5 @@ +let elm; + +for (elm of array) { + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/expected.js b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/expected.js new file mode 100644 index 0000000000..1024f7a727 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/for-of/expected.js @@ -0,0 +1,6 @@ +let elm; + +for (let _i = 0, _array = array; _i < _array.length; _i++) { + elm = _array[_i]; + console.log(elm); +} diff --git a/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/options.json b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/options.json new file mode 100644 index 0000000000..676ed1a7e9 --- /dev/null +++ b/packages/babel-plugin-transform-for-of/test/fixtures/for-of-as-array/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [["transform-for-of", { + "assumeArray": true + }] + ] +}