fix(studio): escape every regex metacharacter when following a bound class list

This commit is contained in:
Miguel Angel Simon Sierra
2026-09-19 11:00:57 -04:00
parent c92a90c70e
commit 006a2214b7
3 changed files with 10 additions and 4 deletions
@@ -41,6 +41,12 @@ describe("extractClassCandidates", () => {
expect(bases(source)).toEqual(["h-ctl", "rounded-hologram"]);
});
it("follows an identifier that contains a dollar sign", () => {
const source = `const $cls = "flex-none";\nconst a = <div className={$cls} />;`;
expect(bases(source)).toEqual(["flex-none"]);
});
it("does not read a lookup key's default value as a class", () => {
// `variant` is the key, not the class list, so the default it falls back
// to is a map entry name and never reaches markup.
@@ -3,6 +3,8 @@
// `*Styles`/`*Classes` bindings). Bare strings are ignored so prose and paths never become "classes".
// Pure: text in, candidates out.
import { escapeRegex } from "../utils/sourcePatcher";
/** A class candidate and where it came from. */
export interface ClassCandidate {
/** The utility with its variant prefixes removed (`hover:bg-x` -> `bg-x`). */
@@ -251,9 +253,7 @@ function regionsAfter(mask: string, anchor: RegExp): [number, number][] {
// Every string bound to `name` by an assignment in this file. A name with no literal binding
// (parameter, import, call result) yields nothing, so an over-broad reference scan is safe.
function boundStrings(text: string, mask: string, name: string): string[] {
const anchor = new RegExp(
`\\b${name.replace(/\$/g, "\\$")}\\b(?:\\s*:[^=;{}()[\\]]*)?\\s*=(?!=)\\s*`,
);
const anchor = new RegExp(`\\b${escapeRegex(name)}\\b(?:\\s*:[^=;{}()[\\]]*)?\\s*=(?!=)\\s*`);
return regionsAfter(mask, anchor).flatMap(([start, end]) => stringsIn(text, mask, start, end));
}
+1 -1
View File
@@ -3,7 +3,7 @@
* Handles inline style updates, attribute changes, and text content.
*/
function escapeRegex(s: string): string {
export function escapeRegex(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}