fix destructuring to support ParanthesizedExpression
This commit is contained in:
parent
ab2f652bdf
commit
a47723c66c
@ -11,7 +11,17 @@ var buildVariableAssign = function (kind, id, init) {
|
||||
}
|
||||
};
|
||||
|
||||
var get = function (node) {
|
||||
if (t.isParenthesizedExpression(node)) {
|
||||
return node.expression;
|
||||
} else {
|
||||
return node;
|
||||
}
|
||||
};
|
||||
|
||||
var push = function (kind, nodes, elem, parentId) {
|
||||
elem = get(elem);
|
||||
|
||||
if (t.isObjectPattern(elem)) {
|
||||
pushObjectPattern(kind, nodes, elem, parentId);
|
||||
} else if (t.isArrayPattern(elem)) {
|
||||
@ -25,7 +35,7 @@ var push = function (kind, nodes, elem, parentId) {
|
||||
|
||||
var pushObjectPattern = function (kind, nodes, pattern, parentId) {
|
||||
_.each(pattern.properties, function (prop) {
|
||||
var pattern2 = prop.value;
|
||||
var pattern2 = get(prop.value);
|
||||
var patternId2 = t.memberExpression(parentId, prop.key);
|
||||
|
||||
if (t.isPattern(pattern2)) {
|
||||
@ -79,7 +89,7 @@ exports.ForOfStatement = function (node, parent, file, scope) {
|
||||
var declar = node.left;
|
||||
if (!t.isVariableDeclaration(declar)) return;
|
||||
|
||||
var pattern = declar.declarations[0].id;
|
||||
var pattern = get(declar.declarations[0].id);
|
||||
if (!t.isPattern(pattern)) return;
|
||||
|
||||
var key = t.identifier(file.generateUid("ref", scope));
|
||||
@ -103,7 +113,7 @@ exports.Function = function (node, parent, file, scope) {
|
||||
var hasDestructuring = false;
|
||||
|
||||
node.params = node.params.map(function (pattern) {
|
||||
if (!t.isPattern(pattern)) return pattern;
|
||||
if (!t.isPattern(get(pattern))) return pattern;
|
||||
|
||||
hasDestructuring = true;
|
||||
var parentId = t.identifier(file.generateUid("ref", scope));
|
||||
@ -132,7 +142,8 @@ exports.ExpressionStatement = function (node, parent, file, scope) {
|
||||
var expr = node.expression;
|
||||
if (expr.type !== "AssignmentExpression") return;
|
||||
|
||||
if (!t.isPattern(expr.left)) return;
|
||||
var left = get(expr.left);
|
||||
if (!t.isPattern(left)) return;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
@ -141,7 +152,7 @@ exports.ExpressionStatement = function (node, parent, file, scope) {
|
||||
t.variableDeclarator(ref, expr.right)
|
||||
]));
|
||||
|
||||
push(false, nodes, expr.left, ref);
|
||||
push(false, nodes, left, ref);
|
||||
|
||||
return nodes;
|
||||
};
|
||||
@ -153,7 +164,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
|
||||
var hasPattern = false;
|
||||
_.each(node.declarations, function (declar) {
|
||||
if (t.isPattern(declar.id)) {
|
||||
if (t.isPattern(get(declar.id))) {
|
||||
hasPattern = true;
|
||||
return false;
|
||||
}
|
||||
@ -162,7 +173,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
|
||||
_.each(node.declarations, function (declar) {
|
||||
var patternId = declar.init;
|
||||
var pattern = declar.id;
|
||||
var pattern = get(declar.id);
|
||||
if (t.isPattern(pattern) && patternId) {
|
||||
pushPattern({
|
||||
kind: node.kind,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user