better isParenthesizedExpression support for destructuring
This commit is contained in:
@@ -11,7 +11,7 @@ var buildVariableAssign = function (kind, id, init) {
|
||||
}
|
||||
};
|
||||
|
||||
var get = function (node) {
|
||||
var normalise = function (node) {
|
||||
if (t.isParenthesizedExpression(node)) {
|
||||
return node.expression;
|
||||
} else {
|
||||
@@ -19,8 +19,12 @@ var get = function (node) {
|
||||
}
|
||||
};
|
||||
|
||||
var isPattern = function (node) {
|
||||
return t.isPattern(normalise(node));
|
||||
};
|
||||
|
||||
var push = function (kind, nodes, elem, parentId) {
|
||||
elem = get(elem);
|
||||
elem = normalise(elem);
|
||||
|
||||
if (t.isObjectPattern(elem)) {
|
||||
pushObjectPattern(kind, nodes, elem, parentId);
|
||||
@@ -35,10 +39,10 @@ var push = function (kind, nodes, elem, parentId) {
|
||||
|
||||
var pushObjectPattern = function (kind, nodes, pattern, parentId) {
|
||||
_.each(pattern.properties, function (prop) {
|
||||
var pattern2 = get(prop.value);
|
||||
var pattern2 = prop.value;
|
||||
var patternId2 = t.memberExpression(parentId, prop.key);
|
||||
|
||||
if (t.isPattern(pattern2)) {
|
||||
if (isPattern(pattern2)) {
|
||||
push(kind, nodes, pattern2, patternId2);
|
||||
} else {
|
||||
nodes.push(buildVariableAssign(kind, pattern2, patternId2));
|
||||
@@ -89,8 +93,8 @@ exports.ForOfStatement = function (node, parent, file, scope) {
|
||||
var declar = node.left;
|
||||
if (!t.isVariableDeclaration(declar)) return;
|
||||
|
||||
var pattern = get(declar.declarations[0].id);
|
||||
if (!t.isPattern(pattern)) return;
|
||||
var pattern = declar.declarations[0].id;
|
||||
if (!isPattern(pattern)) return;
|
||||
|
||||
var key = t.identifier(file.generateUid("ref", scope));
|
||||
node.left = t.variableDeclaration(declar.kind, [
|
||||
@@ -113,7 +117,7 @@ exports.Function = function (node, parent, file, scope) {
|
||||
var hasDestructuring = false;
|
||||
|
||||
node.params = node.params.map(function (pattern) {
|
||||
if (!t.isPattern(get(pattern))) return pattern;
|
||||
if (!isPattern(pattern)) return pattern;
|
||||
|
||||
hasDestructuring = true;
|
||||
var parentId = t.identifier(file.generateUid("ref", scope));
|
||||
@@ -142,8 +146,7 @@ exports.ExpressionStatement = function (node, parent, file, scope) {
|
||||
var expr = node.expression;
|
||||
if (expr.type !== "AssignmentExpression") return;
|
||||
|
||||
var left = get(expr.left);
|
||||
if (!t.isPattern(left)) return;
|
||||
if (!isPattern(expr.left)) return;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
@@ -152,7 +155,7 @@ exports.ExpressionStatement = function (node, parent, file, scope) {
|
||||
t.variableDeclarator(ref, expr.right)
|
||||
]));
|
||||
|
||||
push(false, nodes, left, ref);
|
||||
push(false, nodes, expr.left, ref);
|
||||
|
||||
return nodes;
|
||||
};
|
||||
@@ -164,7 +167,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
|
||||
var hasPattern = false;
|
||||
_.each(node.declarations, function (declar) {
|
||||
if (t.isPattern(get(declar.id))) {
|
||||
if (isPattern(declar.id)) {
|
||||
hasPattern = true;
|
||||
return false;
|
||||
}
|
||||
@@ -173,7 +176,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
|
||||
_.each(node.declarations, function (declar) {
|
||||
var patternId = declar.init;
|
||||
var pattern = get(declar.id);
|
||||
var pattern = declar.id;
|
||||
if (t.isPattern(pattern) && patternId) {
|
||||
pushPattern({
|
||||
kind: node.kind,
|
||||
|
||||
Reference in New Issue
Block a user