more support to t.getIds and add t.needsWhitespaceBefore method
This commit is contained in:
@@ -107,6 +107,8 @@ t.getIds = function (node, map) {
|
||||
_.each(id.elements, function (elem) {
|
||||
search.push(elem);
|
||||
});
|
||||
} else if (t.isMemberExpression(id)) {
|
||||
search.push(id.object);
|
||||
} else if (t.isAssignmentExpression(id)) {
|
||||
search.push(id.left);
|
||||
} else if (t.isObjectPattern(id)) {
|
||||
@@ -115,7 +117,7 @@ t.getIds = function (node, map) {
|
||||
});
|
||||
} else if (t.isVariableDeclaration(id)) {
|
||||
search = search.concat(id.declarations);
|
||||
} else if (t.isVariableDeclarator(id)) {
|
||||
} else if (t.isVariableDeclarator(id) || t.isFunctionDeclaration(id) || t.isClassDeclaration(id)) {
|
||||
search.push(id.id);
|
||||
} else if (t.isSpreadElement(id)) {
|
||||
search.push(id.argument);
|
||||
@@ -140,14 +142,22 @@ t.getSpecifierName = function (specifier) {
|
||||
return specifier.name || specifier.id;
|
||||
};
|
||||
|
||||
t.needsWhitespace = function (node, expression) {
|
||||
if (t.isExpressionStatement(node)) {
|
||||
t.needsWhitespaceBefore = function (node, statement) {
|
||||
if (t.isFunction(node) || t.isClass(node) || t.isFor(node) || t.isSwitchStatement(node) || t.isIfStatement(node) || t.isProperty(node)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
t.needsWhitespaceAfter = function (node, statement) {
|
||||
if (statement && t.isExpressionStatement(node)) {
|
||||
node = node.expression;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
if (t.isFunction(node) || t.isClass(node) || t.isFor(node) || t.isSwitchStatement(node)) {
|
||||
if (t.isFunction(node) || t.isClass(node) || t.isFor(node) || t.isSwitchStatement(node) || t.isProperty(node)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -159,7 +169,7 @@ t.needsWhitespace = function (node, expression) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (expression) return false;
|
||||
if (!statement) return false;
|
||||
|
||||
//
|
||||
|
||||
@@ -187,7 +197,7 @@ t.needsWhitespace = function (node, expression) {
|
||||
}
|
||||
|
||||
return exprs.some(function (expr) {
|
||||
return t.needsWhitespace(expr, true);
|
||||
return t.needsWhitespaceAfter(expr);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -232,6 +242,18 @@ t.needsParans = function (node, parent) {
|
||||
}
|
||||
}
|
||||
|
||||
if (t.isBinaryExpression(node) && node.operator === "in") {
|
||||
// var i = (1 in []);
|
||||
if (t.isVariableDeclarator(parent)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// for ((1 in []);;);
|
||||
if (t.isFor(parent)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// (class {});
|
||||
if (t.isClassExpression(node) && t.isExpressionStatement(parent)) {
|
||||
return true;
|
||||
@@ -267,7 +289,7 @@ t.needsParans = function (node, parent) {
|
||||
}
|
||||
|
||||
if (t.isNewExpression(parent) && parent.callee === node) {
|
||||
return traverse.hasType(node, "CallExpression");
|
||||
return t.isCallExpression(node) || _.some(node, t.isCallExpression);
|
||||
}
|
||||
|
||||
// (1).valueOf()
|
||||
|
||||
Reference in New Issue
Block a user