Skip to content

Commit 686ba95

Browse files
committed
refactor: Update prettier-plugin-tailwindcss to version 0.6.11 and refactor code for improved readability in index.ts
1 parent 5082255 commit 686ba95

File tree

2 files changed

+75
-58
lines changed

2 files changed

+75
-58
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 73 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ function transformLiquid(ast: any, { env }: TransformerContext) {
356356
}) {
357357
return Array.isArray(node.name)
358358
? node.name.every(
359-
(n) => n.type === 'TextNode' && staticAttrs.has(n.value),
360-
)
359+
(n) => n.type === 'TextNode' && staticAttrs.has(n.value),
360+
)
361361
: staticAttrs.has(node.name)
362362
}
363363

@@ -549,18 +549,18 @@ function sortTemplateLiteral(
549549
quasi.value.cooked = same
550550
? quasi.value.raw
551551
: sortClasses(quasi.value.cooked, {
552-
env,
553-
ignoreFirst: i > 0 && !/^\s/.test(quasi.value.cooked),
554-
ignoreLast:
555-
i < node.expressions.length && !/\s$/.test(quasi.value.cooked),
556-
collapseWhitespace: collapseWhitespace && {
557-
start: collapseWhitespace && collapseWhitespace.start && i === 0,
558-
end:
559-
collapseWhitespace &&
560-
collapseWhitespace.end &&
561-
i >= node.expressions.length,
562-
},
563-
})
552+
env,
553+
ignoreFirst: i > 0 && !/^\s/.test(quasi.value.cooked),
554+
ignoreLast:
555+
i < node.expressions.length && !/\s$/.test(quasi.value.cooked),
556+
collapseWhitespace: collapseWhitespace && {
557+
start: collapseWhitespace && collapseWhitespace.start && i === 0,
558+
end:
559+
collapseWhitespace &&
560+
collapseWhitespace.end &&
561+
i >= node.expressions.length,
562+
},
563+
})
564564

565565
if (
566566
quasi.value.raw !== originalRaw ||
@@ -846,6 +846,23 @@ function transformTwig(ast: any, { env, changes }: TransformerContext) {
846846

847847
StringLiteral(node, path, meta) {
848848
if (!meta.sortTextNodes) {
849+
// Check for .addClass() calls in Drupal Twig templates
850+
const isAddClassCall = path.some((entry) => {
851+
return (
852+
entry.parent &&
853+
entry.parent.type === 'CallExpression' &&
854+
entry.parent.callee &&
855+
entry.parent.callee.type === 'MemberExpression' &&
856+
entry.parent.callee.property &&
857+
entry.parent.callee.property.name === 'addClass'
858+
)
859+
})
860+
861+
if (isAddClassCall) {
862+
node.value = sortClasses(node.value, { env })
863+
return
864+
}
865+
849866
return
850867
}
851868

@@ -952,12 +969,12 @@ function transformSvelte(ast: any, { env, changes }: TransformerContext) {
952969
value.data = same
953970
? value.raw
954971
: sortClasses(value.data, {
955-
env,
956-
ignoreFirst: i > 0 && !/^\s/.test(value.data),
957-
ignoreLast: i < attr.value.length - 1 && !/\s$/.test(value.data),
958-
removeDuplicates: false,
959-
collapseWhitespace: false,
960-
})
972+
env,
973+
ignoreFirst: i > 0 && !/^\s/.test(value.data),
974+
ignoreLast: i < attr.value.length - 1 && !/\s$/.test(value.data),
975+
removeDuplicates: false,
976+
collapseWhitespace: false,
977+
})
961978
} else if (value.type === 'MustacheTag') {
962979
visit(value.expression, {
963980
Literal(node) {
@@ -1135,58 +1152,58 @@ export const parsers: Record<string, Parser> = {
11351152

11361153
...(base.parsers.svelte
11371154
? {
1138-
svelte: createParser('svelte', transformSvelte, {
1139-
staticAttrs: ['class'],
1140-
}),
1141-
}
1155+
svelte: createParser('svelte', transformSvelte, {
1156+
staticAttrs: ['class'],
1157+
}),
1158+
}
11421159
: {}),
11431160
...(base.parsers.astro
11441161
? {
1145-
astro: createParser('astro', transformAstro, {
1146-
staticAttrs: ['class', 'className'],
1147-
dynamicAttrs: ['class:list', 'className'],
1148-
}),
1149-
}
1162+
astro: createParser('astro', transformAstro, {
1163+
staticAttrs: ['class', 'className'],
1164+
dynamicAttrs: ['class:list', 'className'],
1165+
}),
1166+
}
11501167
: {}),
11511168
...(base.parsers.astroExpressionParser
11521169
? {
1153-
astroExpressionParser: createParser(
1154-
'astroExpressionParser',
1155-
transformJavaScript,
1156-
{
1157-
staticAttrs: ['class'],
1158-
dynamicAttrs: ['class:list'],
1159-
},
1160-
),
1161-
}
1170+
astroExpressionParser: createParser(
1171+
'astroExpressionParser',
1172+
transformJavaScript,
1173+
{
1174+
staticAttrs: ['class'],
1175+
dynamicAttrs: ['class:list'],
1176+
},
1177+
),
1178+
}
11621179
: {}),
11631180
...(base.parsers.marko
11641181
? {
1165-
marko: createParser('marko', transformMarko, {
1166-
staticAttrs: ['class'],
1167-
}),
1168-
}
1182+
marko: createParser('marko', transformMarko, {
1183+
staticAttrs: ['class'],
1184+
}),
1185+
}
11691186
: {}),
11701187
...(base.parsers.twig
11711188
? {
1172-
twig: createParser('twig', transformTwig, {
1173-
staticAttrs: ['class'],
1174-
}),
1175-
}
1189+
twig: createParser('twig', transformTwig, {
1190+
staticAttrs: ['class'],
1191+
}),
1192+
}
11761193
: {}),
11771194
...(base.parsers.pug
11781195
? {
1179-
pug: createParser('pug', transformPug, {
1180-
staticAttrs: ['class'],
1181-
}),
1182-
}
1196+
pug: createParser('pug', transformPug, {
1197+
staticAttrs: ['class'],
1198+
}),
1199+
}
11831200
: {}),
11841201
...(base.parsers['liquid-html']
11851202
? {
1186-
'liquid-html': createParser('liquid-html', transformLiquid, {
1187-
staticAttrs: ['class'],
1188-
}),
1189-
}
1203+
'liquid-html': createParser('liquid-html', transformLiquid, {
1204+
staticAttrs: ['class'],
1205+
}),
1206+
}
11901207
: {}),
11911208
}
11921209

@@ -1230,6 +1247,6 @@ export interface PluginOptions {
12301247
}
12311248

12321249
declare module 'prettier' {
1233-
interface RequiredOptions extends PluginOptions {}
1234-
interface ParserOptions extends PluginOptions {}
1250+
interface RequiredOptions extends PluginOptions { }
1251+
interface ParserOptions extends PluginOptions { }
12351252
}

0 commit comments

Comments
 (0)