From 53898286a4539d4287b00e58a67999de4197924c Mon Sep 17 00:00:00 2001 From: Denis Borovnev Date: Fri, 24 Sep 2021 14:46:58 +0300 Subject: [PATCH 1/2] Allow print jsx attributes on the new line if somebody typed first attribute on the new line then print all the attributes on the new line. otherwise left all the atributes on the single line if there is enough space --- src/language-js/print/jsx.js | 11 ++++++-- .../__snapshots__/jsfmt.spec.js.snap | 27 +++++++++++++++++++ tests/format/jsx/split-attrs/test.js | 14 ++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/language-js/print/jsx.js b/src/language-js/print/jsx.js index 441ac4ad1262..4e4a640ae460 100644 --- a/src/language-js/print/jsx.js +++ b/src/language-js/print/jsx.js @@ -20,7 +20,7 @@ const { utils: { willBreak }, } = require("../../document/index.js"); -const { getLast, getPreferredQuote } = require("../../common/util.js"); +const { getLast, getPreferredQuote, isNonEmptyArray } = require("../../common/util.js"); const { isJsxNode, rawText, @@ -596,7 +596,7 @@ function printJsxOpeningElement(path, options, print) { // We should print the opening element expanded if any prop value is a // string literal with newlines - const shouldBreak = + const hasMultilineStringAttr = node.attributes && node.attributes.some( (attr) => @@ -605,6 +605,13 @@ function printJsxOpeningElement(path, options, print) { attr.value.value.includes("\n") ); + const isFirstAttrOnTheNextLine = + isNonEmptyArray(node.attributes) && + node.attributes.length > 1 && + node.attributes[0].loc.start.line !== node.loc.start.line; + + const shouldBreak = hasMultilineStringAttr || isFirstAttrOnTheNextLine; + return group( [ "<", diff --git a/tests/format/jsx/split-attrs/__snapshots__/jsfmt.spec.js.snap b/tests/format/jsx/split-attrs/__snapshots__/jsfmt.spec.js.snap index 01c6ad6b2c30..5326302fe6cf 100644 --- a/tests/format/jsx/split-attrs/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/jsx/split-attrs/__snapshots__/jsfmt.spec.js.snap @@ -36,6 +36,20 @@ short_open = hello +short_forced_new_line = + + hello + + +no_new_line_if_there_is_only_one_attribute = + + hello + + make_self_closing =
@@ -167,6 +181,19 @@ short_open = ( ); +short_forced_new_line = ( + + hello + +); + +no_new_line_if_there_is_only_one_attribute = ( + hello +); + make_self_closing = (
+short_forced_new_line = + + hello + + +no_new_line_if_there_is_only_one_attribute = + + hello + + make_self_closing =
From 5e92f21664d735bedede5998b37a35c376503b57 Mon Sep 17 00:00:00 2001 From: Denis Borovnev Date: Fri, 24 Sep 2021 15:11:49 +0300 Subject: [PATCH 2/2] updated package name --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7e4774694c96..69b896946031 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "prettier", + "name": "@logicsoftware/prettier", "version": "2.5.0-dev", "description": "Prettier is an opinionated code formatter", "bin": "./bin/prettier.js",