Skip to content

Commit 047d591

Browse files
Support member access on function calls (#218)
* Refactor * Fix test script * Support member access on function calls * Update changelog
1 parent b300077 commit 047d591

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

CHANGELOG.md

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

1212
- Sort classes inside `className` in Astro ([#215](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/215))
13+
- Support member access on function calls ([#218](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/218))
1314

1415
## [0.5.4] - 2023-08-31
1516

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"predev": "npm run _pre",
2525
"dev": "npm run _esbuild -- --watch",
2626
"pretest": "node scripts/install-fixture-deps.js",
27-
"test": "jest",
27+
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
2828
"prepublishOnly": "npm run build && node scripts/copy-licenses.js",
2929
"format": "prettier \"src/**/*.js\" \"scripts/**/*.js\" \"tests/test.js\" --write --print-width 100 --single-quote --no-semi --plugin-search-dir ./tests",
3030
"release-channel": "node ./scripts/release-channel.js",

src/index.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,37 @@ function isSortableTemplateExpression(node, functions) {
460460
return false
461461
}
462462

463+
/**
464+
*
465+
* @param {import('@babel/types').CallExpression | import('ast-types').namedTypes.CallExpression} node
466+
* @param {Set<string>} functions
467+
* @returns {boolean}
468+
*/
469+
function isSortableCallExpression(node, functions) {
470+
if (!node.arguments?.length) {
471+
return false
472+
}
473+
474+
if (node.callee.type === 'Identifier') {
475+
return functions.has(node.callee.name)
476+
}
477+
478+
if (node.callee.type === 'MemberExpression') {
479+
let expr = node.callee.object
480+
481+
// If the tag is a MemberExpression we should traverse all MemberExpression's until we find the leading Identifier
482+
while (expr.type === 'MemberExpression') {
483+
expr = expr.object
484+
}
485+
486+
if (expr.type === 'Identifier') {
487+
return functions.has(expr.name)
488+
}
489+
}
490+
491+
return false
492+
}
493+
463494
/**
464495
* @param {import('@babel/types').Node} ast
465496
* @param {TransformerContext} param1
@@ -502,11 +533,7 @@ function transformJavaScript(ast, { env }) {
502533

503534
/** @param {import('@babel/types').CallExpression} node */
504535
CallExpression(node) {
505-
if (!node.arguments?.length) {
506-
return
507-
}
508-
509-
if (!functions.has(node.callee?.name ?? '')) {
536+
if (!isSortableCallExpression(node, functions)) {
510537
return
511538
}
512539

tests/fixtures.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ const f = tw.foo\`p-2 sm:p-1\`;
7676
const g = tw.foo.bar\`p-2 sm:p-1\`;
7777
const h = no.foo\`sm:p-1 p-2\`;
7878
const i = no.tw\`sm:p-1 p-2\`;
79+
const k = tw.foo("p-2 sm:p-1");
80+
const l = tw.foo.bar("p-2 sm:p-1");
81+
const m = no.foo("sm:p-1 p-2");
82+
const n = no.tw("sm:p-1 p-2");
7983
8084
const A = (props) => <div className={props.sortMe} />;
8185
const B = () => <A sortMe="p-2 sm:p-1" dontSort="sm:p-1 p-2" />;`,

tests/fixtures/custom-jsx/index.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const f = tw.foo`sm:p-1 p-2`;
1010
const g = tw.foo.bar`sm:p-1 p-2`;
1111
const h = no.foo`sm:p-1 p-2`;
1212
const i = no.tw`sm:p-1 p-2`;
13+
const k = tw.foo('sm:p-1 p-2');
14+
const l = tw.foo.bar('sm:p-1 p-2');
15+
const m = no.foo('sm:p-1 p-2');
16+
const n = no.tw('sm:p-1 p-2');
1317

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

0 commit comments

Comments
 (0)