remove es20xx prefixes from plugins and rename folders (#6575)
This commit is contained in:
3
packages/babel-plugin-transform-destructuring/.npmignore
Normal file
3
packages/babel-plugin-transform-destructuring/.npmignore
Normal file
@@ -0,0 +1,3 @@
|
||||
src
|
||||
test
|
||||
*.log
|
||||
53
packages/babel-plugin-transform-destructuring/README.md
Normal file
53
packages/babel-plugin-transform-destructuring/README.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# @babel/plugin-transform-destructuring
|
||||
|
||||
> Compile ES2015 destructuring to ES5
|
||||
|
||||
## Examples
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
let arr = [1,2,3];
|
||||
let {x, y, z} = arr;
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
var arr = [1, 2, 3];
|
||||
var x = arr.x,
|
||||
y = arr.y,
|
||||
z = arr.z;
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm install --save-dev @babel/plugin-transform-destructuring
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["@babel/transform-destructuring"]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
babel --plugins @babel/transform-destructuring script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require("@babel/core").transform("code", {
|
||||
plugins: ["@babel/transform-destructuring"]
|
||||
});
|
||||
```
|
||||
17
packages/babel-plugin-transform-destructuring/package.json
Normal file
17
packages/babel-plugin-transform-destructuring/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "@babel/plugin-transform-destructuring",
|
||||
"version": "7.0.0-beta.3",
|
||||
"description": "Compile ES2015 destructuring to ES5",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-destructuring",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"@babel/core": "7.0.0-beta.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/helper-plugin-test-runner": "7.0.0-beta.3"
|
||||
}
|
||||
}
|
||||
557
packages/babel-plugin-transform-destructuring/src/index.js
Normal file
557
packages/babel-plugin-transform-destructuring/src/index.js
Normal file
@@ -0,0 +1,557 @@
|
||||
export default function({ types: t }) {
|
||||
/**
|
||||
* Test if a VariableDeclaration's declarations contains any Patterns.
|
||||
*/
|
||||
|
||||
function variableDeclarationHasPattern(node) {
|
||||
for (const declar of (node.declarations: Array)) {
|
||||
if (t.isPattern(declar.id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if an ArrayPattern's elements contain any RestElements.
|
||||
*/
|
||||
|
||||
function hasRest(pattern) {
|
||||
for (const elem of (pattern.elements: Array)) {
|
||||
if (t.isRestElement(elem)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const arrayUnpackVisitor = {
|
||||
ReferencedIdentifier(path, state) {
|
||||
if (state.bindings[path.node.name]) {
|
||||
state.deopt = true;
|
||||
path.stop();
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
class DestructuringTransformer {
|
||||
constructor(opts) {
|
||||
this.blockHoist = opts.blockHoist;
|
||||
this.operator = opts.operator;
|
||||
this.arrays = {};
|
||||
this.nodes = opts.nodes || [];
|
||||
this.scope = opts.scope;
|
||||
this.file = opts.file;
|
||||
this.kind = opts.kind;
|
||||
}
|
||||
|
||||
buildVariableAssignment(id, init) {
|
||||
let op = this.operator;
|
||||
if (t.isMemberExpression(id)) op = "=";
|
||||
|
||||
let node;
|
||||
|
||||
if (op) {
|
||||
node = t.expressionStatement(t.assignmentExpression(op, id, init));
|
||||
} else {
|
||||
node = t.variableDeclaration(this.kind, [
|
||||
t.variableDeclarator(id, init),
|
||||
]);
|
||||
}
|
||||
|
||||
node._blockHoist = this.blockHoist;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
buildVariableDeclaration(id, init) {
|
||||
const declar = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(id, init),
|
||||
]);
|
||||
declar._blockHoist = this.blockHoist;
|
||||
return declar;
|
||||
}
|
||||
|
||||
push(id, init) {
|
||||
if (t.isObjectPattern(id)) {
|
||||
this.pushObjectPattern(id, init);
|
||||
} else if (t.isArrayPattern(id)) {
|
||||
this.pushArrayPattern(id, init);
|
||||
} else if (t.isAssignmentPattern(id)) {
|
||||
this.pushAssignmentPattern(id, init);
|
||||
} else {
|
||||
this.nodes.push(this.buildVariableAssignment(id, init));
|
||||
}
|
||||
}
|
||||
|
||||
toArray(node, count) {
|
||||
if (
|
||||
this.file.opts.loose ||
|
||||
(t.isIdentifier(node) && this.arrays[node.name])
|
||||
) {
|
||||
return node;
|
||||
} else {
|
||||
return this.scope.toArray(node, count);
|
||||
}
|
||||
}
|
||||
|
||||
pushAssignmentPattern(pattern, valueRef) {
|
||||
// we need to assign the current value of the assignment to avoid evaluating
|
||||
// it more than once
|
||||
|
||||
const tempValueRef = this.scope.generateUidIdentifierBasedOnNode(
|
||||
valueRef,
|
||||
);
|
||||
|
||||
const declar = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(tempValueRef, valueRef),
|
||||
]);
|
||||
declar._blockHoist = this.blockHoist;
|
||||
this.nodes.push(declar);
|
||||
|
||||
//
|
||||
|
||||
const tempConditional = t.conditionalExpression(
|
||||
t.binaryExpression(
|
||||
"===",
|
||||
tempValueRef,
|
||||
this.scope.buildUndefinedNode(),
|
||||
),
|
||||
pattern.right,
|
||||
tempValueRef,
|
||||
);
|
||||
|
||||
const left = pattern.left;
|
||||
if (t.isPattern(left)) {
|
||||
const tempValueDefault = t.expressionStatement(
|
||||
t.assignmentExpression("=", tempValueRef, tempConditional),
|
||||
);
|
||||
tempValueDefault._blockHoist = this.blockHoist;
|
||||
|
||||
this.nodes.push(tempValueDefault);
|
||||
this.push(left, tempValueRef);
|
||||
} else {
|
||||
this.nodes.push(this.buildVariableAssignment(left, tempConditional));
|
||||
}
|
||||
}
|
||||
|
||||
pushObjectRest(pattern, objRef, spreadProp, spreadPropIndex) {
|
||||
// get all the keys that appear in this object before the current spread
|
||||
|
||||
let keys = [];
|
||||
|
||||
for (let i = 0; i < pattern.properties.length; i++) {
|
||||
const prop = pattern.properties[i];
|
||||
|
||||
// we've exceeded the index of the spread property to all properties to the
|
||||
// right need to be ignored
|
||||
if (i >= spreadPropIndex) break;
|
||||
|
||||
// ignore other spread properties
|
||||
if (t.isRestElement(prop)) continue;
|
||||
|
||||
let key = prop.key;
|
||||
if (t.isIdentifier(key) && !prop.computed) {
|
||||
key = t.stringLiteral(prop.key.name);
|
||||
}
|
||||
keys.push(key);
|
||||
}
|
||||
|
||||
keys = t.arrayExpression(keys);
|
||||
|
||||
//
|
||||
|
||||
const value = t.callExpression(
|
||||
this.file.addHelper("objectWithoutProperties"),
|
||||
[objRef, keys],
|
||||
);
|
||||
this.nodes.push(this.buildVariableAssignment(spreadProp.argument, value));
|
||||
}
|
||||
|
||||
pushObjectProperty(prop, propRef) {
|
||||
if (t.isLiteral(prop.key)) prop.computed = true;
|
||||
|
||||
const pattern = prop.value;
|
||||
const objRef = t.memberExpression(propRef, prop.key, prop.computed);
|
||||
|
||||
if (t.isPattern(pattern)) {
|
||||
this.push(pattern, objRef);
|
||||
} else {
|
||||
this.nodes.push(this.buildVariableAssignment(pattern, objRef));
|
||||
}
|
||||
}
|
||||
|
||||
pushObjectPattern(pattern, objRef) {
|
||||
// https://github.com/babel/babel/issues/681
|
||||
|
||||
if (!pattern.properties.length) {
|
||||
this.nodes.push(
|
||||
t.expressionStatement(
|
||||
t.callExpression(this.file.addHelper("objectDestructuringEmpty"), [
|
||||
objRef,
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// if we have more than one properties in this pattern and the objectRef is a
|
||||
// member expression then we need to assign it to a temporary variable so it's
|
||||
// only evaluated once
|
||||
|
||||
if (pattern.properties.length > 1 && !this.scope.isStatic(objRef)) {
|
||||
const temp = this.scope.generateUidIdentifierBasedOnNode(objRef);
|
||||
this.nodes.push(this.buildVariableDeclaration(temp, objRef));
|
||||
objRef = temp;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
for (let i = 0; i < pattern.properties.length; i++) {
|
||||
const prop = pattern.properties[i];
|
||||
if (t.isRestElement(prop)) {
|
||||
this.pushObjectRest(pattern, objRef, prop, i);
|
||||
} else {
|
||||
this.pushObjectProperty(prop, t.clone(objRef));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
canUnpackArrayPattern(pattern, arr) {
|
||||
// not an array so there's no way we can deal with this
|
||||
if (!t.isArrayExpression(arr)) return false;
|
||||
|
||||
// pattern has less elements than the array and doesn't have a rest so some
|
||||
// elements wont be evaluated
|
||||
if (pattern.elements.length > arr.elements.length) return;
|
||||
if (pattern.elements.length < arr.elements.length && !hasRest(pattern)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const elem of (pattern.elements: Array)) {
|
||||
// deopt on holes
|
||||
if (!elem) return false;
|
||||
|
||||
// deopt on member expressions as they may be included in the RHS
|
||||
if (t.isMemberExpression(elem)) return false;
|
||||
}
|
||||
|
||||
for (const elem of (arr.elements: Array)) {
|
||||
// deopt on spread elements
|
||||
if (t.isSpreadElement(elem)) return false;
|
||||
|
||||
// deopt call expressions as they might change values of LHS variables
|
||||
if (t.isCallExpression(elem)) return false;
|
||||
|
||||
// deopt on member expressions as they may be getter/setters and have side-effects
|
||||
if (t.isMemberExpression(elem)) return false;
|
||||
}
|
||||
|
||||
// deopt on reference to left side identifiers
|
||||
const bindings = t.getBindingIdentifiers(pattern);
|
||||
const state = { deopt: false, bindings };
|
||||
this.scope.traverse(arr, arrayUnpackVisitor, state);
|
||||
return !state.deopt;
|
||||
}
|
||||
|
||||
pushUnpackedArrayPattern(pattern, arr) {
|
||||
for (let i = 0; i < pattern.elements.length; i++) {
|
||||
const elem = pattern.elements[i];
|
||||
if (t.isRestElement(elem)) {
|
||||
this.push(elem.argument, t.arrayExpression(arr.elements.slice(i)));
|
||||
} else {
|
||||
this.push(elem, arr.elements[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pushArrayPattern(pattern, arrayRef) {
|
||||
if (!pattern.elements) return;
|
||||
|
||||
// optimise basic array destructuring of an array expression
|
||||
//
|
||||
// we can't do this to a pattern of unequal size to it's right hand
|
||||
// array expression as then there will be values that wont be evaluated
|
||||
//
|
||||
// eg: let [a, b] = [1, 2];
|
||||
|
||||
if (this.canUnpackArrayPattern(pattern, arrayRef)) {
|
||||
return this.pushUnpackedArrayPattern(pattern, arrayRef);
|
||||
}
|
||||
|
||||
// if we have a rest then we need all the elements so don't tell
|
||||
// `scope.toArray` to only get a certain amount
|
||||
|
||||
const count = !hasRest(pattern) && pattern.elements.length;
|
||||
|
||||
// so we need to ensure that the `arrayRef` is an array, `scope.toArray` will
|
||||
// return a locally bound identifier if it's been inferred to be an array,
|
||||
// otherwise it'll be a call to a helper that will ensure it's one
|
||||
|
||||
const toArray = this.toArray(arrayRef, count);
|
||||
|
||||
if (t.isIdentifier(toArray)) {
|
||||
// we've been given an identifier so it must have been inferred to be an
|
||||
// array
|
||||
arrayRef = toArray;
|
||||
} else {
|
||||
arrayRef = this.scope.generateUidIdentifierBasedOnNode(arrayRef);
|
||||
this.arrays[arrayRef.name] = true;
|
||||
this.nodes.push(this.buildVariableDeclaration(arrayRef, toArray));
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
for (let i = 0; i < pattern.elements.length; i++) {
|
||||
let elem = pattern.elements[i];
|
||||
|
||||
// hole
|
||||
if (!elem) continue;
|
||||
|
||||
let elemRef;
|
||||
|
||||
if (t.isRestElement(elem)) {
|
||||
elemRef = this.toArray(arrayRef);
|
||||
elemRef = t.callExpression(
|
||||
t.memberExpression(elemRef, t.identifier("slice")),
|
||||
[t.numericLiteral(i)],
|
||||
);
|
||||
|
||||
// set the element to the rest element argument since we've dealt with it
|
||||
// being a rest already
|
||||
elem = elem.argument;
|
||||
} else {
|
||||
elemRef = t.memberExpression(arrayRef, t.numericLiteral(i), true);
|
||||
}
|
||||
|
||||
this.push(elem, elemRef);
|
||||
}
|
||||
}
|
||||
|
||||
init(pattern, ref) {
|
||||
// trying to destructure a value that we can't evaluate more than once so we
|
||||
// need to save it to a variable
|
||||
|
||||
if (!t.isArrayExpression(ref) && !t.isMemberExpression(ref)) {
|
||||
const memo = this.scope.maybeGenerateMemoised(ref, true);
|
||||
if (memo) {
|
||||
this.nodes.push(this.buildVariableDeclaration(memo, ref));
|
||||
ref = memo;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
this.push(pattern, ref);
|
||||
|
||||
return this.nodes;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
visitor: {
|
||||
ExportNamedDeclaration(path) {
|
||||
const declaration = path.get("declaration");
|
||||
if (!declaration.isVariableDeclaration()) return;
|
||||
if (!variableDeclarationHasPattern(declaration.node)) return;
|
||||
|
||||
const specifiers = [];
|
||||
|
||||
for (const name in path.getOuterBindingIdentifiers(path)) {
|
||||
const id = t.identifier(name);
|
||||
specifiers.push(t.exportSpecifier(id, id));
|
||||
}
|
||||
|
||||
// Split the declaration and export list into two declarations so that the variable
|
||||
// declaration can be split up later without needing to worry about not being a
|
||||
// top-level statement.
|
||||
path.replaceWith(declaration.node);
|
||||
path.insertAfter(t.exportNamedDeclaration(null, specifiers));
|
||||
},
|
||||
|
||||
ForXStatement(path, file) {
|
||||
const { node, scope } = path;
|
||||
const left = node.left;
|
||||
|
||||
if (t.isPattern(left)) {
|
||||
// for ({ length: k } in { abc: 3 });
|
||||
|
||||
const temp = scope.generateUidIdentifier("ref");
|
||||
|
||||
node.left = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(temp),
|
||||
]);
|
||||
|
||||
path.ensureBlock();
|
||||
|
||||
node.body.body.unshift(
|
||||
t.variableDeclaration("var", [t.variableDeclarator(left, temp)]),
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!t.isVariableDeclaration(left)) return;
|
||||
|
||||
const pattern = left.declarations[0].id;
|
||||
if (!t.isPattern(pattern)) return;
|
||||
|
||||
const key = scope.generateUidIdentifier("ref");
|
||||
node.left = t.variableDeclaration(left.kind, [
|
||||
t.variableDeclarator(key, null),
|
||||
]);
|
||||
|
||||
const nodes = [];
|
||||
|
||||
const destructuring = new DestructuringTransformer({
|
||||
kind: left.kind,
|
||||
file: file,
|
||||
scope: scope,
|
||||
nodes: nodes,
|
||||
});
|
||||
|
||||
destructuring.init(pattern, key);
|
||||
|
||||
path.ensureBlock();
|
||||
|
||||
const block = node.body;
|
||||
block.body = nodes.concat(block.body);
|
||||
},
|
||||
|
||||
CatchClause({ node, scope }, file) {
|
||||
const pattern = node.param;
|
||||
if (!t.isPattern(pattern)) return;
|
||||
|
||||
const ref = scope.generateUidIdentifier("ref");
|
||||
node.param = ref;
|
||||
|
||||
const nodes = [];
|
||||
|
||||
const destructuring = new DestructuringTransformer({
|
||||
kind: "let",
|
||||
file: file,
|
||||
scope: scope,
|
||||
nodes: nodes,
|
||||
});
|
||||
destructuring.init(pattern, ref);
|
||||
|
||||
node.body.body = nodes.concat(node.body.body);
|
||||
},
|
||||
|
||||
AssignmentExpression(path, file) {
|
||||
const { node, scope } = path;
|
||||
if (!t.isPattern(node.left)) return;
|
||||
|
||||
const nodes = [];
|
||||
|
||||
const destructuring = new DestructuringTransformer({
|
||||
operator: node.operator,
|
||||
file: file,
|
||||
scope: scope,
|
||||
nodes: nodes,
|
||||
});
|
||||
|
||||
let ref;
|
||||
if (
|
||||
path.isCompletionRecord() ||
|
||||
!path.parentPath.isExpressionStatement()
|
||||
) {
|
||||
ref = scope.generateUidIdentifierBasedOnNode(node.right, "ref");
|
||||
|
||||
nodes.push(
|
||||
t.variableDeclaration("var", [
|
||||
t.variableDeclarator(ref, node.right),
|
||||
]),
|
||||
);
|
||||
|
||||
if (t.isArrayExpression(node.right)) {
|
||||
destructuring.arrays[ref.name] = true;
|
||||
}
|
||||
}
|
||||
|
||||
destructuring.init(node.left, ref || node.right);
|
||||
|
||||
if (ref) {
|
||||
nodes.push(t.expressionStatement(ref));
|
||||
}
|
||||
|
||||
path.replaceWithMultiple(nodes);
|
||||
},
|
||||
|
||||
VariableDeclaration(path, file) {
|
||||
const { node, scope, parent } = path;
|
||||
if (t.isForXStatement(parent)) return;
|
||||
if (!parent || !path.container) return; // i don't know why this is necessary - TODO
|
||||
if (!variableDeclarationHasPattern(node)) return;
|
||||
|
||||
const nodeKind = node.kind;
|
||||
const nodes = [];
|
||||
let declar;
|
||||
|
||||
for (let i = 0; i < node.declarations.length; i++) {
|
||||
declar = node.declarations[i];
|
||||
|
||||
const patternId = declar.init;
|
||||
const pattern = declar.id;
|
||||
|
||||
const destructuring = new DestructuringTransformer({
|
||||
blockHoist: node._blockHoist,
|
||||
nodes: nodes,
|
||||
scope: scope,
|
||||
kind: node.kind,
|
||||
file: file,
|
||||
});
|
||||
|
||||
if (t.isPattern(pattern)) {
|
||||
destructuring.init(pattern, patternId);
|
||||
|
||||
if (+i !== node.declarations.length - 1) {
|
||||
// we aren't the last declarator so let's just make the
|
||||
// last transformed node inherit from us
|
||||
t.inherits(nodes[nodes.length - 1], declar);
|
||||
}
|
||||
} else {
|
||||
nodes.push(
|
||||
t.inherits(
|
||||
destructuring.buildVariableAssignment(declar.id, declar.init),
|
||||
declar,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let tail = null;
|
||||
const nodesOut = [];
|
||||
for (const node of nodes) {
|
||||
if (tail !== null && t.isVariableDeclaration(node)) {
|
||||
// Create a single compound declarations
|
||||
tail.declarations.push(...node.declarations);
|
||||
} else {
|
||||
// Make sure the original node kind is used for each compound declaration
|
||||
node.kind = nodeKind;
|
||||
nodesOut.push(node);
|
||||
tail = t.isVariableDeclaration(node) ? node : null;
|
||||
}
|
||||
}
|
||||
|
||||
// Need to unmark the current binding to this var as a param, or other hoists
|
||||
// could be placed above this ref.
|
||||
// https://github.com/babel/babel/issues/4516
|
||||
for (const nodeOut of nodesOut) {
|
||||
if (!nodeOut.declarations) continue;
|
||||
for (const declaration of nodeOut.declarations) {
|
||||
const { name } = declaration.id;
|
||||
if (scope.bindings[name]) {
|
||||
scope.bindings[name].kind = nodeOut.kind;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nodesOut.length === 1) {
|
||||
path.replaceWith(nodesOut[0]);
|
||||
} else {
|
||||
path.replaceWithMultiple(nodesOut);
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
var [a, b] = [1, 2];
|
||||
var [[a, b]] = [[1, 2]];
|
||||
var [a, b, ...c] = [1, 2, 3, 4];
|
||||
var [[a, b, ...c]] = [[1, 2, 3, 4]];
|
||||
|
||||
var [a, b] = [1, 2, 3];
|
||||
var [[a, b]] = [[1, 2, 3]];
|
||||
var [a, b] = [a, b];
|
||||
[a[0], a[1]] = [a[1], a[0]];
|
||||
var [a, b] = [...foo, bar];
|
||||
var [a, b] = [foo(), bar];
|
||||
var [a, b] = [clazz.foo(), bar];
|
||||
var [a, b] = [clazz.foo, bar];
|
||||
@@ -0,0 +1,36 @@
|
||||
var a = 1,
|
||||
b = 2;
|
||||
var a = 1,
|
||||
b = 2;
|
||||
var a = 1,
|
||||
b = 2,
|
||||
c = [3, 4];
|
||||
var a = 1,
|
||||
b = 2,
|
||||
c = [3, 4];
|
||||
var _ref = [1, 2, 3],
|
||||
a = _ref[0],
|
||||
b = _ref[1];
|
||||
var _ref2 = [1, 2, 3],
|
||||
a = _ref2[0],
|
||||
b = _ref2[1];
|
||||
var _ref3 = [a, b],
|
||||
a = _ref3[0],
|
||||
b = _ref3[1];
|
||||
var _ref4 = [a[1], a[0]];
|
||||
a[0] = _ref4[0];
|
||||
a[1] = _ref4[1];
|
||||
|
||||
var _ref5 = [].concat(babelHelpers.toConsumableArray(foo), [bar]),
|
||||
a = _ref5[0],
|
||||
b = _ref5[1];
|
||||
|
||||
var _ref6 = [foo(), bar],
|
||||
a = _ref6[0],
|
||||
b = _ref6[1];
|
||||
var _ref7 = [clazz.foo(), bar],
|
||||
a = _ref7[0],
|
||||
b = _ref7[1];
|
||||
var _ref8 = [clazz.foo, bar],
|
||||
a = _ref8[0],
|
||||
b = _ref8[1];
|
||||
3
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/array/actual.js
vendored
Normal file
3
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/array/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
var [a, [b], [c]] = ["hello", [", ", "junk"], ["world"]];
|
||||
[a, [b], [c]] = ["hello", [", ", "junk"], ["world"]];
|
||||
;
|
||||
9
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/array/expected.js
vendored
Normal file
9
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/array/expected.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
var a = "hello",
|
||||
_ref = [", ", "junk"],
|
||||
b = _ref[0],
|
||||
c = "world";
|
||||
a = "hello";
|
||||
var _ref2 = [", ", "junk"];
|
||||
b = _ref2[0];
|
||||
c = "world";
|
||||
;
|
||||
@@ -0,0 +1,2 @@
|
||||
var x, y;
|
||||
[x, y] = [1, 2];
|
||||
@@ -0,0 +1,5 @@
|
||||
var x, y;
|
||||
var _ref = [1, 2];
|
||||
x = _ref[0];
|
||||
y = _ref[1];
|
||||
_ref;
|
||||
@@ -0,0 +1,2 @@
|
||||
var z = {};
|
||||
var { x: { y } = {} } = z;
|
||||
@@ -0,0 +1,4 @@
|
||||
var z = {};
|
||||
var _z$x = z.x;
|
||||
_z$x = _z$x === void 0 ? {} : _z$x;
|
||||
var y = _z$x.y;
|
||||
@@ -0,0 +1 @@
|
||||
console.log([x] = [123]);
|
||||
@@ -0,0 +1,3 @@
|
||||
var _ref;
|
||||
|
||||
console.log((_ref = [123], x = _ref[0], _ref));
|
||||
@@ -0,0 +1,2 @@
|
||||
[a, b] = f();
|
||||
;
|
||||
@@ -0,0 +1,7 @@
|
||||
var _f = f();
|
||||
|
||||
var _f2 = babelHelpers.slicedToArray(_f, 2);
|
||||
|
||||
a = _f2[0];
|
||||
b = _f2[1];
|
||||
;
|
||||
6
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/chained/exec.js
vendored
Normal file
6
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/chained/exec.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
var a, b, c, d;
|
||||
({ a, b } = { c, d } = { a: 1, b: 2, c: 3, d: 4});
|
||||
assert.equal(a, 1);
|
||||
assert.equal(b, 2);
|
||||
assert.equal(c, 3);
|
||||
assert.equal(d, 4);
|
||||
@@ -0,0 +1,17 @@
|
||||
var f0 = function (a, b = a, c = b) {
|
||||
return [a, b, c];
|
||||
};
|
||||
|
||||
assert.deepEqual(f0(1), [1, 1, 1]);
|
||||
|
||||
var f1 = function ({a}, b = a, c = b) {
|
||||
return [a, b, c];
|
||||
};
|
||||
|
||||
assert.deepEqual(f1({a: 1}), [1, 1, 1]);
|
||||
|
||||
var f2 = function ({a}, b = a, c = a) {
|
||||
return [a, b, c];
|
||||
};
|
||||
|
||||
assert.deepEqual(f2({a: 1}), [1, 1, 1]);
|
||||
@@ -0,0 +1,17 @@
|
||||
var f0 = function (a, b = a, c = b) {
|
||||
return [a, b, c];
|
||||
};
|
||||
|
||||
assert.deepEqual(f0(1), [1, 1, 1]);
|
||||
|
||||
var f1 = function ({a}, b = a, c = b) {
|
||||
return [a, b, c];
|
||||
};
|
||||
|
||||
assert.deepEqual(f1({a: 1}), [1, 1, 1]);
|
||||
|
||||
var f2 = function ({a}, b = a, c = a) {
|
||||
return [a, b, c];
|
||||
};
|
||||
|
||||
assert.deepEqual(f2({a: 1}), [1, 1, 1]);
|
||||
@@ -0,0 +1,29 @@
|
||||
var f0 = function (a) {
|
||||
var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : a;
|
||||
var c = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : b;
|
||||
return [a, b, c];
|
||||
};
|
||||
|
||||
assert.deepEqual(f0(1), [1, 1, 1]);
|
||||
|
||||
var f1 = function (_ref) {
|
||||
var a = _ref.a;
|
||||
var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : a;
|
||||
var c = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : b;
|
||||
return [a, b, c];
|
||||
};
|
||||
|
||||
assert.deepEqual(f1({
|
||||
a: 1
|
||||
}), [1, 1, 1]);
|
||||
|
||||
var f2 = function (_ref2) {
|
||||
var a = _ref2.a;
|
||||
var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : a;
|
||||
var c = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : a;
|
||||
return [a, b, c];
|
||||
};
|
||||
|
||||
assert.deepEqual(f2({
|
||||
a: 1
|
||||
}), [1, 1, 1]);
|
||||
@@ -0,0 +1 @@
|
||||
var {} = null;
|
||||
@@ -0,0 +1,3 @@
|
||||
assert.throws(function () {
|
||||
var {} = null;
|
||||
}, /Cannot destructure undefined/);
|
||||
@@ -0,0 +1,2 @@
|
||||
var _ref = null;
|
||||
babelHelpers.objectDestructuringEmpty(_ref);
|
||||
1
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/empty/actual.js
vendored
Normal file
1
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/empty/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var [, a, [b], [c], d] = ["foo", "hello", [", ", "junk"], ["world"]];
|
||||
7
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/empty/expected.js
vendored
Normal file
7
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/empty/expected.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
var _ref = ["foo", "hello", [", ", "junk"], ["world"]],
|
||||
a = _ref[1],
|
||||
_ref$ = babelHelpers.slicedToArray(_ref[2], 1),
|
||||
b = _ref$[0],
|
||||
_ref$2 = babelHelpers.slicedToArray(_ref[3], 1),
|
||||
c = _ref$2[0],
|
||||
d = _ref[4];
|
||||
@@ -0,0 +1,7 @@
|
||||
var z = {};
|
||||
var { ...x } = z;
|
||||
var { x, ...y } = z;
|
||||
var { [x]: x, ...y } = z;
|
||||
(function({ x, ...y }) { });
|
||||
|
||||
({ x, y, ...z } = o);
|
||||
@@ -0,0 +1,20 @@
|
||||
var z = {};
|
||||
var _z = z,
|
||||
x = babelHelpers.objectWithoutProperties(_z, []);
|
||||
var _z2 = z,
|
||||
x = _z2.x,
|
||||
y = babelHelpers.objectWithoutProperties(_z2, ["x"]);
|
||||
var _z3 = z,
|
||||
x = _z3[x],
|
||||
y = babelHelpers.objectWithoutProperties(_z3, [x]);
|
||||
|
||||
(function (_ref) {
|
||||
var x = _ref.x,
|
||||
y = babelHelpers.objectWithoutProperties(_ref, ["x"]);
|
||||
});
|
||||
|
||||
var _o = o;
|
||||
x = _o.x;
|
||||
y = _o.y;
|
||||
z = babelHelpers.objectWithoutProperties(_o, ["x", "y"]);
|
||||
_o;
|
||||
@@ -0,0 +1,2 @@
|
||||
|
||||
export let {a, b, c: {d, e: {f = 4}}} = {};
|
||||
@@ -0,0 +1,8 @@
|
||||
var _ref = {},
|
||||
a = _ref.a,
|
||||
b = _ref.b,
|
||||
_ref$c = _ref.c,
|
||||
d = _ref$c.d,
|
||||
_ref$c$e$f = _ref$c.e.f,
|
||||
f = _ref$c$e$f === void 0 ? 4 : _ref$c$e$f;
|
||||
export { a, b, d, f };
|
||||
3
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-in/actual.js
vendored
Normal file
3
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-in/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
for (var [name, value] in obj) {
|
||||
print("Name: " + name + ", Value: " + value);
|
||||
}
|
||||
7
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-in/expected.js
vendored
Normal file
7
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-in/expected.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
for (var _ref in obj) {
|
||||
var _ref2 = babelHelpers.slicedToArray(_ref, 2);
|
||||
|
||||
var name = _ref2[0];
|
||||
var value = _ref2[1];
|
||||
print("Name: " + name + ", Value: " + value);
|
||||
}
|
||||
2
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-let/actual.js
vendored
Normal file
2
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-let/actual.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
for (let [ i, n ] = range; ; ) {
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
for (var _range = range, _range2 = babelHelpers.slicedToArray(_range, 2), i = _range2[0], n = _range2[1];;) {}
|
||||
3
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of/actual.js
vendored
Normal file
3
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
for (var [ name, before, after ] of test.expectation.registers) {
|
||||
|
||||
}
|
||||
7
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of/expected.js
vendored
Normal file
7
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of/expected.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
for (var _ref of test.expectation.registers) {
|
||||
var _ref2 = babelHelpers.slicedToArray(_ref, 3);
|
||||
|
||||
var name = _ref2[0];
|
||||
var before = _ref2[1];
|
||||
var after = _ref2[2];
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
let list = [1, 2, 3, 4];
|
||||
for (let i = 0, { length } = list; i < length; i++) {
|
||||
list[i];
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
var list = [1, 2, 3, 4];
|
||||
|
||||
for (var i = 0, length = list.length; i < length; i++) {
|
||||
list[i];
|
||||
}
|
||||
9
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/issue-5090/exec.js
vendored
Normal file
9
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/issue-5090/exec.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
const assign = function([...arr], index, value) {
|
||||
arr[index] = value;
|
||||
return arr;
|
||||
}
|
||||
|
||||
const arr = [1, 2, 3];
|
||||
assign(arr, 1, 42);
|
||||
|
||||
assert.deepEqual(arr, [1, 2, 3]);
|
||||
@@ -0,0 +1,6 @@
|
||||
(function () {
|
||||
let q;
|
||||
let w;
|
||||
let e;
|
||||
if (true) [q, w, e] = [1, 2, 3].map(()=>123);
|
||||
})();
|
||||
17
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/issue-5628/expected.js
vendored
Normal file
17
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/issue-5628/expected.js
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
(function () {
|
||||
var q;
|
||||
var w;
|
||||
var e;
|
||||
|
||||
if (true) {
|
||||
var _map = [1, 2, 3].map(function () {
|
||||
return 123;
|
||||
});
|
||||
|
||||
var _map2 = babelHelpers.slicedToArray(_map, 3);
|
||||
|
||||
q = _map2[0];
|
||||
w = _map2[1];
|
||||
e = _map2[2];
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["external-helpers", "transform-arrow-functions", "transform-destructuring", "transform-spread", "transform-parameters", "transform-block-scoping", "proposal-object-rest-spread", "transform-regenerator"]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
if (true) [a, b] = [b, a];
|
||||
@@ -0,0 +1,6 @@
|
||||
if (true) {
|
||||
var _ref = [b, a];
|
||||
a = _ref[0];
|
||||
b = _ref[1];
|
||||
_ref;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import { NestedObjects } from "./some-module"
|
||||
|
||||
const { Foo, Bar } = NestedObjects
|
||||
@@ -0,0 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
var _someModule = require("./some-module");
|
||||
|
||||
const Foo = _someModule.NestedObjects.Foo,
|
||||
Bar = _someModule.NestedObjects.Bar;
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-destructuring", "transform-modules-commonjs"]
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
var z = [];
|
||||
var [x, ...y] = z;
|
||||
@@ -0,0 +1,3 @@
|
||||
var z = [];
|
||||
var x = z[0],
|
||||
y = z.slice(1);
|
||||
@@ -0,0 +1,2 @@
|
||||
[foo.foo, foo.bar] = [1, 2];
|
||||
;
|
||||
@@ -0,0 +1,4 @@
|
||||
var _ref = [1, 2];
|
||||
foo.foo = _ref[0];
|
||||
foo.bar = _ref[1];
|
||||
;
|
||||
2
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/mixed/actual.js
vendored
Normal file
2
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/mixed/actual.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
var rect = {};
|
||||
var {topLeft: [x1, y1], bottomRight: [x2, y2] } = rect;
|
||||
8
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/mixed/expected.js
vendored
Normal file
8
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/mixed/expected.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
var rect = {};
|
||||
|
||||
var _rect$topLeft = babelHelpers.slicedToArray(rect.topLeft, 2),
|
||||
x1 = _rect$topLeft[0],
|
||||
y1 = _rect$topLeft[1],
|
||||
_rect$bottomRight = babelHelpers.slicedToArray(rect.bottomRight, 2),
|
||||
x2 = _rect$bottomRight[0],
|
||||
y2 = _rect$bottomRight[1];
|
||||
3
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/multiple/actual.js
vendored
Normal file
3
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/multiple/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
var coords = [1, 2];
|
||||
var { x, y } = coords,
|
||||
foo = "bar";
|
||||
@@ -0,0 +1,4 @@
|
||||
var coords = [1, 2];
|
||||
var x = coords.x,
|
||||
y = coords.y,
|
||||
foo = "bar";
|
||||
@@ -0,0 +1,3 @@
|
||||
var rect = {};
|
||||
var {topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}} = rect;
|
||||
var { 3: foo, 5: bar } = [0, 1, 2, 3, 4, 5, 6];
|
||||
@@ -0,0 +1,10 @@
|
||||
var rect = {};
|
||||
var _rect$topLeft = rect.topLeft,
|
||||
x1 = _rect$topLeft.x,
|
||||
y1 = _rect$topLeft.y,
|
||||
_rect$bottomRight = rect.bottomRight,
|
||||
x2 = _rect$bottomRight.x,
|
||||
y2 = _rect$bottomRight.y;
|
||||
var _ref = [0, 1, 2, 3, 4, 5, 6],
|
||||
foo = _ref[3],
|
||||
bar = _ref[5];
|
||||
@@ -0,0 +1,2 @@
|
||||
var coords = [1, 2];
|
||||
var { x, y } = coords;
|
||||
@@ -0,0 +1,3 @@
|
||||
var coords = [1, 2];
|
||||
var x = coords.x,
|
||||
y = coords.y;
|
||||
3
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/options.json
vendored
Normal file
3
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["external-helpers", "transform-destructuring", "transform-spread", "transform-parameters", "transform-block-scoping", "proposal-object-rest-spread", "transform-regenerator"]
|
||||
}
|
||||
15
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/parameters/actual.js
vendored
Normal file
15
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/parameters/actual.js
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
function somethingAdvanced({topLeft: {x: x1, y: y1} = {}, bottomRight: {x: x2, y: y2} = {}}, p2, p3){
|
||||
|
||||
}
|
||||
|
||||
function unpackObject({title: title, author: author}) {
|
||||
return title + " " + author;
|
||||
}
|
||||
|
||||
console.log(unpackObject({title: "title", author: "author"}));
|
||||
|
||||
var unpackArray = function ([a, b, c], [x, y, z]) {
|
||||
return a+b+c;
|
||||
};
|
||||
|
||||
console.log(unpackArray(["hello", ", ", "world"], [1, 2, 3]));
|
||||
37
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/parameters/expected.js
vendored
Normal file
37
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/parameters/expected.js
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
function somethingAdvanced(_ref, p2, p3) {
|
||||
var _ref$topLeft = _ref.topLeft;
|
||||
_ref$topLeft = _ref$topLeft === void 0 ? {} : _ref$topLeft;
|
||||
var x1 = _ref$topLeft.x,
|
||||
y1 = _ref$topLeft.y,
|
||||
_ref$bottomRight = _ref.bottomRight;
|
||||
_ref$bottomRight = _ref$bottomRight === void 0 ? {} : _ref$bottomRight;
|
||||
var x2 = _ref$bottomRight.x,
|
||||
y2 = _ref$bottomRight.y;
|
||||
}
|
||||
|
||||
function unpackObject(_ref2) {
|
||||
var title = _ref2.title,
|
||||
author = _ref2.author;
|
||||
return title + " " + author;
|
||||
}
|
||||
|
||||
console.log(unpackObject({
|
||||
title: "title",
|
||||
author: "author"
|
||||
}));
|
||||
|
||||
var unpackArray = function (_ref3, _ref4) {
|
||||
var _ref5 = babelHelpers.slicedToArray(_ref3, 3),
|
||||
a = _ref5[0],
|
||||
b = _ref5[1],
|
||||
c = _ref5[2];
|
||||
|
||||
var _ref6 = babelHelpers.slicedToArray(_ref4, 3),
|
||||
x = _ref6[0],
|
||||
y = _ref6[1],
|
||||
z = _ref6[2];
|
||||
|
||||
return a + b + c;
|
||||
};
|
||||
|
||||
console.log(unpackArray(["hello", ", ", "world"], [1, 2, 3]));
|
||||
@@ -0,0 +1,7 @@
|
||||
function* f() {
|
||||
for (var i = 0; i < 3; i++) {
|
||||
yield i;
|
||||
}
|
||||
}
|
||||
var [...xs] = f();
|
||||
assert.deepEqual(xs, [0, 1, 2]);
|
||||
5
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/spread/actual.js
vendored
Normal file
5
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/spread/actual.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
function isSorted([x, y, ...wow]) {
|
||||
if (!zs.length) return true
|
||||
if (y > x) return isSorted(zs)
|
||||
return false
|
||||
}
|
||||
10
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/spread/expected.js
vendored
Normal file
10
packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/spread/expected.js
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
function isSorted(_ref) {
|
||||
var _ref2 = babelHelpers.toArray(_ref),
|
||||
x = _ref2[0],
|
||||
y = _ref2[1],
|
||||
wow = _ref2.slice(2);
|
||||
|
||||
if (!zs.length) return true;
|
||||
if (y > x) return isSorted(zs);
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import runner from "@babel/helper-plugin-test-runner";
|
||||
|
||||
runner(__dirname);
|
||||
Reference in New Issue
Block a user