Skip to content

fix: improve CSS classname regexp #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/helpers/createDtsExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,30 @@ export default classes;
.filter(([classname]) => isValidVariable(classname));

filteredClasses.forEach(([classname, originalClassname]) => {
const classRegexp = new RegExp(`${originalClassname}[\\s{]`, 'g');

const matchedLine = cssLines.findIndex((line) => classRegexp.test(line));
const matchedColumn =
matchedLine && cssLines[matchedLine].indexOf(originalClassname);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, matchedLine could be -1. This shouldn't have happened, but the newly introduced regexp above didn't cover enough cases.

let matchedLine;
let matchedColumn;

for (let i = 0; i < cssLines.length; i++) {
const match = new RegExp(
// NOTE: This excludes any match not starting with:
// - `.` for classnames,
// - `:` or ` ` for animation names,
// and any matches followed by valid CSS selector characters.
`[:.\\s]${originalClassname}(?![_a-zA-Z0-9-])`,
'g',
).exec(cssLines[i]);

if (match) {
matchedLine = i;
matchedColumn = match.index;
break;
}
}

const { line: lineNumber } = smc.originalPositionFor({
// Lines start at 1, not 0.
line: matchedLine >= 0 ? matchedLine + 1 : 1,
column: matchedColumn >= 0 ? matchedColumn : 0,
line: matchedLine ? matchedLine + 1 : 1,
column: matchedColumn ? matchedColumn : 0,
});

dtsLines[lineNumber ? lineNumber - 1 : 0] +=
Expand Down