/** * Shared reference-rewriting helper for the runtime "state access" * transforms in scriptParser.ts and moduleExecutor.ts. * * Rewrites standalone references to a reactive variable into * `__state__.varName`. A plain regex substitution corrupts ES6 * object-literal shorthand, the one place a bare identifier reference * cannot be replaced by a member expression: * * $emit("msg", { text, name }) // `name` is a reactive variable * plain rewrite → { text, __state__.name } // SyntaxError! * this helper → { text, name: __state__.name } // valid + reactive * * Destructuring DECLARATIONS (`const { name } = obj`) are left untouched: * they declare a local binding that shadows the state variable, and * rewriting the pattern would be a SyntaxError. Destructuring ASSIGNMENTS * (`({ name } = obj)`) are object literals in expression position, so the * shorthand expansion yields `({ name: __state__.name } = obj)` — valid, * and it correctly writes into reactive state. * * Callers must mask string literals BEFORE calling this (both transforms * already protect strings with placeholders); otherwise string contents * would be rewritten and could confuse the bracket scan. * * Known limitations (unchanged from the previous inline rewrites): * - Destructuring with defaults (`const { name = 1 } = o`) and state-named * function parameters are not scope-analyzed. * - Shorthand inside a plain statement block that happens to look like a * slot (e.g. a comma expression `{ name, other; }`) is treated as a * labeled statement after expansion, which is still valid JS. */ /** * Full state-access transform shared by scriptParser.ts (component scripts * and re-created event-handler function definitions) and moduleExecutor.ts * (module scripts). * * Character-scans the code so that: * - "..." and '...' string literals are protected from rewriting * - template-literal TEXT segments are protected, while the expressions * inside ${...} are recursively transformed (and the transformed result * is itself protected, so restored string literals inside it are not * re-rewritten by the outer pass) * - comments are copied through untouched * * It then rewrites top-level declarations (`let x = v` → `__state__.x ??= v`, * so attribute overrides already in state win over script defaults) unless * `rewriteDeclarations` is false (the event-handler funcDefs path, which has * no declarations to rewrite), and finally rewrites standalone references * via replaceVarWithStateAccess (object-shorthand aware). */ export declare function transformCodeToStateAccess(code: string, variables: string[], options?: { rewriteDeclarations?: boolean; }): string; /** * Replaces standalone references to `varName` with `__state__.varName`, * expanding object-literal shorthand and skipping destructuring declarations. */ export declare function replaceVarWithStateAccess(code: string, varName: string): string;