Skip to content

Commit 1a2133b

Browse files
committed
Refactor
1 parent 5c60389 commit 1a2133b

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

src/index.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,6 @@ let base = await loadPlugins()
2121

2222
const ESCAPE_SEQUENCE_PATTERN = /\\(['"\\nrtbfv0-7xuU])/g
2323

24-
/**
25-
* Helper function to check if an attribute name matches either exact strings or regex patterns
26-
*/
27-
function matchesAttribute(attrName: string, attrs: Set<string>, regexes: RegExp[]): boolean {
28-
// First check for exact match
29-
if (attrs.has(attrName)) {
30-
return true
31-
}
32-
33-
// Then check regex patterns
34-
for (const regex of regexes) {
35-
if (regex.test(attrName)) {
36-
return true
37-
}
38-
}
39-
40-
return false
41-
}
42-
4324
function createParser(
4425
parserFormat: string,
4526
transform: (ast: any, context: TransformerContext) => void,
@@ -298,9 +279,9 @@ function transformHtml(ast: any, { env, changes }: TransformerContext) {
298279
let { parser } = env.options
299280

300281
for (let attr of ast.attrs ?? []) {
301-
if (matchesAttribute(attr.name, staticAttrs, staticAttrsRegex)) {
282+
if (hasMatch(attr.name, staticAttrs, staticAttrsRegex)) {
302283
attr.value = sortClasses(attr.value, { env })
303-
} else if (matchesAttribute(attr.name, dynamicAttrs, dynamicAttrsRegex)) {
284+
} else if (hasMatch(attr.name, dynamicAttrs, dynamicAttrsRegex)) {
304285
if (!/[`'"]/.test(attr.value)) {
305286
continue
306287
}
@@ -323,7 +304,7 @@ function transformGlimmer(ast: any, { env }: TransformerContext) {
323304

324305
visit(ast, {
325306
AttrNode(attr, _path, meta) {
326-
if (matchesAttribute(attr.name, staticAttrs, staticAttrsRegex) && attr.value) {
307+
if (hasMatch(attr.name, staticAttrs, staticAttrsRegex) && attr.value) {
327308
meta.sortTextNodes = true
328309
}
329310
},
@@ -379,8 +360,8 @@ function transformLiquid(ast: any, { env }: TransformerContext) {
379360

380361
function isClassAttr(node: { name: string | { type: string; value: string }[] }) {
381362
return Array.isArray(node.name)
382-
? node.name.every((n) => n.type === 'TextNode' && matchesAttribute(n.value, staticAttrs, staticAttrsRegex))
383-
: matchesAttribute(node.name, staticAttrs, staticAttrsRegex)
363+
? node.name.every((n) => n.type === 'TextNode' && hasMatch(n.value, staticAttrs, staticAttrsRegex))
364+
: hasMatch(node.name, staticAttrs, staticAttrsRegex)
384365
}
385366

386367
function hasSurroundingQuotes(str: string) {
@@ -620,7 +601,7 @@ function isSortableExpression(
620601
}
621602

622603
if (node.type === 'Identifier') {
623-
return functions.has(node.name)
604+
return hasMatch(node.name, functions, [])
624605
}
625606

626607
return false
@@ -706,7 +687,7 @@ function transformJavaScript(ast: import('@babel/types').Node, { env }: Transfor
706687
return
707688
}
708689

709-
if (!matchesAttribute(node.name.name, staticAttrs, staticAttrsRegex)) {
690+
if (!hasMatch(node.name.name, staticAttrs, staticAttrsRegex)) {
710691
return
711692
}
712693

@@ -909,7 +890,7 @@ function transformTwig(ast: any, { env, changes }: TransformerContext) {
909890
}
910891

911892
if (node.type === 'Identifier') {
912-
if (!functions.has(node.name)) return
893+
if (!hasMatch(node.name, functions, [])) return
913894
}
914895

915896
meta.sortTextNodes = true
@@ -1090,6 +1071,19 @@ function transformSvelte(ast: any, { env, changes }: TransformerContext) {
10901071
}
10911072
}
10921073

1074+
/**
1075+
* Check for matches against a static list or possible regex patterns
1076+
*/
1077+
function hasMatch(name: string, list: Set<string>, patterns: RegExp[]): boolean {
1078+
if (list.has(name)) return true
1079+
1080+
for (let regex of patterns) {
1081+
if (regex.test(name)) return true
1082+
}
1083+
1084+
return false
1085+
}
1086+
10931087
export { options } from './options.js'
10941088

10951089
export const printers: Record<string, Printer> = (function () {

0 commit comments

Comments
 (0)