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", 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 =