Skip to content

Commit cd8832b

Browse files
Prevent Svelte files from breaking when there are duplicate classes (#359)
* fix: prevent Svelte from breaking when there are duplicate classes * fix: pass removeDuplicates every time sortTemplateLiteral calls sortClasses * chore: remove redundant test * Update comments * Update changelog --------- Co-authored-by: Jordan Pittman <[email protected]>
1 parent 8fcba16 commit cd8832b

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
- Prevent Svelte files from breaking when there are duplicate classes ([#359](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/359))
1111

1212
## [0.6.12] - 2025-05-30
1313

src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ function sortStringLiteral(
454454
node: any,
455455
{
456456
env,
457+
removeDuplicates,
457458
collapseWhitespace = { start: true, end: true },
458459
}: {
459460
env: TransformerEnv
@@ -463,6 +464,7 @@ function sortStringLiteral(
463464
) {
464465
let result = sortClasses(node.value, {
465466
env,
467+
removeDuplicates,
466468
collapseWhitespace,
467469
})
468470
let didChange = result !== node.value
@@ -513,6 +515,7 @@ function sortTemplateLiteral(
513515
node: any,
514516
{
515517
env,
518+
removeDuplicates,
516519
collapseWhitespace = { start: true, end: true },
517520
}: {
518521
env: TransformerEnv
@@ -530,6 +533,7 @@ function sortTemplateLiteral(
530533

531534
quasi.value.raw = sortClasses(quasi.value.raw, {
532535
env,
536+
removeDuplicates,
533537
// Is not the first "item" and does not start with a space
534538
ignoreFirst: i > 0 && !/^\s/.test(quasi.value.raw),
535539

@@ -553,6 +557,7 @@ function sortTemplateLiteral(
553557
ignoreFirst: i > 0 && !/^\s/.test(quasi.value.cooked),
554558
ignoreLast:
555559
i < node.expressions.length && !/\s$/.test(quasi.value.cooked),
560+
removeDuplicates,
556561
collapseWhitespace: collapseWhitespace && {
557562
start: collapseWhitespace && collapseWhitespace.start && i === 0,
558563
end:
@@ -946,7 +951,7 @@ function transformSvelte(ast: any, { env, changes }: TransformerContext) {
946951
env,
947952
ignoreFirst: i > 0 && !/^\s/.test(value.raw),
948953
ignoreLast: i < attr.value.length - 1 && !/\s$/.test(value.raw),
949-
removeDuplicates: false,
954+
removeDuplicates: true,
950955
collapseWhitespace: false,
951956
})
952957
value.data = same
@@ -955,7 +960,7 @@ function transformSvelte(ast: any, { env, changes }: TransformerContext) {
955960
env,
956961
ignoreFirst: i > 0 && !/^\s/.test(value.data),
957962
ignoreLast: i < attr.value.length - 1 && !/\s$/.test(value.data),
958-
removeDuplicates: false,
963+
removeDuplicates: true,
959964
collapseWhitespace: false,
960965
})
961966
} else if (value.type === 'MustacheTag') {

tests/plugins.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,26 @@ import Custom from '../components/Custom.astro'
444444
`<div\n class={\`underline \n flex\`}></div>`,
445445
`<div\n class={\`flex \n underline\`}\n></div>`,
446446
],
447+
448+
// Duplicates can be removed in simple attributes
449+
[
450+
`<div class="flex flex underline flex flex"></div>`,
451+
`<div class="flex underline"></div>`,
452+
],
453+
454+
// Duplicates cannot be removed in string literals otherwise invalid
455+
// code will be produced during printing.
456+
[
457+
`<div class={'flex underline flex'}></div>`,
458+
`<div class={'flex flex underline'}></div>`,
459+
],
460+
461+
// Duplicates cannot be removed in template literals otherwise invalid
462+
// code will be produced during printing.
463+
[
464+
`<div class={\`flex underline flex\`}></div>`,
465+
`<div class={\`flex flex underline\`}></div>`,
466+
],
447467
],
448468
},
449469
},

0 commit comments

Comments
 (0)