diff --git a/src/printer.js b/src/printer.js index 52f493c85..aada11148 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1837,18 +1837,27 @@ function genericPrintNoParens(path, options, print, args) { parts.push("declare "); } + const canBreak = ( + n.right.type === "StringLiteralTypeAnnotation" + ); + + const printed = printAssignmentRight( + n.right, + path.call(print, "right"), + canBreak, + options + ); + parts.push( "type ", path.call(print, "id"), path.call(print, "typeParameters"), " =", - hasLeadingOwnLineComment(options.originalText, n.right) - ? indent(concat([hardline, path.call(print, "right")])) - : concat([" ", path.call(print, "right")]), + printed, semi ); - return concat(parts); + return group(concat(parts)); } case "TypeCastExpression": return concat([ @@ -3345,6 +3354,18 @@ function printBinaryishExpressions(path, print, options, isNested, isInsideParen return parts; } +function printAssignmentRight(rightNode, printedRight, canBreak, options) { + if (hasLeadingOwnLineComment(options.originalText, rightNode)) { + return indent(concat([hardline, printedRight])); + } + + if (canBreak) { + return indent(concat([line, printedRight])); + } + + return concat([" ", printedRight]); +} + function printAssignment( leftNode, printedLeft, @@ -3357,20 +3378,20 @@ function printAssignment( return printedLeft; } - let printed; - if (hasLeadingOwnLineComment(options.originalText, rightNode)) { - printed = indent(concat([hardline, printedRight])); - } else if ( + const canBreak = ( (isBinaryish(rightNode) && !shouldInlineLogicalExpression(rightNode)) || (leftNode.type === "Identifier" || leftNode.type === "MemberExpression") && (rightNode.type === "StringLiteral" || (rightNode.type === "Literal" && typeof rightNode.value === "string") || isMemberExpressionChain(rightNode)) - ) { - printed = indent(concat([line, printedRight])); - } else { - printed = concat([" ", printedRight]); - } + ); + + const printed = printAssignmentRight( + rightNode, + printedRight, + canBreak, + options + ); return group(concat([printedLeft, " ", operator, printed])); } diff --git a/tests/flow_type_declarations/__snapshots__/jsfmt.spec.js.snap b/tests/flow_type_declarations/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..c4992d058 --- /dev/null +++ b/tests/flow_type_declarations/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,22 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`long.js 1`] = ` +export type AdamPlacementValidationSingleErrorKey = + 'SOME_FANCY_TARGETS.GLOBAL_TARGET'; + +export type SomeReallyLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = true; + +export type SomeOtherLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = null; + +type SomeOtherLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = SomeReallyLongLongLongLongLongLongIdentifier; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +export type AdamPlacementValidationSingleErrorKey = + "SOME_FANCY_TARGETS.GLOBAL_TARGET"; + +export type SomeReallyLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = true; + +export type SomeOtherLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = null; + +type SomeOtherLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = SomeReallyLongLongLongLongLongLongIdentifier; + +`; diff --git a/tests/flow_type_declarations/jsfmt.spec.js b/tests/flow_type_declarations/jsfmt.spec.js new file mode 100644 index 000000000..1840e9d36 --- /dev/null +++ b/tests/flow_type_declarations/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, null, ["babylon"]); diff --git a/tests/flow_type_declarations/long.js b/tests/flow_type_declarations/long.js new file mode 100644 index 000000000..1728e1c01 --- /dev/null +++ b/tests/flow_type_declarations/long.js @@ -0,0 +1,8 @@ +export type AdamPlacementValidationSingleErrorKey = + 'SOME_FANCY_TARGETS.GLOBAL_TARGET'; + +export type SomeReallyLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = true; + +export type SomeOtherLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = null; + +type SomeOtherLongLongLongLongLongLongLongLongLongLongLongLongLongLongKey = SomeReallyLongLongLongLongLongLongIdentifier;