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: printer: JSXText: remove trailing whitespaces #1223

Closed
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
4 changes: 2 additions & 2 deletions lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1311,12 +1311,12 @@ 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";
}
}

return print(childPath);
}, "children"),
).indentTail(options.tabWidth);
Expand Down
34 changes: 34 additions & 0 deletions test/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import { parse } from "../lib/parser";
import { Printer } from "../lib/printer";
import * as types from "ast-types";
import assert from "assert";

const nodeMajorVersion = parseInt(process.versions.node, 10);

for (const { title, parser } of [
Expand Down Expand Up @@ -61,3 +63,35 @@ 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' +
Comment on lines +91 to +94
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the bugfix

now i just need prettier for pretty jsx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have ideas for a better fix, you are welcome :)

'}'
);
});