Remove multilineGroup (#450)

This commit is contained in:
James Long authored and GitHub committed 2017-01-24 13:54:01 -05:00
1 parent d753e56a2a
commit 3aa267d5e8
6 files changed
+65 -48

No files matched your search

+20 -5
View File
@@ -24,11 +24,12 @@ line, but if it doesn't fit it will break the outermost group first
and try again. It will continue breaking groups until everything fits and try again. It will continue breaking groups until everything fits
(or there are no more groups to break). (or there are no more groups to break).
### multilineGroup A document can force parent groups to break by including `breakParent`
(see below). A hard and literal line automatically include this so
This is the same as `group`, but with an additional behavior: if this they always break parent groups. Breaks are propagated to all parent
group spans any other groups that have hard breaks (see below) this groups, so if a deeply nested expression has a hard break, everything
group *always* breaks. Otherwise it acts the same as `group`. with break. This only matters for "hard" breaks, i.e. newlines that
are printed no matter what and can be statically analyzed.
For example, an array will try to fit on one line: For example, an array will try to fit on one line:
@@ -53,6 +54,20 @@ Functions always break after the opening curly brace no matter what,
so the array breaks as well for consistent formatting. See the so the array breaks as well for consistent formatting. See the
implementation of `ArrayExpression` for an example. implementation of `ArrayExpression` for an example.
### breakParent
Include this anywhere to force all parent groups to break. See `group`
for more info. Example:
```js
group(concat([
" ",
expr,
" ",
breakParent
]))
```
### join ### join
Join an array of items with a separator. Join an array of items with a separator.
+1 -9
View File
@@ -33,13 +33,6 @@ function group(contents, opts) {
}; };
} }
function multilineGroup(contents, opts) {
return group(
contents,
Object.assign(opts || {}, { shouldBreak: willBreak(contents) })
);
}
function conditionalGroup(states, opts) { function conditionalGroup(states, opts) {
return group( return group(
states[0], states[0],
@@ -62,7 +55,7 @@ const breakParent = { type: "break-parent" };
const line = { type: "line" }; const line = { type: "line" };
const softline = { type: "line", soft: true }; const softline = { type: "line", soft: true };
const hardline = concat([{ type: "line", hard: true }, breakParent ]); const hardline = concat([{ type: "line", hard: true }, breakParent ]);
const literalline = { type: "line", hard: true, literal: true }; const literalline = concat([{ type: "line", hard: true, literal: true }, breakParent ]);
function join(sep, arr) { function join(sep, arr) {
var res = []; var res = [];
@@ -86,7 +79,6 @@ module.exports = {
hardline, hardline,
literalline, literalline,
group, group,
multilineGroup,
conditionalGroup, conditionalGroup,
breakParent, breakParent,
ifBreak, ifBreak,
+2 -2
View File
@@ -19,8 +19,8 @@ function flattenDoc(doc) {
} else if (doc.type === "if-break") { } else if (doc.type === "if-break") {
return Object.assign({}, doc, { return Object.assign({}, doc, {
breakContents: doc.breakContents ? flattenDoc(doc.breakContents) : null, breakContents: doc.breakContents != null ? flattenDoc(doc.breakContents) : null,
flatContents: doc.flatContents ? flattenDoc(doc.flatContents) : null flatContents: doc.flatContents != null ? flattenDoc(doc.flatContents) : null
}); });
} else if (doc.type === "group") { } else if (doc.type === "group") {
+36 -29
View File
@@ -13,7 +13,6 @@ var hardline = docBuilders.hardline;
var softline = docBuilders.softline; var softline = docBuilders.softline;
var literalline = docBuilders.literalline; var literalline = docBuilders.literalline;
var group = docBuilders.group; var group = docBuilders.group;
var multilineGroup = docBuilders.multilineGroup;
var indent = docBuilders.indent; var indent = docBuilders.indent;
var conditionalGroup = docBuilders.conditionalGroup; var conditionalGroup = docBuilders.conditionalGroup;
var ifBreak = docBuilders.ifBreak; var ifBreak = docBuilders.ifBreak;
@@ -270,6 +269,8 @@ function genericPrintNoParens(path, options, print) {
const body = path.call(print, "body"); const body = path.call(print, "body");
const collapsed = concat([ concat(parts), " ", body ]); const collapsed = concat([ concat(parts), " ", body ]);
// We want to always keep these types of nodes on the same line
// as the arrow.
if ( if (
n.body.type === "ArrayExpression" || n.body.type === "ArrayExpression" ||
n.body.type === "ObjectExpression" || n.body.type === "ObjectExpression" ||
@@ -278,13 +279,21 @@ function genericPrintNoParens(path, options, print) {
return group(collapsed); return group(collapsed);
} }
return conditionalGroup([ // These nested groups are a little wonky, but because
collapsed, // `conditionalGroup` suppresses break propagation, we want to
concat([ // re-propagate it. We still want to allow the printer to choose
concat(parts), // the more collapsed version, but still break parents if there
indent(options.tabWidth, concat([ line, body ])) // are any hard breaks in the content.
]) return group(
]); conditionalGroup([
collapsed,
concat([
concat(parts),
indent(options.tabWidth, concat([ line, body ]))
])
]),
{ shouldBreak: willBreak(body) }
);
case "MethodDefinition": case "MethodDefinition":
if (n.static) { if (n.static) {
parts.push("static "); parts.push("static ");
@@ -425,7 +434,7 @@ function genericPrintNoParens(path, options, print) {
if (grouped.length > 0) { if (grouped.length > 0) {
parts.push( parts.push(
multilineGroup( group(
concat([ concat([
"{", "{",
indent( indent(
@@ -560,7 +569,7 @@ function genericPrintNoParens(path, options, print) {
if (props.length === 0) { if (props.length === 0) {
return "{}"; return "{}";
} else { } else {
return multilineGroup( return group(
concat([ concat([
leftBrace, leftBrace,
indent( indent(
@@ -641,7 +650,7 @@ function genericPrintNoParens(path, options, print) {
lastElem === null; lastElem === null;
parts.push( parts.push(
multilineGroup( group(
concat([ concat([
"[", "[",
indent( indent(
@@ -781,7 +790,7 @@ function genericPrintNoParens(path, options, print) {
parts.push(";"); parts.push(";");
} }
return multilineGroup(concat(parts)); return group(concat(parts));
case "VariableDeclarator": case "VariableDeclarator":
return n.init return n.init
? concat([ path.call(print, "id"), " = ", path.call(print, "init") ]) ? concat([ path.call(print, "id"), " = ", path.call(print, "init") ])
@@ -1086,17 +1095,15 @@ function genericPrintNoParens(path, options, print) {
concat([ concat([
"<", "<",
path.call(print, "name"), path.call(print, "name"),
multilineGroup( concat([
concat([ indent(
indent( options.tabWidth,
options.tabWidth, concat(
concat( path.map(attr => concat([ line, print(attr) ]), "attributes")
path.map(attr => concat([ line, print(attr) ]), "attributes") )
) ),
), n.selfClosing ? line : softline
n.selfClosing ? line : softline ]),
])
),
n.selfClosing ? "/>" : ">" n.selfClosing ? "/>" : ">"
]) ])
); );
@@ -1310,7 +1317,7 @@ function genericPrintNoParens(path, options, print) {
parts.push(path.call(print, "typeParameters")); parts.push(path.call(print, "typeParameters"));
parts.push(multilineGroup(printFunctionParams(path, print, options))); parts.push(group(printFunctionParams(path, print, options)));
// The returnType is not wrapped in a TypeAnnotation, so the colon // The returnType is not wrapped in a TypeAnnotation, so the colon
// needs to be added separately. // needs to be added separately.
@@ -1655,7 +1662,7 @@ function printMethod(path, options, print) {
parts.push( parts.push(
key, key,
path.call(print, "value", "typeParameters"), path.call(print, "value", "typeParameters"),
multilineGroup( group(
concat([ concat([
path.call( path.call(
function(valuePath) { function(valuePath) {
@@ -1726,7 +1733,7 @@ function printArgumentsList(path, options, print) {
); );
} }
return multilineGroup( return group(
concat([ concat([
"(", "(",
indent( indent(
@@ -1811,7 +1818,7 @@ function printObjectMethod(path, options, print) {
} }
parts.push( parts.push(
multilineGroup( group(
concat([ concat([
printFunctionParams(path, print, options), printFunctionParams(path, print, options),
printReturnType(path, print) printReturnType(path, print)
@@ -1879,7 +1886,7 @@ function printExportDeclaration(path, options, print) {
} else { } else {
parts.push( parts.push(
decl.exportKind === "type" ? "type " : "", decl.exportKind === "type" ? "type " : "",
multilineGroup( group(
concat([ concat([
"{", "{",
indent( indent(
@@ -2276,7 +2283,7 @@ function maybeWrapJSXElementInParens(path, elem, options) {
return elem; return elem;
} }
return multilineGroup( return group(
concat([ concat([
ifBreak("("), ifBreak("("),
indent(options.tabWidth, concat([ softline, elem ])), indent(options.tabWidth, concat([ softline, elem ])),
@@ -179,7 +179,8 @@ Observable
/* Comments in JSX tag are placed in a non optimal way*/ /* Comments in JSX tag are placed in a non optimal way*/
<div <div
/* comment*/ />; /* comment*/
/>;
// Comments disappear in empty blocks // Comments disappear in empty blocks
if (1) { if (1) {
@@ -379,7 +380,8 @@ Observable
// Comments in JSX tag are placed in a non optimal way // Comments in JSX tag are placed in a non optimal way
<// comment <// comment
div />; div
/>;
// Comments disappear in empty blocks // Comments disappear in empty blocks
if (1) { if (1) {
@@ -18,7 +18,8 @@ export type Result<T, V> =
\` \`
\` + \` \` +
\`
\`; \`;