Break inline object first in function arguments (#1453)

This is getting subtle where the groups need to be in a precise position but that works :)

Fixes #1409
master
Christopher Chedeau 2017-05-01 14:49:03 -07:00 committed by GitHub
parent 54b8cac0a7
commit 8f9bb3a223
3 changed files with 89 additions and 33 deletions

View File

@ -806,20 +806,13 @@ function genericPrintNoParens(path, options, print, args) {
(lastElem.type === "RestProperty" || lastElem.type === "RestElement")
);
const shouldBreak =
n.type !== "ObjectPattern" &&
util.hasNewlineInRange(
options.originalText,
util.locStart(n),
util.locEnd(n)
);
if (props.length === 0) {
let content;
if (props.length === 0 && !n.typeAnnotation) {
if (!hasDanglingComments(n)) {
return concat([leftBrace, rightBrace]);
}
return group(
content = group(
concat([
prefix,
leftBrace,
@ -829,33 +822,52 @@ function genericPrintNoParens(path, options, print, args) {
])
);
} else {
return group(
concat([
prefix,
leftBrace,
indent(
align(
parentIsUnionTypeAnnotation ? 2 : 0,
concat([
options.bracketSpacing ? line : softline,
concat(props)
])
)
),
ifBreak(
canHaveTrailingComma && shouldPrintComma(options) ? "," : ""
),
content = concat([
prefix,
leftBrace,
indent(
align(
parentIsUnionTypeAnnotation ? 2 : 0,
concat([options.bracketSpacing ? line : softline, rightBrace])
),
n.typeAnnotation ? ": " : "",
path.call(print, "typeAnnotation")
]),
{ shouldBreak }
);
concat([
options.bracketSpacing ? line : softline,
concat(props)
])
)
),
ifBreak(
canHaveTrailingComma && shouldPrintComma(options) ? "," : ""
),
align(
parentIsUnionTypeAnnotation ? 2 : 0,
concat([options.bracketSpacing ? line : softline, rightBrace])
),
n.typeAnnotation ? ": " : "",
path.call(print, "typeAnnotation")
]);
}
// If we inline the object as first argument of the parent, we don't want
// to create another group so that the object breaks before the return
// type
if (
n.type === "ObjectPattern" &&
parent.params &&
parent.params.length === 1 &&
parent.params[0] === n
) {
return content;
}
const shouldBreak =
n.type !== "ObjectPattern" &&
util.hasNewlineInRange(
options.originalText,
util.locStart(n),
util.locEnd(n)
);
return group(content, { shouldBreak });
case "PropertyPattern":
return concat([
path.call(print, "key"),

View File

@ -26,3 +26,34 @@ a + function() {};
new function() {}();
`;
exports[`single_expand.js 1`] = `
function onDidInsertSuggestion({
editor,
triggerPosition,
re
}): Promise<void> {
}
class X {
async onDidInsertSuggestion({editor, triggerPosition, suggestion}): Promise<
void
> {
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function onDidInsertSuggestion({
editor,
triggerPosition,
re
}): Promise<void> {}
class X {
async onDidInsertSuggestion({
editor,
triggerPosition,
suggestion
}): Promise<void> {}
}
`;

View File

@ -0,0 +1,13 @@
function onDidInsertSuggestion({
editor,
triggerPosition,
re
}): Promise<void> {
}
class X {
async onDidInsertSuggestion({editor, triggerPosition, suggestion}): Promise<
void
> {
}
}