Skip to content

Commit 9b9aeae

Browse files
mizdraclaude
andauthored
fix(core): use matchAll instead of exec in findUsedTokenNames (#333)
* fix(core): use `String.prototype.matchAll` instead of `RegExp.prototype.exec` in `findUsedTokenNames` Replace `RegExp.prototype.exec` with `String.prototype.matchAll` to avoid potential issues with shared `lastIndex` state on the module-scoped `TOKEN_CONSUMER_PATTERN` regex. `matchAll` internally copies the regex, making the function robust against future changes that might interrupt the loop early. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: add changeset for findUsedTokenNames fix Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c4b6e2e commit 9b9aeae

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

.changeset/fix-regexp-exec.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@css-modules-kit/core': patch
3+
---
4+
5+
fix(core): use `matchAll` instead of `exec` in `findUsedTokenNames` to avoid potential `lastIndex` state issues

packages/core/src/util.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ const TOKEN_CONSUMER_PATTERN = /styles\.([$_\p{ID_Start}][$\u200c\u200d\p{ID_Con
4343

4444
export function findUsedTokenNames(componentText: string): Set<string> {
4545
const usedClassNames = new Set<string>();
46-
let match;
47-
while ((match = TOKEN_CONSUMER_PATTERN.exec(componentText)) !== null) {
46+
for (const match of componentText.matchAll(TOKEN_CONSUMER_PATTERN)) {
4847
usedClassNames.add(match[1]!);
4948
}
5049
return usedClassNames;

0 commit comments

Comments
 (0)