Skip to content

Print more files with AST formatting, and keep Mata indentation, else if, and comments before a ; as written - #373

Merged
jbearak merged 1 commit into
mainfrom
ast-printer-leftovers
Nov 5, 2026
Merged

jbearak merged 1 commit into
mainfrom
ast-printer-leftovers

Conversation

@jbearak

@jbearak jbearak commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Follow-up to #369. Since #369, AST formatting falls back to source-preserving formatting for a file when the parser can't hold some of its code. This PR makes AST mode print more of those files itself, and fixes three things AST mode didn't print as written. Stata behavior was checked on StataMP 18.

1. Code after a command's options. The option parser stopped at a token that can't start an option:

  • di "a", "b": display has no options, so its comma is an argument (Stata shows a b). It now stays in the command's arguments.
  • twoway scatter y x, mcolor(red) || lfit y x, lcolor(blue): everything from the || on now goes into a new options_tail, which AST formatting prints after the options. An option that touches the stray token goes with it (robust/cluster).
  • regress y x,: AST mode dropped the trailing comma without falling back. With no option before it, the comma itself is now kept in options_tail.
  • frame f' {(a frame named by a macro) madestata/vview.ado` fall back. Frame blocks now accept a macro as the name.

Among the 77 .do/.ado/.doh files in the repo (fixtures included), files that fall back went from 4 to 2, and the 2 left are deliberately invalid fixtures. In a sweep of 45 common command shapes, 4 fell back before and none do now. twoway (scatter y x) (line y x), graph combine and display with %9.2f/_n never fell back, and now have tests that they round-trip unchanged.

2. Mata and Python indentation. AST mode removed the first content line's indentation, because the parser trimmed the block's content. Python reads that indentation as code. Block content now keeps it.

3. else if was printed as else { with the if inside it. It now prints as written.

4. Comments before a ;. Under #delimit ;, local a b // c / d // e / ; changed again on a second AST pass, because // e moved after the ;. The parser now marks trailing comments that come before a statement's ; (before_terminator), and the printer keeps them there.

sight parse --json changes (no schema_version bump, since fields were only added):

  • Every command has options_tail ([] when empty). twoway scatter y x, mcolor(red) || line y x has ||, line, y, x; regress y x, has [","].
  • A display comma is in args: di "a", "b" has "a", and "b".
  • A Mata/Python block's content.text keeps its first line's indentation.

Not changed: AST formatting collapses the whitespace inside a multi-line literal macro value (local a b // c then d holds b d in Stata, but b d after AST formatting). Source-preserving formatting also shifts that whitespace when it re-indents the statement, so it's left for a separate change.

Proof: 22 of the new tests (tests/unit/option-tail.test.ts, tests/unit/ast-printer-as-written.test.ts) fail on main, and the formatter cases run in both modes with idempotence. I AST-formatted a do-file with every shape and ran it next to the original in StataMP 18: the output is the same except for the macro whitespace above. The four gates pass (9345 tests). Two property tests assumed display a, opt has options; they now use other commands.

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

Created with Claude Opus 5.5 in Claude Code; reviewed by gpt-6.1-sol (Codex).

…as, macro frame names, else if, block indentation, and comments before a ;

- display has no options, so its comma is an argument (Stata 18:
  `di "a", "b"` shows `a b`). Other commands keep the rest of the
  statement after a token no option can start in `options_tail`
  (`twoway scatter y x, mcolor(red) || line y x`, a bare trailing
  comma), and AST formatting prints it after the options. `sight parse
  --json` reports it as `options_tail`.
- A frame block can be named by a macro (`frame `f' {`).
- An embedded block keeps its first content line's indentation, so AST
  formatting prints Mata and Python content as written.
- `else if` is marked in the AST and printed as written.
- Under #delimit ;, trailing comments before a statement's `;` are
  marked, so AST formatting keeps them before it and is idempotent for
  `local a b // c` then `d // e` then `;`.
@coderabbitai

coderabbitai Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

Next included review available in 6 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available. Your 50 included PR review attempts over the past 7 days set your current allowance at 4 reviews per hour.

Learn how review limits work.

Review configuration:

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: 10f58615-1423-4b3c-ae54-206b923e7da7
Commits

Reviewing files that changed from the base of the PR and between 4cba365 and f11ef57.

Files selected for processing: 11

  • docs/cli.md
  • src/cli/parse-json.ts
  • src/parser/index.ts
  • src/pretty-printer/index.ts
  • src/types/index.ts
  • tests/fixtures/cli-json/expected/parse.json
  • tests/property/quoted-path-parsing.prop.test.ts
  • tests/property/varlist-parsing-consolidation.prop.test.ts
  • tests/unit/ast-format-unprintable-code.test.ts
  • tests/unit/ast-printer-as-written.test.ts
  • tests/unit/option-tail.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@jbearak
jbearak merged commit 721cf97 into main Nov 5, 2026
16 checks passed
@jbearak
jbearak deleted the ast-printer-leftovers branch November 5, 2026 03:54
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