Skip to content

Commit

Permalink
Merge pull request #1403 from benjamn/fix/1223-redo
Browse files Browse the repository at this point in the history
fix: prevent removal of trailing JSX whitespace
  • Loading branch information
eventualbuddha authored Jun 1, 2024
2 parents ac2903c + 5d0ec0a commit 933e25a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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" +
"}",
);
});

0 comments on commit 933e25a

Please sign in to comment.