Skip to content

(feat) trim whitespace in class attributes #356

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

Merged
merged 1 commit into from
Mar 22, 2023
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
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 " />