Skip to content

Commit 9ca81da

Browse files
mizdraclaude
andauthored
fix(core): support bracket notation in findUsedTokenNames (#125) (#334)
Support `styles['foo']` and `styles["foo"]` bracket notation in addition to the existing `styles.foo` dot notation. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9b9aeae commit 9ca81da

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

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): support `styles['foo']` and `styles["foo"]` bracket notation in `findUsedTokenNames`

packages/core/src/util.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ test('findUsedTokenNames', () => {
3737
styles.a_1;
3838
styles.a_1;
3939
styles.a_2;
40-
styles['a_3'];
41-
styles["a_4"];
42-
styles[\`a_5\`];
40+
styles['a-3'];
41+
styles["a-4"];
42+
styles[\`a-5\`];
4343
// styles.a_6; // false positive, but it is acceptable for simplicity of implementation
44-
styles['a_7;
45-
styles['a_8"];
44+
styles['a-7;
45+
styles['a-8"];
4646
styles;
4747
`;
48-
const expected = new Set(['a_1', 'a_2', 'a_6']);
48+
const expected = new Set(['a_1', 'a_2', 'a-3', 'a-4', 'a_6']);
4949
expect(findUsedTokenNames(text)).toEqual(expected);
5050
});

packages/core/src/util.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@ export function validateTokenName(name: string, options: ValidateTokenNameOption
3434

3535
/**
3636
* The syntax pattern for consuming tokens imported from CSS Module.
37-
* @example `styles.foo`
37+
* @example `styles.foo`, `styles['foo']`, `styles["foo"]`
3838
*/
39-
// TODO(#125): Support `styles['foo']` and `styles["foo"]`
4039
// MEMO: The `xxxStyles.foo` format is not supported, because the css module file for current component file is usually imported with `styles`.
4140
// It is sufficient to support only the `styles.foo` format.
42-
const TOKEN_CONSUMER_PATTERN = /styles\.([$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*)/gu;
41+
const TOKEN_CONSUMER_PATTERN =
42+
/styles(?:\.([$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*)|\['([^']*?)'\]|\["([^"]*?)"\])/gu;
4343

4444
export function findUsedTokenNames(componentText: string): Set<string> {
4545
const usedClassNames = new Set<string>();
4646
for (const match of componentText.matchAll(TOKEN_CONSUMER_PATTERN)) {
47-
usedClassNames.add(match[1]!);
47+
const name = match[1] ?? match[2] ?? match[3];
48+
if (name) usedClassNames.add(name);
4849
}
4950
return usedClassNames;
5051
}

0 commit comments

Comments
 (0)