Wrap parens around default exports starting with function/class - fixes T7136

This commit is contained in:
Logan Smyth
2016-03-12 15:23:42 -08:00
parent 5373733b8d
commit 256c0100cb
3 changed files with 34 additions and 30 deletions

View File

@@ -43,12 +43,7 @@ export function UpdateExpression(node: Object, parent: Object): boolean {
}
export function ObjectExpression(node: Object, parent: Object, printStack: Array<Object>): boolean {
if (t.isExpressionStatement(parent)) {
// ({ foo: "bar" });
return true;
}
return isFirstInStatement(printStack, true);
return isFirstInStatement(printStack, {considerArrow: true});
}
export function Binary(node: Object, parent: Object): boolean {
@@ -152,18 +147,8 @@ export function YieldExpression(node: Object, parent: Object): boolean {
export { YieldExpression as AwaitExpression };
export function ClassExpression(node: Object, parent: Object): boolean {
// (class {});
if (t.isExpressionStatement(parent)) {
return true;
}
// export default (class () {});
if (t.isExportDeclaration(parent)) {
return true;
}
return false;
export function ClassExpression(node: Object, parent: Object, printStack: Array<Object>): boolean {
return isFirstInStatement(printStack, {considerDefaultExports: true});
}
export function UnaryLike(node: Object, parent: Object): boolean {
@@ -179,17 +164,7 @@ export function UnaryLike(node: Object, parent: Object): boolean {
}
export function FunctionExpression(node: Object, parent: Object, printStack: Array<Object>): boolean {
// (function () {});
if (t.isExpressionStatement(parent)) {
return true;
}
// export default (function () {});
if (t.isExportDeclaration(parent)) {
return true;
}
return isFirstInStatement(printStack);
return isFirstInStatement(printStack, {considerDefaultExports: true});
}
export function ArrowFunctionExpression(node: Object, parent: Object): boolean {
@@ -235,7 +210,10 @@ 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>, considerArrow: bool = false): boolean {
function isFirstInStatement(printStack: Array<Object>, {
considerArrow = false,
considerDefaultExports = false
} = {}): boolean {
let i = printStack.length - 1;
let node = printStack[i];
i--;
@@ -245,6 +223,10 @@ function isFirstInStatement(printStack: Array<Object>, considerArrow: bool = fal
return true;
}
if (considerDefaultExports && t.isExportDefaultDeclaration(parent, { declaration: node })) {
return true;
}
if (considerArrow && t.isArrowFunctionExpression(parent, { body: node })) {
return true;
}