Skip to content

Jsx force multiline attributes #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: logicsoftware
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 9 additions & 2 deletions src/language-js/print/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) =>
Expand All @@ -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(
[
"<",
Expand Down
27 changes: 27 additions & 0 deletions tests/format/jsx/split-attrs/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ short_open =
hello
</BaseForm>

short_forced_new_line =
<BaseForm
url="/auth/google"
method="GET">
hello
</BaseForm>

no_new_line_if_there_is_only_one_attribute =
<BaseForm
url="/auth/google"
>
hello
</BaseForm>

make_self_closing =
<div>
<BaseForm url="/auth/google" method="GET" colour="blue" size="large" submitLabel="Sign in with Google">
Expand Down Expand Up @@ -167,6 +181,19 @@ short_open = (
</BaseForm>
);

short_forced_new_line = (
<BaseForm
url="/auth/google"
method="GET"
>
hello
</BaseForm>
);

no_new_line_if_there_is_only_one_attribute = (
<BaseForm url="/auth/google">hello</BaseForm>
);

make_self_closing = (
<div>
<BaseForm
Expand Down
14 changes: 14 additions & 0 deletions tests/format/jsx/split-attrs/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ short_open =
hello
</BaseForm>

short_forced_new_line =
<BaseForm
url="/auth/google"
method="GET">
hello
</BaseForm>

no_new_line_if_there_is_only_one_attribute =
<BaseForm
url="/auth/google"
>
hello
</BaseForm>

make_self_closing =
<div>
<BaseForm url="/auth/google" method="GET" colour="blue" size="large" submitLabel="Sign in with Google">
Expand Down