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

fix: prevent removal of trailing JSX whitespace #1403

Merged
merged 1 commit into from
Jun 1, 2024
Merged
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 lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ function genericPrintNoParens(path: any, options: any, print: any) {
typeof child.value === "string"
) {
if (/\S/.test(child.value)) {
return child.value.replace(/^\s+|\s+$/g, "");
return child.value.replace(/^\s+/g, "");
} else if (/\n/.test(child.value)) {
return "\n";
}
Expand Down
32 changes: 32 additions & 0 deletions test/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { parse } from "../lib/parser";
import { Printer } from "../lib/printer";
import assert from "assert";
import * as types from "ast-types";
const nodeMajorVersion = parseInt(process.versions.node, 10);

Expand Down Expand Up @@ -61,3 +62,34 @@ for (const { title, parser } of [
});
});
}

it("should not remove trailing whitespaces", function () {
const printer = new Printer({ tabWidth: 2 });
const source =
"function App() {\n" +
' const name = "world";\n' +
"\n" +
" return (\n" +
' <div className="app">\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">hello {name}\n' +
" </div>\n" +
" );\n" +
"}",
);
});
Loading