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 diff --git a/src/index.ts b/src/index.ts index 7e4c647..0b4d8ff 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), @@ -553,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: @@ -946,7 +951,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 +960,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..bb4cfd8 100644 --- a/tests/plugins.test.ts +++ b/tests/plugins.test.ts @@ -444,6 +444,26 @@ import Custom from '../components/Custom.astro' ``, ``, ], + + // Duplicates can be removed in simple attributes + [ + `
`, + `
`, + ], + + // Duplicates cannot be removed in string literals otherwise invalid + // code will be produced during printing. + [ + `
`, + `
`, + ], + + // Duplicates cannot be removed in template literals otherwise invalid + // code will be produced during printing. + [ + `
`, + `
`, + ], ], }, },