Skip to content

Commit c3c5ff5

Browse files
Handle quote escapes in LESS when sorting @apply (#392)
* Handle quote escapes in LESS when sorting `@apply` It’s perfect since the AST doesn’t actually have information on these (it’s just strings) but this should be good enough. * Update changelog
1 parent db0bd74 commit c3c5ff5

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Format quotes in `@source`, `@plugin`, and `@config` ([#387](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/387))
1313
- Support sorting in callable template literals ([#367](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/367))
1414
- Support sorting in function calls mixed with property accesses ([#367](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/367))
15+
- Handle quote escapes in LESS when sorting `@apply` ([#392](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/392))
1516

1617
## [0.6.14] - 2025-07-09
1718

src/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,14 +810,33 @@ function transformCss(ast: any, { env }: TransformerContext) {
810810
node.params,
811811
)
812812

813-
node.params = sortClasses(node.params, {
813+
let classList = node.params
814+
815+
let prefix = ''
816+
let suffix = ''
817+
818+
if (classList.startsWith('~"') && classList.endsWith('"')) {
819+
prefix = '~"'
820+
suffix = '"'
821+
classList = classList.slice(2, -1)
822+
isImportant = false
823+
} else if (classList.startsWith("~'") && classList.endsWith("'")) {
824+
prefix = "~'"
825+
suffix = "'"
826+
classList = classList.slice(2, -1)
827+
isImportant = false
828+
}
829+
830+
classList = sortClasses(classList, {
814831
env,
815832
ignoreLast: isImportant,
816833
collapseWhitespace: {
817834
start: false,
818835
end: !isImportant,
819836
},
820837
})
838+
839+
node.params = `${prefix}${classList}${suffix}`
821840
}
822841
})
823842
}

tests/tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export let tests: Record<string, TestEntry[]> = {
266266
t`@apply ${yes} #{'''!important'''};`,
267267
t`@apply ${yes} #{"'"'"!important"'"'"};`,
268268
],
269-
less: [...css, t`@apply ${yes} !important;`],
269+
less: [...css, t`@apply ${yes} !important;`, t`@apply ~"${yes}";`, t`@apply ~'${yes}';`],
270270
babel: javascript,
271271
typescript: javascript,
272272
'babel-ts': javascript,

0 commit comments

Comments
 (0)