Skip to content

Commit

Permalink
Do not delete line breaks and indentation of lines in class attribute (
Browse files Browse the repository at this point in the history
…#369)

Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
  • Loading branch information
artemst and Princesseuh authored Aug 22, 2023
1 parent b806845 commit fa1a6e3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/six-lobsters-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prettier-plugin-astro": minor
---

Do not delete line breaks and indentation of lines in class attribute
7 changes: 6 additions & 1 deletion src/printer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ export function trimTextNodeRight(node: TextNode): void {
}

export function printClassNames(value: string) {
return value.trim().split(/\s+/).join(' ');
const lines = value.trim().split(/[\r\n]+/);
const formattedLines = lines.map((line) => {
const spaces = line.match(/^\s+/);
return (spaces ? spaces[0] : '') + line.trim().split(/\s+/).join(' ');
});
return formattedLines.join('\n');
}

/** dedent string & return tabSize (the last part is what we need) */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class=" sm:p-0 p-0
m-2 sm:m-1
border "></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="sm:p-0 p-0
m-2 sm:m-1
border"></div>
6 changes: 6 additions & 0 deletions test/tests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ test('Can format HTML custom elements', files, 'basic/html-custom-elements');

test('Can properly format the class attribute', files, 'basic/html-class-attribute');

test(
'Can properly format the class attribute with line breaks',
files,
'basic/html-class-attribute-with-line-breaks'
);

test('Can format long self-closing tags with multiple attributes', files, 'basic/self-closing');

test('Can properly format inline tags and respect whitespace', files, 'basic/inline-whitespace');

0 comments on commit fa1a6e3

Please sign in to comment.