From 5894e8cb628df1f18af729ac552caefc90c2f188 Mon Sep 17 00:00:00 2001 From: Derek Santolo Date: Sat, 26 Apr 2025 20:03:16 -0700 Subject: [PATCH 1/5] fix: prevent Svelte from breaking when there are duplicate classes --- src/index.ts | 8 ++++++-- tests/plugins.test.ts | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7e4c647..a98abb2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -454,6 +454,7 @@ function sortStringLiteral( node: any, { env, + removeDuplicates, collapseWhitespace = { start: true, end: true }, }: { env: TransformerEnv @@ -463,6 +464,7 @@ function sortStringLiteral( ) { let result = sortClasses(node.value, { env, + removeDuplicates, collapseWhitespace, }) let didChange = result !== node.value @@ -513,6 +515,7 @@ function sortTemplateLiteral( node: any, { env, + removeDuplicates, collapseWhitespace = { start: true, end: true }, }: { env: TransformerEnv @@ -530,6 +533,7 @@ function sortTemplateLiteral( quasi.value.raw = sortClasses(quasi.value.raw, { env, + removeDuplicates, // Is not the first "item" and does not start with a space ignoreFirst: i > 0 && !/^\s/.test(quasi.value.raw), @@ -946,7 +950,7 @@ function transformSvelte(ast: any, { env, changes }: TransformerContext) { env, ignoreFirst: i > 0 && !/^\s/.test(value.raw), ignoreLast: i < attr.value.length - 1 && !/\s$/.test(value.raw), - removeDuplicates: false, + removeDuplicates: true, collapseWhitespace: false, }) value.data = same @@ -955,7 +959,7 @@ function transformSvelte(ast: any, { env, changes }: TransformerContext) { env, ignoreFirst: i > 0 && !/^\s/.test(value.data), ignoreLast: i < attr.value.length - 1 && !/\s$/.test(value.data), - removeDuplicates: false, + removeDuplicates: true, collapseWhitespace: false, }) } else if (value.type === 'MustacheTag') { diff --git a/tests/plugins.test.ts b/tests/plugins.test.ts index 5ba0443..eb1b799 100644 --- a/tests/plugins.test.ts +++ b/tests/plugins.test.ts @@ -444,6 +444,26 @@ import Custom from '../components/Custom.astro' ``, ``, ], + [ + `
`, + `
`, + ], + + // Remove duplicates in Svelte text case: + [ + `
`, + `
`, + ], + // Svelte literal case (project doesn't break with duplicates): + [ + `
`, + `
`, + ], + // Svelte string literal case (project doesn't break with duplicates); + [ + `
`, + `
`, + ], ], }, }, From 4796e8abf9f35af9b02eb795bad62b9fa802bffe Mon Sep 17 00:00:00 2001 From: Derek Santolo Date: Sat, 26 Apr 2025 20:15:43 -0700 Subject: [PATCH 2/5] fix: pass removeDuplicates every time sortTemplateLiteral calls sortClasses --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index a98abb2..0b4d8ff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -557,6 +557,7 @@ function sortTemplateLiteral( ignoreFirst: i > 0 && !/^\s/.test(quasi.value.cooked), ignoreLast: i < node.expressions.length && !/\s$/.test(quasi.value.cooked), + removeDuplicates, collapseWhitespace: collapseWhitespace && { start: collapseWhitespace && collapseWhitespace.start && i === 0, end: From 00cfe3c5ca79b8b97dd7aa46a65c29640e7b325c Mon Sep 17 00:00:00 2001 From: Derek Santolo Date: Sat, 26 Apr 2025 20:22:26 -0700 Subject: [PATCH 3/5] chore: remove redundant test --- tests/plugins.test.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/plugins.test.ts b/tests/plugins.test.ts index eb1b799..90e20c0 100644 --- a/tests/plugins.test.ts +++ b/tests/plugins.test.ts @@ -444,10 +444,6 @@ import Custom from '../components/Custom.astro' ``, ``, ], - [ - `
`, - `
`, - ], // Remove duplicates in Svelte text case: [ From d852d06d004ab9be1b4d1673748726bb3fdaae58 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Fri, 30 May 2025 11:42:34 -0400 Subject: [PATCH 4/5] Update comments --- tests/plugins.test.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/plugins.test.ts b/tests/plugins.test.ts index 90e20c0..bb4cfd8 100644 --- a/tests/plugins.test.ts +++ b/tests/plugins.test.ts @@ -445,17 +445,21 @@ import Custom from '../components/Custom.astro' ``, ], - // Remove duplicates in Svelte text case: + // Duplicates can be removed in simple attributes [ `
`, `
`, ], - // Svelte literal case (project doesn't break with duplicates): + + // Duplicates cannot be removed in string literals otherwise invalid + // code will be produced during printing. [ `
`, `
`, ], - // Svelte string literal case (project doesn't break with duplicates); + + // Duplicates cannot be removed in template literals otherwise invalid + // code will be produced during printing. [ `
`, `
`, From 433ad20ea5f8ee648be8a32dc059f977b2bbba33 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Fri, 30 May 2025 11:43:43 -0400 Subject: [PATCH 5/5] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92822b7..e3161e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Nothing yet! +- Prevent Svelte files from breaking when there are duplicate classes ([#359](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/359)) ## [0.6.12] - 2025-05-30