From 8f9bb3a22374ba4f6efe0d425a6cbe10e5dd4067 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 1 May 2017 14:49:03 -0700 Subject: [PATCH] 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 --- src/printer.js | 78 +++++++++++-------- .../function/__snapshots__/jsfmt.spec.js.snap | 31 ++++++++ tests/function/single_expand.js | 13 ++++ 3 files changed, 89 insertions(+), 33 deletions(-) create mode 100644 tests/function/single_expand.js diff --git a/src/printer.js b/src/printer.js index 563b425d..272c6f42 100644 --- a/src/printer.js +++ b/src/printer.js @@ -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"), diff --git a/tests/function/__snapshots__/jsfmt.spec.js.snap b/tests/function/__snapshots__/jsfmt.spec.js.snap index 6856bfe0..38c9c18a 100644 --- a/tests/function/__snapshots__/jsfmt.spec.js.snap +++ b/tests/function/__snapshots__/jsfmt.spec.js.snap @@ -26,3 +26,34 @@ a + function() {}; new function() {}(); `; + +exports[`single_expand.js 1`] = ` +function onDidInsertSuggestion({ + editor, + triggerPosition, + re +}): Promise { +} + +class X { + async onDidInsertSuggestion({editor, triggerPosition, suggestion}): Promise< + void + > { + } +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +function onDidInsertSuggestion({ + editor, + triggerPosition, + re +}): Promise {} + +class X { + async onDidInsertSuggestion({ + editor, + triggerPosition, + suggestion + }): Promise {} +} + +`; diff --git a/tests/function/single_expand.js b/tests/function/single_expand.js new file mode 100644 index 00000000..5884fa4c --- /dev/null +++ b/tests/function/single_expand.js @@ -0,0 +1,13 @@ +function onDidInsertSuggestion({ + editor, + triggerPosition, + re +}): Promise { +} + +class X { + async onDidInsertSuggestion({editor, triggerPosition, suggestion}): Promise< + void + > { + } +}