add destructuring support
This commit is contained in:
@@ -98,7 +98,7 @@ console.log(`${x} + ${y} = ${x + y}`); // "5 + 10 = 15"
|
||||
0o767 === 503; // true
|
||||
```
|
||||
|
||||
## Iterators
|
||||
## For Of
|
||||
|
||||
```javascript
|
||||
```
|
||||
@@ -134,7 +134,7 @@ function f(x, y) {
|
||||
[for (i of [1, 2, 3]) i * i]; // [1, 4, 9]
|
||||
```
|
||||
|
||||
## Destructuring assignment
|
||||
## Destructuring
|
||||
|
||||
```javascript
|
||||
var [a, [b], c, d] = ["hello", [", ", "junk"], ["world"]];
|
||||
|
||||
@@ -45,9 +45,9 @@
|
||||
| [Computed property names](FEATURES.md#computed-property-names) | ✓ | |
|
||||
| [Constants](FEATURES.md#constants) | ✓ | |
|
||||
| [Binary and Octal Literals](FEATURES.md#binary-and-octal-literals) | ✓ | |
|
||||
| [Iterators](FEATURES.md#iterators) | ✓ | ✓ |
|
||||
| [For of](FEATURES.md#for-of) | ✓ | ✓ |
|
||||
| [Array comprehension](FEATURES.md#array-comprehension) | ✓ | |
|
||||
| [Destructuring assignment](FEATURES.md#destructuring-assignment) | | |
|
||||
| [Destructuring](FEATURES.md#destructuring) | ✓ | |
|
||||
| [Generators](FEATURES.md#generators) | | |
|
||||
|
||||
## Installation
|
||||
|
||||
1
lib/6to5/templates/variable-assign.js
Normal file
1
lib/6to5/templates/variable-assign.js
Normal file
@@ -0,0 +1 @@
|
||||
var KEY = VALUE;
|
||||
112
lib/6to5/transformers/destructuring.js
Normal file
112
lib/6to5/transformers/destructuring.js
Normal file
@@ -0,0 +1,112 @@
|
||||
var util = require("../util");
|
||||
var _ = require("lodash");
|
||||
|
||||
var isPattern = function (id) {
|
||||
return id.type === "ArrayPattern" || id.type === "ObjectPattern";
|
||||
};
|
||||
|
||||
exports.VariableDeclaration = function (node, parent, opts, generateUid) {
|
||||
var nodes = [];
|
||||
|
||||
var hasPattern = false;
|
||||
_.each(node.declarations, function (declar) {
|
||||
if (isPattern(declar.id)) {
|
||||
hasPattern = true;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
if (!hasPattern) return;
|
||||
|
||||
//
|
||||
|
||||
var buildVariableAssign = function (id, init) {
|
||||
return {
|
||||
type: "VariableDeclaration",
|
||||
kind: node.kind,
|
||||
declarations: [{
|
||||
type: "VariableDeclarator",
|
||||
id: id,
|
||||
init: init
|
||||
}]
|
||||
};
|
||||
};
|
||||
|
||||
var push = function (pattern, parentId) {
|
||||
if (pattern.type === "ObjectPattern") {
|
||||
pushObjectPattern(pattern, parentId);
|
||||
} else if (pattern.type === "ArrayPattern") {
|
||||
pushArrayPattern(pattern, parentId);
|
||||
}
|
||||
};
|
||||
|
||||
var pushObjectPattern = function (pattern, parentId) {
|
||||
_.each(pattern.properties, function (prop) {
|
||||
var id = prop.value;
|
||||
|
||||
var init = {
|
||||
type: "MemberExpression",
|
||||
object: parentId,
|
||||
property: prop.key
|
||||
};
|
||||
|
||||
if (isPattern(id)) {
|
||||
push(id, init);
|
||||
} else {
|
||||
nodes.push(buildVariableAssign(id, init));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var pushArrayPattern = function (pattern, parentId) {
|
||||
_.each(pattern.elements, function (id, i) {
|
||||
var init = {
|
||||
type: "MemberExpression",
|
||||
computed: true,
|
||||
object: parentId,
|
||||
property: {
|
||||
type: "Literal",
|
||||
value: i
|
||||
}
|
||||
};
|
||||
|
||||
if (id.type === "Identifier") {
|
||||
nodes.push(buildVariableAssign(id, init));
|
||||
} else {
|
||||
push(id, init);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var pushPattern = function (id, init) {
|
||||
if (init.type !== "MemberExpression" && init.type !== "Identifier") {
|
||||
var key = generateUid("ref");
|
||||
|
||||
nodes.push(util.template("variable-assign", {
|
||||
KEY: key,
|
||||
VALUE: init
|
||||
}, true));
|
||||
|
||||
init = {
|
||||
type: "Identifier",
|
||||
name: key
|
||||
};
|
||||
}
|
||||
|
||||
push(id, init);
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
_.each(node.declarations, function (declar) {
|
||||
var init = declar.init;
|
||||
var id = declar.id;
|
||||
|
||||
if (isPattern(id)) {
|
||||
pushPattern(id, init);
|
||||
} else {
|
||||
nodes.push(buildVariableAssign(id, init));
|
||||
}
|
||||
});
|
||||
|
||||
return nodes;
|
||||
};
|
||||
1
test/fixtures/destructuring/array/actual.js
vendored
Normal file
1
test/fixtures/destructuring/array/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var [a, [b], [c], d] = ["hello", [", ", "junk"], ["world"]];
|
||||
5
test/fixtures/destructuring/array/expected.js
vendored
Normal file
5
test/fixtures/destructuring/array/expected.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
var _ref = ["hello", [", ", "junk"], ["world"]];
|
||||
var a = _ref[0];
|
||||
var b = _ref[1][0];
|
||||
var c = _ref[2][0];
|
||||
var d = _ref[3];
|
||||
1
test/fixtures/destructuring/mixed/actual.js
vendored
Normal file
1
test/fixtures/destructuring/mixed/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var {topLeft: [x1, y1], bottomRight: [x2, y2] } = rect;
|
||||
4
test/fixtures/destructuring/mixed/expected.js
vendored
Normal file
4
test/fixtures/destructuring/mixed/expected.js
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
var x1 = rect.topLeft[0];
|
||||
var y1 = rect.topLeft[1];
|
||||
var x2 = rect.bottomRight[0];
|
||||
var y2 = rect.bottomRight[1];
|
||||
1
test/fixtures/destructuring/multiple/actual.js
vendored
Normal file
1
test/fixtures/destructuring/multiple/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var { x, y } = coords, foo = "bar";
|
||||
3
test/fixtures/destructuring/multiple/expected.js
vendored
Normal file
3
test/fixtures/destructuring/multiple/expected.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
var x = coords.x;
|
||||
var y = coords.y;
|
||||
var foo = "bar";
|
||||
1
test/fixtures/destructuring/object-advanced/actual.js
vendored
Normal file
1
test/fixtures/destructuring/object-advanced/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var {topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}} = rect;
|
||||
4
test/fixtures/destructuring/object-advanced/expected.js
vendored
Normal file
4
test/fixtures/destructuring/object-advanced/expected.js
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
var x1 = rect.topLeft.x;
|
||||
var y1 = rect.topLeft.y;
|
||||
var x2 = rect.bottomRight.x;
|
||||
var y2 = rect.bottomRight.y;
|
||||
1
test/fixtures/destructuring/object-basic/actual.js
vendored
Normal file
1
test/fixtures/destructuring/object-basic/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var { x, y } = coords;
|
||||
2
test/fixtures/destructuring/object-basic/expected.js
vendored
Normal file
2
test/fixtures/destructuring/object-basic/expected.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
var x = coords.x;
|
||||
var y = coords.y;
|
||||
Reference in New Issue
Block a user