Add "allowArrayLike" opt to destructuring and spread transforms (#11265)

This commit is contained in:
Nicolò Ribaudo
2020-05-24 23:00:06 +02:00
committed by GitHub
parent 28231e1be6
commit 93978267ec
20 changed files with 110 additions and 5 deletions

View File

@@ -4,13 +4,13 @@ import { types as t } from "@babel/core";
export default declare((api, options) => {
api.assertVersion(7);
const { loose } = options;
const { loose, allowArrayLike } = options;
function getSpreadLiteral(spread, scope) {
if (loose && !t.isIdentifier(spread.argument, { name: "arguments" })) {
return spread.argument;
} else {
return scope.toArray(spread.argument, true);
return scope.toArray(spread.argument, true, allowArrayLike);
}
}

View File

@@ -0,0 +1,6 @@
var p2 = { 0: "a", 2: "c", length: 3 };
var arr = [...p2, "d"];
expect(arr).toEqual(["a", undefined, "c", "d"]);
expect(1 in arr).toBe(true); // Not holey

View File

@@ -0,0 +1,6 @@
{
"plugins": [
["external-helpers", { "helperVersion": "7.100.0" }],
["transform-spread", { "allowArrayLike": true }]
]
}

View File

@@ -0,0 +1,5 @@
var p2 = { 0: "b", 1: "c", 2: "d", length: 2 };
var arr = ["a", ...p2, "e"];
expect(arr).toEqual(["a", "b", "c", "e"]);

View File

@@ -0,0 +1,6 @@
{
"plugins": [
["external-helpers", { "helperVersion": "7.100.0" }],
["transform-spread", { "allowArrayLike": true }]
]
}

View File

@@ -0,0 +1,5 @@
var p2 = { 0: "b", 1: "c", 2: "d", length: 3 };
var arr = ["a", ...p2, "e"];
expect(arr).toEqual(["a", "b", "c", "d", "e"]);

View File

@@ -0,0 +1 @@
var arr = ["a", ...p2, "e"];

View File

@@ -0,0 +1,6 @@
{
"plugins": [
["external-helpers", { "helperVersion": "7.100.0" }],
["transform-spread", { "allowArrayLike": true }]
]
}

View File

@@ -0,0 +1 @@
var arr = ["a"].concat(babelHelpers.maybeArrayLike(babelHelpers.toConsumableArray, p2), ["e"]);