Add "allowArrayLike" support to the for-of transform (#11266)
This commit is contained in:
parent
5dd7f438c9
commit
28231e1be6
8
Makefile
8
Makefile
@ -219,6 +219,14 @@ prepublish:
|
|||||||
IS_PUBLISH=true $(MAKE) test
|
IS_PUBLISH=true $(MAKE) test
|
||||||
|
|
||||||
new-version:
|
new-version:
|
||||||
|
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||||
|
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||||
|
@echo "!!!!!! !!!!!!"
|
||||||
|
@echo "!!!!!! Enable the check in transform-for-of !!!!!!"
|
||||||
|
@echo "!!!!!! !!!!!!"
|
||||||
|
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||||
|
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||||
|
@exit 1
|
||||||
git pull --rebase
|
git pull --rebase
|
||||||
$(YARN) lerna version --force-publish=$(FORCE_PUBLISH)
|
$(YARN) lerna version --force-publish=$(FORCE_PUBLISH)
|
||||||
|
|
||||||
|
|||||||
@ -1082,10 +1082,16 @@ helpers.createForOfIteratorHelper = helper("7.9.0")`
|
|||||||
// e: error (called whenever something throws)
|
// e: error (called whenever something throws)
|
||||||
// f: finish (always called at the end)
|
// f: finish (always called at the end)
|
||||||
|
|
||||||
export default function _createForOfIteratorHelper(o) {
|
export default function _createForOfIteratorHelper(o, allowArrayLike) {
|
||||||
|
var it;
|
||||||
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
|
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
|
||||||
// Fallback for engines without symbol support
|
// Fallback for engines without symbol support
|
||||||
if (Array.isArray(o) || (o = unsupportedIterableToArray(o))) {
|
if (
|
||||||
|
Array.isArray(o) ||
|
||||||
|
(it = unsupportedIterableToArray(o)) ||
|
||||||
|
(allowArrayLike && o && typeof o.length === "number")
|
||||||
|
) {
|
||||||
|
if (it) o = it;
|
||||||
var i = 0;
|
var i = 0;
|
||||||
var F = function(){};
|
var F = function(){};
|
||||||
return {
|
return {
|
||||||
@ -1102,7 +1108,7 @@ helpers.createForOfIteratorHelper = helper("7.9.0")`
|
|||||||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
throw new TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var it, normalCompletion = true, didErr = false, err;
|
var normalCompletion = true, didErr = false, err;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
s: function() {
|
s: function() {
|
||||||
@ -1131,22 +1137,29 @@ helpers.createForOfIteratorHelper = helper("7.9.0")`
|
|||||||
helpers.createForOfIteratorHelperLoose = helper("7.9.0")`
|
helpers.createForOfIteratorHelperLoose = helper("7.9.0")`
|
||||||
import unsupportedIterableToArray from "unsupportedIterableToArray";
|
import unsupportedIterableToArray from "unsupportedIterableToArray";
|
||||||
|
|
||||||
export default function _createForOfIteratorHelperLoose(o) {
|
export default function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
||||||
var i = 0;
|
var it;
|
||||||
|
|
||||||
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
|
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
|
||||||
// Fallback for engines without symbol support
|
// Fallback for engines without symbol support
|
||||||
if (Array.isArray(o) || (o = unsupportedIterableToArray(o)))
|
if (
|
||||||
|
Array.isArray(o) ||
|
||||||
|
(it = unsupportedIterableToArray(o)) ||
|
||||||
|
(allowArrayLike && o && typeof o.length === "number")
|
||||||
|
) {
|
||||||
|
if (it) o = it;
|
||||||
|
var i = 0;
|
||||||
return function() {
|
return function() {
|
||||||
if (i >= o.length) return { done: true };
|
if (i >= o.length) return { done: true };
|
||||||
return { done: false, value: o[i++] };
|
return { done: false, value: o[i++] };
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
throw new TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
||||||
}
|
}
|
||||||
|
|
||||||
i = o[Symbol.iterator]();
|
it = o[Symbol.iterator]();
|
||||||
return i.next.bind(i);
|
return it.next.bind(it);
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import transformWithoutHelper from "./no-helper-implementation";
|
|||||||
export default declare((api, options) => {
|
export default declare((api, options) => {
|
||||||
api.assertVersion(7);
|
api.assertVersion(7);
|
||||||
|
|
||||||
const { loose, assumeArray } = options;
|
const { loose, assumeArray, allowArrayLike } = options;
|
||||||
|
|
||||||
if (loose === true && assumeArray === true) {
|
if (loose === true && assumeArray === true) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@ -14,6 +14,30 @@ export default declare((api, options) => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (assumeArray === true && allowArrayLike === true) {
|
||||||
|
throw new Error(
|
||||||
|
`The assumeArray and allowArrayLike options cannot be used together in @babel/plugin-transform-for-of`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Remove in Babel 8
|
||||||
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
// TODO: Enable before releasing 7.10.0
|
||||||
|
/*if (allowArrayLike && /^7\.\d\./.test(api.version)) {
|
||||||
|
throw new Error(
|
||||||
|
`The allowArrayLike is only supported when using @babel/core@^7.10.0`,
|
||||||
|
);
|
||||||
|
}*/
|
||||||
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
|
||||||
if (assumeArray) {
|
if (assumeArray) {
|
||||||
return {
|
return {
|
||||||
name: "transform-for-of",
|
name: "transform-for-of",
|
||||||
@ -86,12 +110,12 @@ export default declare((api, options) => {
|
|||||||
`);
|
`);
|
||||||
|
|
||||||
const buildForOfLoose = template.statements(`
|
const buildForOfLoose = template.statements(`
|
||||||
for (var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT), STEP_KEY;
|
for (var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT, ALLOW_ARRAY_LIKE), STEP_KEY;
|
||||||
!(STEP_KEY = ITERATOR_HELPER()).done;) BODY;
|
!(STEP_KEY = ITERATOR_HELPER()).done;) BODY;
|
||||||
`);
|
`);
|
||||||
|
|
||||||
const buildForOf = template.statements(`
|
const buildForOf = template.statements(`
|
||||||
var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT), STEP_KEY;
|
var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT, ALLOW_ARRAY_LIKE), STEP_KEY;
|
||||||
try {
|
try {
|
||||||
for (ITERATOR_HELPER.s(); !(STEP_KEY = ITERATOR_HELPER.n()).done;) BODY;
|
for (ITERATOR_HELPER.s(); !(STEP_KEY = ITERATOR_HELPER.n()).done;) BODY;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -200,6 +224,7 @@ export default declare((api, options) => {
|
|||||||
const nodes = builder.build({
|
const nodes = builder.build({
|
||||||
CREATE_ITERATOR_HELPER: state.addHelper(builder.helper),
|
CREATE_ITERATOR_HELPER: state.addHelper(builder.helper),
|
||||||
ITERATOR_HELPER: scope.generateUidIdentifier("iterator"),
|
ITERATOR_HELPER: scope.generateUidIdentifier("iterator"),
|
||||||
|
ALLOW_ARRAY_LIKE: allowArrayLike ? t.booleanLiteral(true) : null,
|
||||||
STEP_KEY: t.identifier(stepKey),
|
STEP_KEY: t.identifier(stepKey),
|
||||||
OBJECT: node.right,
|
OBJECT: node.right,
|
||||||
BODY: node.body,
|
BODY: node.body,
|
||||||
|
|||||||
7
packages/babel-plugin-transform-for-of/test/fixtures/loose-allowArrayLike/holes/exec.js
vendored
Normal file
7
packages/babel-plugin-transform-for-of/test/fixtures/loose-allowArrayLike/holes/exec.js
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
var p2 = { 0: "a", 2: "c", length: 3 };
|
||||||
|
|
||||||
|
var arr = [];
|
||||||
|
for (var x of p2) arr.push(x);
|
||||||
|
|
||||||
|
expect(arr).toEqual(["a", undefined, "c"]);
|
||||||
|
expect(1 in arr).toBe(true); // Not holey
|
||||||
6
packages/babel-plugin-transform-for-of/test/fixtures/loose-allowArrayLike/holes/options.json
vendored
Normal file
6
packages/babel-plugin-transform-for-of/test/fixtures/loose-allowArrayLike/holes/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["external-helpers", { "helperVersion": "7.100.0" }],
|
||||||
|
["transform-for-of", { "loose": true, "allowArrayLike": true }]
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
var p2 = { 0: "b", 1: "c", 2: "d", length: 2 };
|
||||||
|
|
||||||
|
var arr = [];
|
||||||
|
for (var x of p2) arr.push(x);
|
||||||
|
|
||||||
|
expect(arr).toEqual(["b", "c"]);
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["external-helpers", { "helperVersion": "7.100.0" }],
|
||||||
|
["transform-for-of", { "loose": true, "allowArrayLike": true }]
|
||||||
|
]
|
||||||
|
}
|
||||||
6
packages/babel-plugin-transform-for-of/test/fixtures/loose-allowArrayLike/simple/exec.js
vendored
Normal file
6
packages/babel-plugin-transform-for-of/test/fixtures/loose-allowArrayLike/simple/exec.js
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
var p2 = { 0: "b", 1: "c", 2: "d", length: 3 };
|
||||||
|
|
||||||
|
var arr = [];
|
||||||
|
for (var x of p2) arr.push(x);
|
||||||
|
|
||||||
|
expect(arr).toEqual(["b", "c", "d"]);
|
||||||
1
packages/babel-plugin-transform-for-of/test/fixtures/loose-allowArrayLike/simple/input.js
vendored
Normal file
1
packages/babel-plugin-transform-for-of/test/fixtures/loose-allowArrayLike/simple/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
for (var x of p2) arr.push(x);
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["external-helpers", { "helperVersion": "7.100.0" }],
|
||||||
|
["transform-for-of", { "loose": true, "allowArrayLike": true }]
|
||||||
|
]
|
||||||
|
}
|
||||||
4
packages/babel-plugin-transform-for-of/test/fixtures/loose-allowArrayLike/simple/output.js
vendored
Normal file
4
packages/babel-plugin-transform-for-of/test/fixtures/loose-allowArrayLike/simple/output.js
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
for (var _iterator = babelHelpers.createForOfIteratorHelperLoose(p2, true), _step; !(_step = _iterator()).done;) {
|
||||||
|
var x = _step.value;
|
||||||
|
arr.push(x);
|
||||||
|
}
|
||||||
7
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/holes/exec.js
vendored
Normal file
7
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/holes/exec.js
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
var p2 = { 0: "a", 2: "c", length: 3 };
|
||||||
|
|
||||||
|
var arr = [];
|
||||||
|
for (var x of p2) arr.push(x);
|
||||||
|
|
||||||
|
expect(arr).toEqual(["a", undefined, "c"]);
|
||||||
|
expect(1 in arr).toBe(true); // Not holey
|
||||||
6
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/holes/options.json
vendored
Normal file
6
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/holes/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["external-helpers", { "helperVersion": "7.100.0" }],
|
||||||
|
["transform-for-of", { "allowArrayLike": true }]
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
var p2 = { 0: "b", 1: "c", 2: "d", length: 2 };
|
||||||
|
|
||||||
|
var arr = [];
|
||||||
|
for (var x of p2) arr.push(x);
|
||||||
|
|
||||||
|
expect(arr).toEqual(["b", "c"]);
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["external-helpers", { "helperVersion": "7.100.0" }],
|
||||||
|
["transform-for-of", { "allowArrayLike": true }]
|
||||||
|
]
|
||||||
|
}
|
||||||
6
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/simple/exec.js
vendored
Normal file
6
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/simple/exec.js
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
var p2 = { 0: "b", 1: "c", 2: "d", length: 3 };
|
||||||
|
|
||||||
|
var arr = [];
|
||||||
|
for (var x of p2) arr.push(x);
|
||||||
|
|
||||||
|
expect(arr).toEqual(["b", "c", "d"]);
|
||||||
1
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/simple/input.js
vendored
Normal file
1
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/simple/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
for (var x of p2) arr.push(x);
|
||||||
6
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/simple/options.json
vendored
Normal file
6
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/simple/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["external-helpers", { "helperVersion": "7.100.0" }],
|
||||||
|
["transform-for-of", { "allowArrayLike": true }]
|
||||||
|
]
|
||||||
|
}
|
||||||
13
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/simple/output.js
vendored
Normal file
13
packages/babel-plugin-transform-for-of/test/fixtures/spec-allowArrayLike/simple/output.js
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
var _iterator = babelHelpers.createForOfIteratorHelper(p2, true),
|
||||||
|
_step;
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
||||||
|
var x = _step.value;
|
||||||
|
arr.push(x);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
_iterator.e(err);
|
||||||
|
} finally {
|
||||||
|
_iterator.f();
|
||||||
|
}
|
||||||
@ -6,7 +6,7 @@ function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(
|
|||||||
|
|
||||||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
||||||
|
|
||||||
function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
||||||
|
|
||||||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user