Skip to content

Allow sorting classes inside function calls in Twig templates #358

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Format quotes in `@source`, `@plugin`, and `@config` ([#387](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/387))
- Support sorting in callable template literals ([#367](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/367))
- Support sorting in function calls mixed with property accesses ([#367](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/367))
- Support sorting in function calls in Twig ([#358](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/358))

## [0.6.14] - 2025-07-09

Expand Down
27 changes: 26 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ function transformMarko(ast: any, { env }: TransformerContext) {
}

function transformTwig(ast: any, { env, changes }: TransformerContext) {
let { staticAttrs } = env.customizations
let { staticAttrs, functions } = env.customizations

for (let child of ast.expressions ?? []) {
transformTwig(child, { env, changes })
Expand All @@ -911,6 +911,31 @@ function transformTwig(ast: any, { env, changes }: TransformerContext) {
meta.sortTextNodes = true
},

CallExpression(node, _path, meta) {
// Traverse property accesses and function calls to find the *trailing* ident
while (
node.type === 'CallExpression' ||
node.type === 'MemberExpression'
) {
if (node.type === 'CallExpression') {
node = node.callee
} else if (node.type === 'MemberExpression') {
// TODO: This is *different* than `isSortableExpression` and that doesn't feel right
// but they're mutually exclusive implementations
//
// This is to handle foo.fnNameHere(…) where `isSortableExpression` is intentionally
// handling `fnNameHere.foo(…)`.
node = node.property
}
}

if (node.type === 'Identifier') {
if (!functions.has(node.name)) return
}

meta.sortTextNodes = true
},

StringLiteral(node, path, meta) {
if (!meta.sortTextNodes) {
return
Expand Down
17 changes: 17 additions & 0 deletions tests/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ let tests: PluginTest[] = [
plugins: ['@zackad/prettier-plugin-twig'],
options: {
twigAlwaysBreakObjects: false,
tailwindFunctions: ['addClass', 'tw'],
},
tests: {
twig: [
Expand Down Expand Up @@ -164,6 +165,22 @@ let tests: PluginTest[] = [
`<div class="{{ ' flex ' + ' underline ' + ' block ' }}"></div>`,
`<div class="{{ 'flex ' + ' underline' + ' block' }}"></div>`,
],

// Function call tests
t`<div {{ tw('${yes}') }}></div>`,
t`<div {{ attributes.addClass('${yes}') }}></div>`,

t`{{ tw('${yes}') }}`,
t`{{ attributes.addClass('${yes}') }}`,

t`{{ tw('${yes}').tw('${yes}').tw('${yes}') }}`,
t`{{ attributes.addClass('${yes}').addClass('${yes}').addClass('${yes}') }}`,

t`{% set className = '${no}' %} {{ attributes.addClass(className) }}`,
[
`{{ attributes.addClass("sm:p-0 " ~ variant ~ " p-0") }}`,
`{{ attributes.addClass('sm:p-0 ' ~ variant ~ ' p-0') }}`,
],
],
},
},
Expand Down