Skip to content
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

print jsx children on newline when we have a multi-line opening element #1409

Open
wants to merge 1 commit into
base: master
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
12 changes: 9 additions & 3 deletions lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ function genericPrintNoParens(path: any, options: any, print: any) {
return openingLines;
}

const childLines = concat(
let childLines = concat(
path.map(function (childPath: any) {
const child = childPath.getValue();

Expand All @@ -1322,7 +1322,13 @@ function genericPrintNoParens(path: any, options: any, print: any) {

return print(childPath);
}, "children"),
).indentTail(options.tabWidth);
);
if (openingLines.length > 1) {
// If we have a multiline opening element, start the child out on a newline
childLines = concat(["\n", childLines]).indent(options.tabWidth);
} else {
childLines = childLines.indentTail(options.tabWidth);
}

const closingLines = path.call(print, closingPropName);

Expand Down Expand Up @@ -1351,10 +1357,10 @@ function genericPrintNoParens(path: any, options: any, print: any) {
attrParts[i] = "\n";
}
});
attrParts.push("\n");

attrLines = concat(attrParts).indentTail(options.tabWidth);
}

parts.push(attrLines, n.selfClosing ? " />" : ">");

return concat(parts);
Expand Down
75 changes: 75 additions & 0 deletions test/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,78 @@ it("should not remove trailing whitespaces", function () {
"}",
);
});

it("should print end jsx > on same line when not wrapping", function () {
const printer = new Printer({ tabWidth: 2 });
const source =
"function App() {\n" +
' const name = "world";\n' +
"\n" +
" return (\n" +
" <div\n" +
' className="app"\n' +
' id="12345"\n' +
' data-attribute="test-information"\n' +
" >\n" +
" hello {name}\n" +
" </div>\n" +
" );\n" +
"}";
const ast = parse(source);
ast.program.body[0].body.body[1].argument.openingElement.attributes[0].name.name =
"abc";

const code = printer.printGenerically(ast).code;

assert.equal(
code,
"function App() {\n" +
' const name = "world";\n' +
"\n" +
" return (\n" +
' <div abc="app" id="12345" data-attribute="test-information">hello {name}\n' +
" </div>\n" +
" );\n" +
"}",
);
});

it("should print end jsx > on a new line when wrapping", function () {
const printer = new Printer({ tabWidth: 2, wrapColumn: 50 });
const source =
"function App() {\n" +
' const name = "world";\n' +
"\n" +
" return (\n" +
" <div\n" +
' className="app"\n' +
' id="12345"\n' +
' data-attribute="test-information"\n' +
" >\n" +
" hello {name}\n" +
" </div>\n" +
" );\n" +
"}";
const ast = parse(source);
ast.program.body[0].body.body[1].argument.openingElement.attributes[0].name.name =
"abc";

const code = printer.printGenerically(ast).code;

assert.equal(
code,
"function App() {\n" +
' const name = "world";\n' +
"\n" +
" return (\n" +
" <div\n" +
' abc="app"\n' +
' id="12345"\n' +
' data-attribute="test-information"\n' +
" >\n" +
" hello {name}\n" +
" </div>\n" +
" );\n" +
"}",
);
});