Skip to content

Commit 3819676

Browse files
authored
fix: improve CSS classname regexp (#205)
1 parent 9ef8e07 commit 3819676

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/helpers/createDtsExports.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,30 @@ export default classes;
7676
.filter(([classname]) => isValidVariable(classname));
7777

7878
filteredClasses.forEach(([classname, originalClassname]) => {
79-
const classRegexp = new RegExp(`${originalClassname}[\\s{]`, 'g');
80-
81-
const matchedLine = cssLines.findIndex((line) => classRegexp.test(line));
82-
const matchedColumn =
83-
matchedLine && cssLines[matchedLine].indexOf(originalClassname);
79+
let matchedLine;
80+
let matchedColumn;
81+
82+
for (let i = 0; i < cssLines.length; i++) {
83+
const match = new RegExp(
84+
// NOTE: This excludes any match not starting with:
85+
// - `.` for classnames,
86+
// - `:` or ` ` for animation names,
87+
// and any matches followed by valid CSS selector characters.
88+
`[:.\\s]${originalClassname}(?![_a-zA-Z0-9-])`,
89+
'g',
90+
).exec(cssLines[i]);
91+
92+
if (match) {
93+
matchedLine = i;
94+
matchedColumn = match.index;
95+
break;
96+
}
97+
}
8498

8599
const { line: lineNumber } = smc.originalPositionFor({
86100
// Lines start at 1, not 0.
87-
line: matchedLine >= 0 ? matchedLine + 1 : 1,
88-
column: matchedColumn >= 0 ? matchedColumn : 0,
101+
line: matchedLine ? matchedLine + 1 : 1,
102+
column: matchedColumn ? matchedColumn : 0,
89103
});
90104

91105
dtsLines[lineNumber ? lineNumber - 1 : 0] +=

0 commit comments

Comments
 (0)