make destructuring in catch clauses block scoped and add support for non-variable destructuring in for-in/of heads

This commit is contained in:
Sebastian McKenzie 2015-02-08 14:33:55 +11:00
parent 4c8e6481b6
commit e42d5a889e

View File

@ -179,21 +179,40 @@ Destructuring.prototype.init = function (pattern, parentId) {
exports.ForInStatement = exports.ForInStatement =
exports.ForOfStatement = function (node, parent, scope, file) { exports.ForOfStatement = function (node, parent, scope, file) {
var declar = node.left; var left = node.left;
if (!t.isVariableDeclaration(declar)) return;
var pattern = declar.declarations[0].id; if (t.isPattern(left)) {
// for ({ length: k } in { abc: 3 });
var temp = scope.generateUidIdentifier("ref");
node.left = t.variableDeclaration("var", [
t.variableDeclarator(temp)
]);
t.ensureBlock(node);
node.body.body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(left, temp)
]));
return;
}
if (!t.isVariableDeclaration(left)) return;
var pattern = left.declarations[0].id;
if (!t.isPattern(pattern)) return; if (!t.isPattern(pattern)) return;
var key = scope.generateUidIdentifier("ref"); var key = scope.generateUidIdentifier("ref");
node.left = t.variableDeclaration(declar.kind, [ node.left = t.variableDeclaration(left.kind, [
t.variableDeclarator(key, null) t.variableDeclarator(key, null)
]); ]);
var nodes = []; var nodes = [];
var destructuring = new Destructuring({ var destructuring = new Destructuring({
kind: declar.kind, kind: left.kind,
file: file, file: file,
scope: scope, scope: scope,
nodes: nodes nodes: nodes
@ -248,7 +267,7 @@ exports.CatchClause = function (node, parent, scope, file) {
var nodes = []; var nodes = [];
var destructuring = new Destructuring({ var destructuring = new Destructuring({
kind: "var", kind: "let",
file: file, file: file,
scope: scope, scope: scope,
nodes: nodes nodes: nodes
@ -256,6 +275,8 @@ exports.CatchClause = function (node, parent, scope, file) {
destructuring.init(pattern, ref); destructuring.init(pattern, ref);
node.body.body = nodes.concat(node.body.body); node.body.body = nodes.concat(node.body.body);
return node;
}; };
exports.ExpressionStatement = function (node, parent, scope, file) { exports.ExpressionStatement = function (node, parent, scope, file) {