Skip to content

feat(sdk): variable usage scan + preview-values adapter seam#2048

Open
jrusso1020 wants to merge 1 commit into
07-07-feat_sdk_variable_declaration_edit_ops_declare_update_remove_from
07-07-feat_sdk_variable_usage_scan_preview-values_adapter_seam
Open

feat(sdk): variable usage scan + preview-values adapter seam#2048
jrusso1020 wants to merge 1 commit into
07-07-feat_sdk_variable_declaration_edit_ops_declare_update_remove_from
07-07-feat_sdk_variable_usage_scan_preview-values_adapter_seam

Conversation

@jrusso1020

@jrusso1020 jrusso1020 commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

What

Third PR of the template-variables stack: usage analysis + the preview-values adapter seam.

  • parsers: scanVariableUsage(scriptText) — browser-safe acorn scan for getVariables() reads (destructuring, member access, single-alias tracking). Anything opaque (computed keys, rest spreads, escaping values object, parse errors) flips scanIncomplete so consumers treat usedIds as a lower bound, never a fact
  • sdk: comp.getVariableUsage() cross-references the scan of every inline script against the declared schema → { usedIds, unusedDeclarations, undeclaredReads, scanIncomplete }
  • sdk: optional PreviewAdapter.setPreviewVariables(values | null) + comp.setPreviewVariables() delegation — the platform seam for "preview with these values" (ephemeral, never persisted)
  • Review hardening absorbed into this PR: direct window.__hfVariables reads now flip scanIncomplete (they're invisible to the scanner), var(--id) CSS consumers count as usage (a variable consumed only via CSS must not be badged unused), and scans are content-keyed-cached (same rationale as _gsapLabelCache)

Why

Compositions read variables via JS calls — there's no DOM attribute to scan — so script analysis is the only way to answer "which variables does this composition actually use". The honest-lower-bound design (scanIncomplete) is the load-bearing decision: a wrong "unused" badge invites users to delete variables their render depends on.

Test plan

  • Unit tests: variableUsage.test.ts (9 scanner cases incl. alias tracking, escape flagging, parse errors), session.variableusage.test.ts (cross-reference report, adapter delegation)
  • Full sdk + parsers suites green
  • Manual: usage badges and the scanIncomplete degradation verified in the live panel (see fix(studio): code-review and live-test fixes for the variables stack #2052)

🤖 Generated with Claude Code

Third PR of the template-variables Studio stack.

- parsers: scanVariableUsage(scriptText) — browser-safe acorn scan for
  getVariables() reads (destructuring, member access, single-alias
  tracking). Anything opaque (computed keys, rest spreads, escaping values
  object, parse errors) flips scanIncomplete so consumers treat usedIds as
  a lower bound rather than a fact. Compositions have no DOM binding for
  variable reads, so script scanning is the only usage signal.
- sdk: comp.getVariableUsage() cross-references the scan of every inline
  script against the declared schema → { usedIds, unusedDeclarations,
  undeclaredReads, scanIncomplete }.
- sdk: optional PreviewAdapter.setPreviewVariables(values | null) +
  comp.setPreviewVariables() delegation — the platform seam for
  "preview with these values" (ephemeral, never persisted; values must
  reach the runtime before composition scripts run). Studio's adapter
  implements it in a later PR; embedders can implement their own.
- fallow: targeted suppressions — complexity on the three AST classifiers
  (precedent: gsapParserAcorn) and code-duplication markers on two
  pre-existing mutate.ts clone groups the stack's line shifts re-attributed
  to this diff.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@jrusso1020 jrusso1020 force-pushed the 07-07-feat_sdk_variable_declaration_edit_ops_declare_update_remove_ branch from d3ab9db to ed9f222 Compare July 8, 2026 08:00
@jrusso1020 jrusso1020 force-pushed the 07-07-feat_sdk_variable_usage_scan_preview-values_adapter_seam branch from a2085a5 to 6acf033 Compare July 8, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant