add destructuring support

This commit is contained in:
Sebastian McKenzie
2014-09-29 18:29:08 +10:00
parent dbf25a82ee
commit 0e19006641
15 changed files with 140 additions and 4 deletions

View File

@@ -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"]];

View File

@@ -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

View File

@@ -0,0 +1 @@
var KEY = VALUE;

View 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;
};

View File

@@ -0,0 +1 @@
var [a, [b], [c], d] = ["hello", [", ", "junk"], ["world"]];

View 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];

View File

@@ -0,0 +1 @@
var {topLeft: [x1, y1], bottomRight: [x2, y2] } = rect;

View 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];

View File

@@ -0,0 +1 @@
var { x, y } = coords, foo = "bar";

View File

@@ -0,0 +1,3 @@
var x = coords.x;
var y = coords.y;
var foo = "bar";

View File

@@ -0,0 +1 @@
var {topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}} = rect;

View 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;

View File

@@ -0,0 +1 @@
var { x, y } = coords;

View File

@@ -0,0 +1,2 @@
var x = coords.x;
var y = coords.y;