Skip to content

Read a command's macro writes from one module, so definitions and loop poisoning can't drift - #387

Merged
jbearak merged 1 commit into
mainfrom
refactor/command-macro-writes
Nov 5, 2026
Merged

jbearak merged 1 commit into
mainfrom
refactor/command-macro-writes

Conversation

@jbearak

@jbearak jbearak commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

Every recent "stop reporting X as undefined after " fix had to edit the analyzer twice. One if-chain registered the macros a command writes (tempvar, marksample, args, unab, gettoken, file read, local()/global() options, c_local). A second chain, documented as "mirroring" the first, told the loop expander which names to poison. The two chains had already drifted on which names count.

command_macro_writes (new, in src/analyzer/command-macro-writes.ts) now answers both: it returns the literal writes, the globals macro drop clears, and whether a target name is known only at run time. Adding a command is now one entry in one place, and its tests call that function directly instead of going through the diagnostics pipeline. syntax_locals does the same job for syntax. Completion had kept its own copy of that logic, which offered fweight for a weight argument; Stata defines weight and exp.

Behaviour changes:

  • A user program's local() option now matches only in lowercase. In StataMP 18, myprog, Cache(r) fails with "option cache() required" and defines nothing.
  • tempvar, unab and args names now must be valid identifiers. gettoken and file read already had this check, as did loop poisoning.

Not changed: Stata also accepts an abbreviated user-program option (c(r) for Cache(string)). Sight didn't recognise abbreviations before this PR and still doesn't.

Proof: real-Stata probe on StataMP 18 for the option-case rule; new tests/unit/command-macro-writes.test.ts; typecheck, lint and bun test ./tests pass. The one exception is a pre-existing severity-settings property flake (_b from the generator), which gets its own fix.

Reviewed with sol-loop: 1 round with gpt-6.1-sol. Sol verdict: approved

Created with Claude Opus 5.5 in T3 Code.

Summary by CodeRabbit

  • New Features
    • Improved recognition of local and global macros created or cleared by commands and program calls, including macros declared through syntax options.
    • Program argument completion now accounts for named anything and weight arguments.
  • Bug Fixes
    • Syntax option matching now respects capitalization, preventing differently capitalized options from registering unintended macros.

…p poisoning can't drift

The analyzer kept two parallel lists of the macros a command writes
(tempvar, marksample, args, unab, gettoken, file read, local()/global()
options, c_local): one registered definitions, the other poisoned loop
folds and "mirrored" the first. command_macro_writes now answers both.
syntax_locals does the same for `syntax`, which completion had copied
and drifted from (it offered `fweight` instead of `weight` and `exp`).

A user program's local() option now matches exact-case: in StataMP 18,
`myprog, Cache(r)` fails with "option cache() required" and defines
nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds shared helpers to detect command-created macro writes and syntax-declared locals. The analyzer uses them to register macros and track loop redefinitions. Completion derives program argument names from the same syntax-local model. Tests cover detection rules, local-name mappings, and option-name matching.

Changes: Command Macro Write Analysis

Layer / File(s) Summary
Detect command writes and syntax locals
src/analyzer/command-macro-writes.ts
New helpers identify positional and option-based writes, called-program c_local writes, global drops, and locals declared by syntax signatures.
Register writes and redefinitions
src/analyzer/index.ts
The analyzer uses the shared helpers to register macro writes and pass detected writes, dropped globals, and unknown-target status to loop expansion.
Use syntax locals in completion and tests
src/providers/completion.ts, tests/property/program-arguments.test.ts, tests/unit/analyzer.test.ts, tests/unit/command-macro-writes.test.ts
Completion derives program argument names from syntax locals. Tests cover name mappings, command write detection, and case-sensitive option matching.

Estimated code review effort: 4 (Complex) | ~40 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant SemanticAnalyzer
  participant command_macro_writes
  participant register_macro_write
  participant LoopExpansion
  SemanticAnalyzer->>command_macro_writes: Analyze command and program context
  command_macro_writes-->>SemanticAnalyzer: Return writes, dropped globals, and unknown target
  SemanticAnalyzer->>register_macro_write: Register each detected write
  SemanticAnalyzer->>command_macro_writes: Analyze command during loop expansion
  command_macro_writes-->>LoopExpansion: Supply writes, dropped globals, and unknown target
Loading

Possibly related PRs

  • jbearak/sight#383: Both PRs cover marksample name detection and analyzer macro registration.
  • jbearak/sight#381: PR #387 uses the c_local argument-position interface added in PR #381.
  • jbearak/sight#382: Both PRs use shared name-word handling for command macro writes, including c_local and positional-name commands.
Merge Risk: Minimal · up to a2edf

This change consolidates how command-created macros are detected and reuses the same syntax-local model in completion. The checked old and new paths behave the same for gettoken and macro drop, so no concrete merge-blocking risk remains. The user-program option case change is intentional and covered by tests.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly describes the main change: centralizing command macro-write detection in one module to keep definition tracking and loop-poisoning behavior consistent.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 14 functions across 6 files.

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Team
  • Run ID: c9779d75-d039-4fd1-b35e-f1e1979dffed
Commits

Reviewing files that changed from the base of the PR and between 77cd973 and a2edf21.

Files selected for processing: 6

  • src/analyzer/command-macro-writes.ts
  • src/analyzer/index.ts
  • src/providers/completion.ts
  • tests/property/program-arguments.test.ts
  • tests/unit/analyzer.test.ts
  • tests/unit/command-macro-writes.test.ts

Included review availability: This review used your included allowance. 1 included review remains after this review. Your included PR review attempts over the past 7 days set your current allowance at 2 reviews per hour.

@jbearak
jbearak merged commit df4bf7b into main Nov 5, 2026
16 checks passed
@jbearak
jbearak deleted the refactor/command-macro-writes branch November 5, 2026 20:51
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