Repository navigation
Print more files with AST formatting, and keep Mata indentation, else if, and comments before a ; as written - #373
Merged
Merged
Conversation
…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 `;`.
Contributor
|
Warning Review limit reachedEnable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. Next included review available in 6 minutes. View limit details
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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":displayhas no options, so its comma is an argument (Stata showsa 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 newoptions_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 inoptions_tail.framef' {(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/.dohfiles 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 combineanddisplaywith%9.2f/_nnever 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 ifwas printed aselse {with theifinside 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// emoved after the;. The parser now marks trailing comments that come before a statement's;(before_terminator), and the printer keeps them there.sight parse --jsonchanges (noschema_versionbump, since fields were only added):options_tail([]when empty).twoway scatter y x, mcolor(red) || line y xhas||,line,y,x;regress y x,has[","].displaycomma is inargs:di "a", "b"has"a",and"b".content.textkeeps its first line's indentation.Not changed: AST formatting collapses the whitespace inside a multi-line literal macro value (
local a b // cthendholdsb din Stata, butb dafter 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 assumeddisplay a, opthas 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).