Merge pull request #3311 from babel/object-body

[T7047] Consider arrow functions when parenthesizing object expressions
This commit is contained in:
Henry Zhu 2016-02-04 14:44:22 -05:00
commit f8036c7230
4 changed files with 8 additions and 12 deletions

View File

@ -77,15 +77,5 @@ export function ArrowFunctionExpression(node: Object) {
this.push(" => ");
const bodyNeedsParens = t.isObjectExpression(node.body);
if (bodyNeedsParens) {
this.push("(");
}
this.print(node.body, node);
if (bodyNeedsParens) {
this.push(")");
}
}

View File

@ -50,7 +50,7 @@ export function ObjectExpression(node: Object, parent: Object, printStack: Array
return true;
}
return isFirstInStatement(printStack);
return isFirstInStatement(printStack, true);
}
export function Binary(node: Object, parent: Object): boolean {
@ -233,7 +233,7 @@ export function AssignmentExpression(node: Object): boolean {
// Walk up the print stack to deterimine if our node can come first
// in statement.
function isFirstInStatement(printStack: Array<Object>): boolean {
function isFirstInStatement(printStack: Array<Object>, considerArrow: bool = false): boolean {
let i = printStack.length - 1;
let node = printStack[i];
i--;
@ -243,6 +243,10 @@ function isFirstInStatement(printStack: Array<Object>): boolean {
return true;
}
if (considerArrow && t.isArrowFunctionExpression(parent, { body: node })) {
return true;
}
if ((t.isCallExpression(parent, { callee: node })) ||
(t.isSequenceExpression(parent) && parent.expressions[0] === node) ||
(t.isMemberExpression(parent, { object: node })) ||

View File

@ -2,3 +2,4 @@ var foo = arr.map(v => ({
x: v.bar,
y: v.bar*2
}));
var fn = () => ({}).key;

View File

@ -2,3 +2,4 @@ var foo = arr.map(v => ({
x: v.bar,
y: v.bar * 2
}));
var fn = () => ({}).key;