Skip to content

Commit c712a7f

Browse files
committed
Drop dynamicAttrsRegex
Dynamic attribute names have definite rules in Vue and Angular. Testing those rules directly and matching the actual “name” portion against the regex is a better option. We still match against the static `dynamicAttrs` list for compat reasons
1 parent d5c3670 commit c712a7f

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/options.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ export function createMatcher(options: RequiredOptions, parser: string, defaults
7272
let dynamicAttrs = new Set<string>(defaults.dynamicAttrs)
7373
let functions = new Set<string>(defaults.functions)
7474
let staticAttrsRegex: RegExp[] = [...defaults.staticAttrsRegex]
75-
let dynamicAttrsRegex: RegExp[] = [...defaults.dynamicAttrsRegex]
7675
let functionsRegex: RegExp[] = [...defaults.functionsRegex]
7776

7877
// Create a list of "static" attributes
@@ -104,15 +103,6 @@ export function createMatcher(options: RequiredOptions, parser: string, defaults
104103
}
105104
}
106105

107-
for (let regex of staticAttrsRegex) {
108-
if (parser === 'vue') {
109-
dynamicAttrsRegex.push(new RegExp(`:${regex.source}`, regex.flags))
110-
dynamicAttrsRegex.push(new RegExp(`v-bind:${regex.source}`, regex.flags))
111-
} else if (parser === 'angular') {
112-
dynamicAttrsRegex.push(new RegExp(`\\[${regex.source}\\]`, regex.flags))
113-
}
114-
}
115-
116106
// Generate a list of supported functions
117107
for (let fn of options.tailwindFunctions ?? []) {
118108
let regex = parseRegex(fn)
@@ -126,11 +116,34 @@ export function createMatcher(options: RequiredOptions, parser: string, defaults
126116

127117
return {
128118
hasStaticAttr: (name: string) => hasMatch(name, staticAttrs, staticAttrsRegex),
129-
hasDynamicAttr: (name: string) => hasMatch(name, dynamicAttrs, dynamicAttrsRegex),
119+
hasDynamicAttr: (name: string) => {
120+
if (hasMatch(name, dynamicAttrs, [])) return true
121+
122+
let newName = nameFromDynamicAttr(name, parser)
123+
if (!newName) return false
124+
125+
return hasMatch(newName, staticAttrs, staticAttrsRegex)
126+
},
130127
hasFunction: (name: string) => hasMatch(name, functions, functionsRegex),
131128
}
132129
}
133130

131+
function nameFromDynamicAttr(name: string, parser: string) {
132+
if (parser === 'vue') {
133+
if (name.startsWith(':')) return name.slice(1)
134+
if (name.startsWith('v-bind:')) return name.slice(7)
135+
if (name.startsWith('v-')) return name
136+
return null
137+
}
138+
139+
if (parser === 'angular') {
140+
if (name.startsWith('[') && name.endsWith(']')) return name.slice(1, -1)
141+
return null
142+
}
143+
144+
return null
145+
}
146+
134147
/**
135148
* Check for matches against a static list or possible regex patterns
136149
*/

0 commit comments

Comments
 (0)