Skip to content

feat: Add support for callable template literals when using custom JSX parser #367

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 4 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
- 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))

## [0.6.11] - 2025-01-23

Expand Down
53 changes: 20 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,24 +579,7 @@ function isSortableTemplateExpression(
| import('ast-types').namedTypes.TaggedTemplateExpression,
functions: Set<string>,
): boolean {
if (node.tag.type === 'Identifier') {
return functions.has(node.tag.name)
}

if (node.tag.type === 'MemberExpression') {
let expr = node.tag.object

// If the tag is a MemberExpression we should traverse all MemberExpression's until we find the leading Identifier
while (expr.type === 'MemberExpression') {
expr = expr.object
}

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

return false
return isSortableExpression(node.tag, functions)
}

function isSortableCallExpression(
Expand All @@ -605,25 +588,29 @@ function isSortableCallExpression(
| import('ast-types').namedTypes.CallExpression,
functions: Set<string>,
): boolean {
if (!node.arguments?.length) {
return false
}

if (node.callee.type === 'Identifier') {
return functions.has(node.callee.name)
}
if (!node.arguments?.length) return false

if (node.callee.type === 'MemberExpression') {
let expr = node.callee.object
return isSortableExpression(node.callee, functions)
}

// If the tag is a MemberExpression we should traverse all MemberExpression's until we find the leading Identifier
while (expr.type === 'MemberExpression') {
expr = expr.object
function isSortableExpression(
node:
| import('@babel/types').Expression
| import('@babel/types').V8IntrinsicIdentifier
| import('ast-types').namedTypes.ASTNode,
functions: Set<string>,
): boolean {
// Traverse property accesses and function calls to find the leading ident
while (node.type === 'CallExpression' || node.type === 'MemberExpression') {
if (node.type === 'CallExpression') {
node = node.callee
} else if (node.type === 'MemberExpression') {
node = node.object
}
}

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

return false
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/custom-jsx/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const k = tw.foo('sm:p-1 p-2');
const l = tw.foo.bar('sm:p-1 p-2');
const m = no.foo('sm:p-1 p-2');
const n = no.tw('sm:p-1 p-2');
const o = tw(Foo)`sm:p-1 p-2`;
const p = tw(Foo)(Bar)`sm:p-1 p-2`;
const q = no(Foo)`sm:p-1 p-2`;
const r = no.tw(Foo)`sm:p-1 p-2`;
const s = tw(Foo)('sm:p-1 p-2');
const t = tw(Foo)(Bar)('sm:p-1 p-2');
const u = no(Foo)('sm:p-1 p-2');
const v = no.tw(Foo)('sm:p-1 p-2');
const w = tw.div(Foo)`sm:p-1 p-2`;
const x = tw(Foo).div`sm:p-1 p-2`;
const y = no.tw(Foo)`sm:p-1 p-2`;
const z = no(Foo).tw`sm:p-1 p-2`;

const A = (props) => <div className={props.sortMe} />;
const B = () => <A sortMe="sm:p-1 p-2" dontSort="sm:p-1 p-2" />;
12 changes: 12 additions & 0 deletions tests/fixtures/custom-jsx/output.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const k = tw.foo("p-2 sm:p-1");
const l = tw.foo.bar("p-2 sm:p-1");
const m = no.foo("sm:p-1 p-2");
const n = no.tw("sm:p-1 p-2");
const o = tw(Foo)`p-2 sm:p-1`;
const p = tw(Foo)(Bar)`p-2 sm:p-1`;
const q = no(Foo)`sm:p-1 p-2`;
const r = no.tw(Foo)`sm:p-1 p-2`;
const s = tw(Foo)("p-2 sm:p-1");
const t = tw(Foo)(Bar)("p-2 sm:p-1");
const u = no(Foo)("sm:p-1 p-2");
const v = no.tw(Foo)("sm:p-1 p-2");
const w = tw.div(Foo)`p-2 sm:p-1`;
const x = tw(Foo).div`p-2 sm:p-1`;
const y = no.tw(Foo)`sm:p-1 p-2`;
const z = no(Foo).tw`sm:p-1 p-2`;

const A = (props) => <div className={props.sortMe} />;
const B = () => <A sortMe="p-2 sm:p-1" dontSort="sm:p-1 p-2" />;