Skip to content

Keep syntax statements when AST formatting instead of deleting them - #354

Merged
jbearak merged 3 commits into
mainfrom
ast-printer-keep-syntax
Nov 5, 2026
Merged

jbearak merged 3 commits into
mainfrom
ast-printer-keep-syntax

Conversation

@jbearak

@jbearak jbearak commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

The AST formatter (formatting.mode = "ast") deleted every syntax statement. program p / syntax varlist / end became program p / blank line / end, and formatting again removed the blank line. The program lost its argument parsing. The printer had no case for the syntax node, so it printed nothing. The source-preserving mode was unaffected.

The parser now keeps the spec text after syntax on the node (/// lines joined, as it already does for macro values and prefix arguments). The printer prints it with its prefixes (capture noisily syntax ..., version 14: syntax ...) and comments. Like commands, a ///-continued syntax comes out on one line in AST mode.

A related parser bug: in syntax varlist /* c */ [if], the parser stopped the spec at the comment and read [if] as an if statement, which the AST formatter printed as if ] { }. The comment now reads as whitespace, as #349 did for other statements, so the signature includes [if].

With prefixes, the same applies: #351 read a comment between prefixes as whitespace except before syntax, because a syntax node didn't keep comments. It does now, so capture /* c */ noisily syntax varlist stays one statement instead of splitting into capture and noisily syntax varlist.

args, version, and program drop/list/dir already printed correctly. #delimit was fixed in #350.

Property generators can emit syntax as a command name again (#352 had excluded it to stop the flake).

Proof: reproduced the deletion on main; the new tests (both formatter modes, each formatted twice, including capture, quietly, and capture noisily before syntax) fail on main and pass here; with syntax weighted into the command-name generator, formatting-preservation [ast] and parser-roundtrip fail on main and pass here; the four gates pass, apart from two help-coverage tests that need a local Stata install this machine lacks.

Reviewed with sol-loop: 3 rounds with gpt-6.1-sol. Sol verdict: approved

Created with Claude Opus 5.5 in Claude Code (T3 Code); reviewed by GPT-6.1 Sol in Codex.

Summary by CodeRabbit

  • New Features
    • Syntax statements are now preserved when formatting, including their specifications, comments, and continued lines.
    • User-defined commands can now use syntax as a command name.
  • Bug Fixes
    • Syntax statements preceded by command prefixes are parsed together with those prefixes.
    • Comments within syntax statements are retained, and block comments between statement components no longer disrupt parsing.

@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 parser now retains syntax specification text and comments between syntax tokens. The pretty-printer prints syntax statements using the retained text. Tests cover parsing, formatting modes, and generated command names.

Changes: Syntax Statement Support

Layer / File(s) Summary
Retain syntax specifications and comments
src/types/index.ts, src/parser/index.ts, tests/unit/formatter-syntax-statement.test.ts, tests/unit/parser-statement-structure.test.ts
SyntaxNode adds optional arguments and interiorTrivia. The parser stores reconstructed specification text and collects comments between syntax tokens. Parser tests cover comments between arguments and at the statement end, plus prefixed syntax statements.
Print retained syntax text
src/pretty-printer/index.ts, tests/unit/formatter-syntax-statement.test.ts, tests/property/generators/primitives.ts, tests/property/generator-validity.test.ts
The pretty-printer prints syntax prefixes, arguments, and interior comments. Formatter tests cover modes, formatting cases, and idempotence. Property generator checks no longer exclude syntax as a command name.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant SourceText
  participant Parser
  participant SyntaxNode
  participant PrettyPrinter
  participant FormattedText
  SourceText->>Parser: syntax statement tokens
  Parser->>SyntaxNode: stores arguments and interiorTrivia
  SyntaxNode->>PrettyPrinter: provides syntax fields
  PrettyPrinter->>FormattedText: prints prefix, syntax, arguments, and comments
Loading
Merge Risk: Minimal · up to 81e54

Syntax statements are retained, though AST formatting still drops continuation-comment text. No material new merge risk was identified.

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 and concisely describes the main change: preserving syntax statements during AST formatting instead of removing them.
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 1 functions across 7 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: 35a264b0-8fb0-49a8-95c2-5b7a551b1cf4
Commits

Reviewing files that changed from the base of the PR and between ef1d149 and 81e54d2.

Files selected for processing: 3

  • src/parser/index.ts
  • tests/unit/formatter-syntax-statement.test.ts
  • tests/unit/parser-statement-structure.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 8 reviews per hour.

@jbearak
jbearak force-pushed the ast-printer-keep-syntax branch 2 times, most recently from bc9ec70 to febe968 Compare November 5, 2026 00:28
The AST printer had no case for the syntax node, so it printed nothing
and deleted the statement. The parser now keeps the spec text on the
node (continuations joined), and the printer prints it with its
prefixes and comments.

A block comment inside the spec now reads as whitespace, as in other
statements, instead of ending it: `syntax varlist /* c */ [if]` had
parsed `[if]` as an if statement.

Property generators can emit syntax as a command name again.
A syntax node now keeps interior comments, so a block comment between
prefixes before syntax reads as whitespace, as before a plain command:
`capture /* c */ noisily syntax varlist` was split into `capture` and
`noisily syntax varlist`. Test prefixed syntax in both formatter modes.
@jbearak
jbearak force-pushed the ast-printer-keep-syntax branch from febe968 to 81e54d2 Compare November 5, 2026 00:35
@jbearak
jbearak merged commit 3146b7a into main Nov 5, 2026
16 checks passed
@jbearak
jbearak deleted the ast-printer-keep-syntax branch November 5, 2026 00:43
jbearak added a commit that referenced this pull request Nov 5, 2026
…en /// between prefixes

#354 keeps interior comments on syntax nodes, so the prefix chain no
longer needs to stop at a comment before a prefixed syntax. A block
comment followed by /// between prefixes (quietly /* c */ ///, then
: gen x = 1) already reads as whitespace here; test it.
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