Skip to content

fix: prevent Svelte files from breaking when there are duplicate classes #359

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
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ function sortStringLiteral(
node: any,
{
env,
removeDuplicates,
collapseWhitespace = { start: true, end: true },
}: {
env: TransformerEnv
Expand All @@ -463,6 +464,7 @@ function sortStringLiteral(
) {
let result = sortClasses(node.value, {
env,
removeDuplicates,
collapseWhitespace,
})
let didChange = result !== node.value
Expand Down Expand Up @@ -513,6 +515,7 @@ function sortTemplateLiteral(
node: any,
{
env,
removeDuplicates,
collapseWhitespace = { start: true, end: true },
}: {
env: TransformerEnv
Expand All @@ -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),

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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') {
Expand Down
20 changes: 20 additions & 0 deletions tests/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,26 @@ import Custom from '../components/Custom.astro'
`<div\n class={\`underline \n flex\`}></div>`,
`<div\n class={\`flex \n underline\`}\n></div>`,
],

// Duplicates can be removed in simple attributes
[
`<div class="flex flex underline flex flex"></div>`,
`<div class="flex underline"></div>`,
],

// Duplicates cannot be removed in string literals otherwise invalid
// code will be produced during printing.
[
`<div class={'flex underline flex'}></div>`,
`<div class={'flex flex underline'}></div>`,
],

// Duplicates cannot be removed in template literals otherwise invalid
// code will be produced during printing.
[
`<div class={\`flex underline flex\`}></div>`,
`<div class={\`flex flex underline\`}></div>`,
],
],
},
},
Expand Down