Add "allowArrayLike" opt to destructuring and spread transforms (#11265)
This commit is contained in:
@@ -4,7 +4,11 @@ import { types as t } from "@babel/core";
|
||||
export default declare((api, options) => {
|
||||
api.assertVersion(7);
|
||||
|
||||
const { loose = false, useBuiltIns = false } = options;
|
||||
const {
|
||||
loose = false,
|
||||
useBuiltIns = false,
|
||||
allowArrayLike = false,
|
||||
} = options;
|
||||
|
||||
if (typeof loose !== "boolean") {
|
||||
throw new Error(`.loose must be a boolean or undefined`);
|
||||
@@ -85,6 +89,7 @@ export default declare((api, options) => {
|
||||
this.scope = opts.scope;
|
||||
this.kind = opts.kind;
|
||||
this.arrayOnlySpread = opts.arrayOnlySpread;
|
||||
this.allowArrayLike = opts.allowArrayLike;
|
||||
this.addHelper = opts.addHelper;
|
||||
}
|
||||
|
||||
@@ -141,7 +146,7 @@ export default declare((api, options) => {
|
||||
) {
|
||||
return node;
|
||||
} else {
|
||||
return this.scope.toArray(node, count);
|
||||
return this.scope.toArray(node, count, this.allowArrayLike);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -523,6 +528,7 @@ export default declare((api, options) => {
|
||||
scope: scope,
|
||||
nodes: nodes,
|
||||
arrayOnlySpread,
|
||||
allowArrayLike,
|
||||
addHelper: name => this.addHelper(name),
|
||||
});
|
||||
|
||||
@@ -548,6 +554,7 @@ export default declare((api, options) => {
|
||||
scope: scope,
|
||||
nodes: nodes,
|
||||
arrayOnlySpread,
|
||||
allowArrayLike,
|
||||
addHelper: name => this.addHelper(name),
|
||||
});
|
||||
destructuring.init(pattern, ref);
|
||||
@@ -566,6 +573,7 @@ export default declare((api, options) => {
|
||||
scope: scope,
|
||||
nodes: nodes,
|
||||
arrayOnlySpread,
|
||||
allowArrayLike,
|
||||
addHelper: name => this.addHelper(name),
|
||||
});
|
||||
|
||||
@@ -624,6 +632,7 @@ export default declare((api, options) => {
|
||||
scope: scope,
|
||||
kind: node.kind,
|
||||
arrayOnlySpread,
|
||||
allowArrayLike,
|
||||
addHelper: name => this.addHelper(name),
|
||||
});
|
||||
|
||||
|
||||
6
packages/babel-plugin-transform-destructuring/test/fixtures/allowArrayLike/holes/exec.js
vendored
Normal file
6
packages/babel-plugin-transform-destructuring/test/fixtures/allowArrayLike/holes/exec.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
var o = { 0: "a", 2: "c", length: 3 };
|
||||
|
||||
var [...rest] = o;
|
||||
|
||||
expect(rest).toEqual(["a", undefined, "c"]);
|
||||
expect(1 in rest).toBe(true); // Not holey
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"plugins": [
|
||||
["external-helpers", { "helperVersion": "7.100.0" }],
|
||||
["transform-destructuring", { "allowArrayLike": true }]
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
var o = { 0: "a", 1: "b", 2: "c", length: 2 };
|
||||
|
||||
var [first, ...rest] = o;
|
||||
|
||||
expect(first).toBe("a");
|
||||
expect(rest).toEqual(["b"]);
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"plugins": [
|
||||
["external-helpers", { "helperVersion": "7.100.0" }],
|
||||
["transform-destructuring", { "allowArrayLike": true }]
|
||||
]
|
||||
}
|
||||
6
packages/babel-plugin-transform-destructuring/test/fixtures/allowArrayLike/simple/exec.js
vendored
Normal file
6
packages/babel-plugin-transform-destructuring/test/fixtures/allowArrayLike/simple/exec.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
var o = { 0: "a", 1: "b", 2: "c", length: 3 };
|
||||
|
||||
var [first, ...rest] = o;
|
||||
|
||||
expect(first).toBe("a");
|
||||
expect(rest).toEqual(["b", "c"]);
|
||||
1
packages/babel-plugin-transform-destructuring/test/fixtures/allowArrayLike/simple/input.js
vendored
Normal file
1
packages/babel-plugin-transform-destructuring/test/fixtures/allowArrayLike/simple/input.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var [first, ...rest] = o;
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"plugins": [
|
||||
["external-helpers", { "helperVersion": "7.100.0" }],
|
||||
["transform-destructuring", { "allowArrayLike": true }]
|
||||
]
|
||||
}
|
||||
4
packages/babel-plugin-transform-destructuring/test/fixtures/allowArrayLike/simple/output.js
vendored
Normal file
4
packages/babel-plugin-transform-destructuring/test/fixtures/allowArrayLike/simple/output.js
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
var _o = o,
|
||||
_o2 = babelHelpers.maybeArrayLike(babelHelpers.toArray, _o),
|
||||
first = _o2[0],
|
||||
rest = _o2.slice(1);
|
||||
Reference in New Issue
Block a user