From 006a2214b73a681efad35c6cb637588af50db575 Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Sat, 19 Sep 2026 11:00:57 -0400 Subject: [PATCH] fix(studio): escape every regex metacharacter when following a bound class list --- packages/studio/src/styles/classCandidates.test.ts | 6 ++++++ packages/studio/src/styles/classCandidates.ts | 6 +++--- packages/studio/src/utils/sourcePatcher.ts | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/studio/src/styles/classCandidates.test.ts b/packages/studio/src/styles/classCandidates.test.ts index 3ef1bcf24e..d120c23546 100644 --- a/packages/studio/src/styles/classCandidates.test.ts +++ b/packages/studio/src/styles/classCandidates.test.ts @@ -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 =
;`; + + 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. diff --git a/packages/studio/src/styles/classCandidates.ts b/packages/studio/src/styles/classCandidates.ts index dd04ff0ec1..10310a4d40 100644 --- a/packages/studio/src/styles/classCandidates.ts +++ b/packages/studio/src/styles/classCandidates.ts @@ -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)); } diff --git a/packages/studio/src/utils/sourcePatcher.ts b/packages/studio/src/utils/sourcePatcher.ts index 5f3c531cb6..fd799632ff 100644 --- a/packages/studio/src/utils/sourcePatcher.ts +++ b/packages/studio/src/utils/sourcePatcher.ts @@ -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, "\\$&"); }