Skip to content

Commit a13d661

Browse files
Don't move partial classes inside Twig attributes (#184)
* Simplify test * Fix CS * Fix concat expressions in Twig attributes * Update changelog
1 parent dfc23a8 commit a13d661

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Don't move partial classes inside Twig attributes ([#184](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/184))
1113

1214
## [0.4.0] - 2023-07-11
1315

src/plugin-v2.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,13 +642,19 @@ function transformMelody(ast, { env, changes }) {
642642
meta.sortTextNodes = true
643643
},
644644

645-
StringLiteral(node, _parent, _key, _index, meta) {
645+
StringLiteral(node, parent, _key, _index, meta) {
646646
if (!meta.sortTextNodes) {
647647
return
648648
}
649649

650+
const isConcat = parent.type === 'BinaryConcatExpression'
651+
650652
node.value = sortClasses(node.value, {
651653
env,
654+
ignoreFirst:
655+
isConcat && _key === 'right' && !/^[^\S\r\n]/.test(node.value),
656+
ignoreLast:
657+
isConcat && _key === 'left' && !/[^\S\r\n]$/.test(node.value),
652658
})
653659
},
654660
})

src/plugin-v3.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,13 +642,19 @@ function transformMelody(ast, { env, changes }) {
642642
meta.sortTextNodes = true
643643
},
644644

645-
StringLiteral(node, _parent, _key, _index, meta) {
645+
StringLiteral(node, parent, _key, _index, meta) {
646646
if (!meta.sortTextNodes) {
647647
return
648648
}
649649

650+
const isConcat = parent.type === 'BinaryConcatExpression'
651+
650652
node.value = sortClasses(node.value, {
651653
env,
654+
ignoreFirst:
655+
isConcat && _key === 'right' && !/^[^\S\r\n]/.test(node.value),
656+
ignoreLast:
657+
isConcat && _key === 'left' && !/[^\S\r\n]$/.test(node.value),
652658
})
653659
},
654660
})

tests/plugins.test.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,17 @@ let tests = [
100100
`<section class="{{ {base:css.prices}|classes }}"></section>`,
101101
`<section class="{{ { base: css.prices }|classes }}"></section>`,
102102
],
103-
[
104-
`<section class="sm:p-0 p-4"></section>`,
105-
`<section class="p-4 sm:p-0"></section>`,
106-
],
103+
t`<section class="${yes}"></section>`,
104+
t`<section class="${yes} text-{{ i }}"></section>`,
105+
t`<section class="${yes} {{ i }}-text"></section>`,
106+
t`<section class="text-{{ i }} ${yes}"></section>`,
107+
t`<section class="{{ i }}-text ${yes}"></section>`,
108+
109+
// text-center is used because it's placed between p-0 and sm:p-0
110+
t`<section class="${yes} text-center{{ i }}"></section>`,
111+
t`<section class="${yes} {{ i }}text-center"></section>`,
112+
t`<section class="text-center{{ i }} ${yes}"></section>`,
113+
t`<section class="{{ i }}text-center ${yes}"></section>`,
107114
],
108115
},
109116
},
@@ -235,7 +242,7 @@ let tests = [
235242
t`<div>
236243
<h1 class='${yes}'/>
237244
</div>`,
238-
t`style {
245+
t`style {
239246
h1 {
240247
@apply ${yes};
241248
}
@@ -270,7 +277,7 @@ let tests = [
270277
@apply bg-fuchsia-50 p-20 w-full;
271278
}
272279
</style>`,
273-
`<style>
280+
`<style>
274281
h1 {
275282
@apply w-full bg-fuchsia-50 p-20;
276283
}
@@ -331,7 +338,7 @@ import Custom from '../components/Custom.astro'
331338
],
332339
],
333340
},
334-
}
341+
},
335342
]
336343

337344
// Disable pug printer -- it produces noisy test output

0 commit comments

Comments
 (0)