Skip to content

Commit 9b87e4a

Browse files
Sort template literals in Angular expressions (#377)
* Sort template literals in Angular expressions Prettier fixed their parser bug in v3.6 so we just need to check for template literals * Update changelog
1 parent 3f06943 commit 9b87e4a

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
- Add support for OXC + Hermes Prettier plugins ([#376](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/376))
11+
- Sort template literals in Angular expressions ([#377](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/377))
1112

1213
## [0.6.13] - 2025-06-19
1314

src/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,44 @@ function transformDynamicAngularAttribute(attr: any, env: TransformerEnv) {
141141
}),
142142
})
143143
},
144+
145+
TemplateLiteral(node, path) {
146+
if (!node.quasis.length) return
147+
148+
let concat = path.find((entry) => {
149+
return (
150+
entry.parent &&
151+
entry.parent.type === 'BinaryExpression' &&
152+
entry.parent.operator === '+'
153+
)
154+
})
155+
156+
for (let i = 0; i < node.quasis.length; i++) {
157+
let quasi = node.quasis[i]
158+
159+
changes.push({
160+
start: quasi.start,
161+
end: quasi.end,
162+
before: quasi.value.raw,
163+
after: sortClasses(quasi.value.raw, {
164+
env,
165+
166+
// Is not the first "item" and does not start with a space
167+
ignoreFirst: i > 0 && !/^\s/.test(quasi.value.raw),
168+
169+
// Is between two expressions
170+
// And does not end with a space
171+
ignoreLast:
172+
i < node.expressions.length && !/\s$/.test(quasi.value.raw),
173+
174+
collapseWhitespace: {
175+
start: concat?.key !== 'right' && i === 0,
176+
end: concat?.key !== 'left' && i >= node.expressions.length,
177+
},
178+
}),
179+
})
180+
}
181+
},
144182
})
145183

146184
attr.value = spliceChangesIntoString(attr.value, changes)

tests/tests.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ export let tests: Record<string, TestEntry[]> = {
238238

239239
// TODO: Enable this test — it causes console noise but not a failure
240240
// t`<div [ngClass]="{ '${no}': foo && definitely&a:syntax*error }" class="${yes}"></div>`,
241+
242+
// TODO: Enable test once we can run all tests against Prettier v3.6
243+
// t`<div [ngClass]="\`${yes}\`"></div>`,
241244
],
242245
css: [...css, t`@apply ${yes} !important;`],
243246
scss: [

0 commit comments

Comments
 (0)