Skip to content

Commit 64762d8

Browse files
committed
Improve attribute matching logic and documentation
1 parent f94f33b commit 64762d8

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ If a local configuration file cannot be found the plugin will fallback to the de
7070

7171
By default this plugin sorts classes in the `class` attribute, any framework-specific equivalents like `className`, `:class`, `[ngClass]`, and any Tailwind `@apply` directives.
7272

73-
You can sort additional attributes using the `tailwindAttributes` option, which takes an array of attribute names:
73+
You can extend this behavior to sort classes in any attribute using the following options:
74+
75+
- `tailwindAttributes`: An array of exact attribute names to sort.
76+
- `tailwindAttributesStartsWith`: An array of prefixes to match attributes that begin with a certain string.
77+
- `tailwindAttributesEndsWith`: An array of suffixes to match attributes that end with a certain string.
78+
79+
#### Example 1
7480

7581
```json5
7682
// .prettierrc
@@ -91,17 +97,16 @@ function MyButton({ children }) {
9197
}
9298
```
9399

94-
#### Matching attributes with a prefix or suffix
100+
#### Example 2
95101

96102
```json5
97103
// .prettierrc
98104
{
99-
"tailwindAttributesStartsWith": ["data"],
100105
"tailwindAttributesEndsWith": ["ClassName"]
101106
}
102107
```
103108

104-
With this configuration, attributes like `data-active-classes` and `buttonClassName` will be sorted:
109+
With this configuration, any class found with suffix `ClassName` will be sorted:
105110

106111
```jsx
107112
function MyButton({ children }) {

src/index.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ function transformHtml(ast: any, { env, changes }: TransformerContext) {
279279
let { parser } = env.options
280280

281281
for (let attr of ast.attrs ?? []) {
282-
if (isSortableAttribute(env.customizations, attr.name)) {
282+
if (isSortableAttribute(attr.name, env.customizations)) {
283283
attr.value = sortClasses(attr.value, { env })
284284
} else if (dynamicAttrs.has(attr.name)) {
285285
if (!/[`'"]/.test(attr.value)) {
@@ -302,7 +302,7 @@ function transformHtml(ast: any, { env, changes }: TransformerContext) {
302302
function transformGlimmer(ast: any, { env }: TransformerContext) {
303303
visit(ast, {
304304
AttrNode(attr, _path, meta) {
305-
if (isSortableAttribute(env.customizations, attr.name) && attr.value) {
305+
if (isSortableAttribute(attr.name, env.customizations) && attr.value) {
306306
meta.sortTextNodes = true
307307
}
308308
},
@@ -356,8 +356,8 @@ function transformGlimmer(ast: any, { env }: TransformerContext) {
356356
function transformLiquid(ast: any, { env }: TransformerContext) {
357357
function isClassAttr(node: { name: string | { type: string; value: string }[] }) {
358358
return Array.isArray(node.name)
359-
? node.name.every((n) => n.type === 'TextNode' && isSortableAttribute(env.customizations, n.value))
360-
: isSortableAttribute(env.customizations, node.name)
359+
? node.name.every((n) => n.type === 'TextNode' && isSortableAttribute(n.value, env.customizations))
360+
: isSortableAttribute(node.name, env.customizations)
361361
}
362362

363363
function hasSurroundingQuotes(str: string) {
@@ -564,7 +564,7 @@ function sortTemplateLiteral(
564564
return didChange
565565
}
566566

567-
function isSortableAttribute(customizations: Customizations, name: string) {
567+
function isSortableAttribute(name: string, customizations: Customizations) {
568568
const { staticAttrs, suffixAttrs, prefixAttrs } = customizations
569569

570570
if (staticAttrs.has(name)) return true
@@ -699,7 +699,7 @@ function transformJavaScript(ast: import('@babel/types').Node, { env }: Transfor
699699
return
700700
}
701701

702-
if (!isSortableAttribute(env.customizations, node.name.name)) {
702+
if (!isSortableAttribute(node.name.name, env.customizations)) {
703703
return
704704
}
705705

@@ -805,7 +805,7 @@ function transformAstro(ast: any, { env, changes }: TransformerContext) {
805805

806806
if (ast.type === 'element' || ast.type === 'custom-element' || ast.type === 'component') {
807807
for (let attr of ast.attributes ?? []) {
808-
if (isSortableAttribute(env.customizations, attr.name) && attr.type === 'attribute' && attr.kind === 'quoted') {
808+
if (isSortableAttribute(attr.name, env.customizations) && attr.type === 'attribute' && attr.kind === 'quoted') {
809809
attr.value = sortClasses(attr.value, {
810810
env,
811811
})
@@ -844,7 +844,7 @@ function transformMarko(ast: any, { env }: TransformerContext) {
844844
nodesToVisit.push(...currentNode.body)
845845
break
846846
case 'MarkoAttribute':
847-
if (!isSortableAttribute(env.customizations, currentNode.name)) break
847+
if (!isSortableAttribute(currentNode.name, env.customizations)) break
848848
switch (currentNode.value.type) {
849849
case 'ArrayExpression':
850850
const classList = currentNode.value.elements
@@ -866,15 +866,13 @@ function transformMarko(ast: any, { env }: TransformerContext) {
866866
}
867867

868868
function transformTwig(ast: any, { env, changes }: TransformerContext) {
869-
let { staticAttrs } = env.customizations
870-
871869
for (let child of ast.expressions ?? []) {
872870
transformTwig(child, { env, changes })
873871
}
874872

875873
visit(ast, {
876874
Attribute(node, _path, meta) {
877-
if (!isSortableAttribute(env.customizations, node.name.name)) return
875+
if (!isSortableAttribute(node.name.name, env.customizations)) return
878876

879877
meta.sortTextNodes = true
880878
},
@@ -912,7 +910,7 @@ function transformPug(ast: any, { env }: TransformerContext) {
912910

913911
// First sort the classes in attributes
914912
for (const token of ast.tokens) {
915-
if (token.type === 'attribute' && isSortableAttribute(env.customizations, token.name)) {
913+
if (token.type === 'attribute' && isSortableAttribute(token.name, env.customizations)) {
916914
token.val = [token.val.slice(0, 1), sortClasses(token.val.slice(1, -1), { env }), token.val.slice(-1)].join('')
917915
}
918916
}
@@ -958,7 +956,7 @@ function transformPug(ast: any, { env }: TransformerContext) {
958956

959957
function transformSvelte(ast: any, { env, changes }: TransformerContext) {
960958
for (let attr of ast.attributes ?? []) {
961-
if (!isSortableAttribute(env.customizations, attr.name) || attr.type !== 'Attribute') {
959+
if (!isSortableAttribute(attr.name, env.customizations) || attr.type !== 'Attribute') {
962960
continue
963961
}
964962

0 commit comments

Comments
 (0)