Skip to content

Commit

Permalink
(feat) trim whitespace in class attributes (#356)
Browse files Browse the repository at this point in the history
fixes #339
  • Loading branch information
dummdidumm authored Mar 22, 2023
1 parent 7abac5f commit a94563c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- (feat) support `requirePragma` and `insertPragma` options ([#350](https://github.com/sveltejs/prettier-plugin-svelte/issues/350))
- (feat) support `<svelte:document>`
- (feat) trim whitespace in `class` attributes ([#339](https://github.com/sveltejs/prettier-plugin-svelte/issues/339))

## 2.9.0

Expand Down
32 changes: 30 additions & 2 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,38 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
*/
return fill(splitTextToDocs(node));
} else {
const rawText = getUnencodedText(node);
if (path.getParentNode().type === 'Attribute') {
let rawText = getUnencodedText(node);
const parent = path.getParentNode();
if (parent.type === 'Attribute') {
// Direct child of attribute value -> add literallines at end of lines
// so that other things don't break in unexpected places
if (parent.name === 'class' && path.getParentNode(1).type === 'Element') {
// Special treatment for class attribute on html elements. Prettier
// will force everything into one line, we deviate from that and preserve lines.
rawText = rawText.replace(
/([^ \t\n])(([ \t]+$)|([ \t]+(\r?\n))|[ \t]+)/g,
// Remove trailing whitespace in lines with non-whitespace characters
// except at the end of the string
(
match,
characterBeforeWhitespace,
_,
isEndOfString,
isEndOfLine,
endOfLine,
) =>
isEndOfString
? match
: characterBeforeWhitespace + (isEndOfLine ? endOfLine : ' '),
);
// Shrink trailing whitespace in case it's followed by a mustache tag
// and remove it completely if it's at the end of the string, but not
// if it's on its own line
rawText = rawText.replace(
/([^ \t\n])[ \t]+$/,
parent.value.indexOf(node) === parent.value.length - 1 ? '$1' : '$1 ',
);
}
return concat(replaceEndOfLineWith(rawText, literalline));
}
return rawText;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div
class=" {longExpressionThatForcesLinebreaks} longExpressionThatForcesLinebreaks longExpressionThatForcesLinebreaks "
class=" {longExpressionThatForcesLinebreaks} longExpressionThatForcesLinebreaks longExpressionThatForcesLinebreaks"
alt="When reformatting class attributes it is fine to trim and add new line breaks.
That's not the case with other attributes. "
data-value={array.map((item) => {
Expand Down
26 changes: 26 additions & 0 deletions test/formatting/samples/attributes-class/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<button
class="bg-green-500 text-gray-100 hover:bg-green-400 flex items-center px-4 py-2 border border-l-0 rounded-r "
>
Copy
</button>
<div
class="bg-green-500 text-gray-100 hover:bg-green-400 flex items-center px-4 py-2 border border-l-0 rounded-r"
>
Copy
</div>
<span
class="bg-green-500 text-gray-100 hover:bg-green-400 flex
items-center px-4 py-2 border border-l-0 rounded-r "
>
Copy
</span>
<p
class="bg-green-500
text-gray-100 hover:bg-green-400 flex items-center px-4 py-2 border border-l-0 rounded-r
"
>
Copy
</p>

<P class="leave me alone because I'm a prop " />
26 changes: 26 additions & 0 deletions test/formatting/samples/attributes-class/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<button
class="bg-green-500 text-gray-100 hover:bg-green-400 flex items-center px-4 py-2 border border-l-0 rounded-r"
>
Copy
</button>
<div
class="bg-green-500 text-gray-100 hover:bg-green-400 flex items-center px-4 py-2 border border-l-0 rounded-r"
>
Copy
</div>
<span
class="bg-green-500 text-gray-100 hover:bg-green-400 flex
items-center px-4 py-2 border border-l-0 rounded-r"
>
Copy
</span>
<p
class="bg-green-500
text-gray-100 hover:bg-green-400 flex items-center px-4 py-2 border border-l-0 rounded-r
"
>
Copy
</p>

<P class="leave me alone because I'm a prop " />

0 comments on commit a94563c

Please sign in to comment.