add support for AssignmentExpression destructuring outside of ExpressionStatement
This commit is contained in:
parent
f1759dc419
commit
04bd023787
@ -1,3 +1,5 @@
|
||||
// TODO: Clean up
|
||||
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
@ -183,10 +185,38 @@ exports.ExpressionStatement = function (node, parent, file, scope) {
|
||||
return nodes;
|
||||
};
|
||||
|
||||
exports.AssignmentExpression = function (node, parent, file) {
|
||||
exports.AssignmentExpression = function (node, parent, file, scope) {
|
||||
if (parent.type === "ExpressionStatement") return;
|
||||
if (!t.isPattern(node.left)) return;
|
||||
throw file.errorWithNode(node, "AssignmentExpression destructuring outside of a ExpressionStatement is forbidden due to current 6to5 limitations");
|
||||
|
||||
var tempName = file.generateUid("temp", scope);
|
||||
var temp = t.identifier(tempName);
|
||||
scope.push(tempName, temp);
|
||||
|
||||
var nodes = [];
|
||||
nodes.push(t.assignmentExpression("=", temp, node.right));
|
||||
|
||||
push({
|
||||
kind: false,
|
||||
file: file,
|
||||
scope: scope
|
||||
}, nodes, node.left, temp);
|
||||
|
||||
nodes.push(temp);
|
||||
|
||||
nodes = nodes.map(function (node) {
|
||||
if (t.isExpressionStatement(node)) {
|
||||
return node.expression;
|
||||
} else if (t.isVariableDeclaration(node)) {
|
||||
var declar = node.declarations[0];
|
||||
scope.push(declar.id.name, declar.id);
|
||||
return t.assignmentExpression("=", declar.id, declar.init);
|
||||
} else {
|
||||
return node;
|
||||
}
|
||||
});
|
||||
|
||||
return t.sequenceExpression(nodes);
|
||||
};
|
||||
|
||||
exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
|
||||
@ -1 +0,0 @@
|
||||
console.log([x] = [1]);
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"throws": "AssignmentExpression destructuring outside of a ExpressionStatement is forbidden due to current 6to5 limitations"
|
||||
}
|
||||
2
test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js
vendored
Normal file
2
test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
var _temp;
|
||||
console.log((_temp = [1, 2, 3], x = _temp[0], _temp));
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"throws": "AssignmentExpression destructuring outside of a ExpressionStatement is forbidden due to current 6to5 limitations"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user