Repository navigation
feat(cli): add taskless ast <file> to show a file's named-node AST - #683
Conversation
|
@claude /review |
|
Claude finished @theCodeDrift's task in 3m 26s —— View job Review mode: incremental — read 0 prior review thread(s) before reviewing.No prior reviews, review threads, or top-level comments exist on this PR beyond the trigger comment itself, so this is the first review — everything below is a fresh assessment of the full diff.
SummaryThis is a well-scoped, carefully documented command. The approach (driving Two [New] findings below, both narrow edge cases rather than blockers — posted as inline comments:
Everything else checked out:
|
Both findings handled in 11ae471: non-UTF-8 input is now refused with — AI Coding Agent |
Rule authors had no way to see a language's node kinds short of locating the bundled ast-grep binary by hand. `ast` dumps the named-node tree with the pinned ast-grep, resolving the language the way `check` does (asked of the binary), with --lang, --range and --json. create-sg-rule and the sg-regex-needs-kind message now point at it instead of --debug-query. Closes #673
A lenient utf8 read replaced invalid bytes with U+FFFD, so the dump showed a tree of text the file does not hold. Decode strictly and fail with INVALID_INPUT instead. Also pin that the language probe resolves files ignore rules would skip in a walk, since ast-grep never filters a path it is given.
…nd issue #682 moved sg-regex-needs-kind into the ast-grep rule schema, so the validateRequirements loop that appended the `taskless ast <file>` pointer is gone. Apply the pointer in validateSchema's formatter instead, to both the layer error and its attributed violation, and point the recipe's regex/kind section at `taskless ast` rather than --debug-query=ast.
11ae471 to
73158e2
Compare
|
Rebased onto main after #682 and #691 landed. #682 moved
— AI Coding Agent |
Rebasing onto main brought in text from #682, #683, #686 and #691 that pushed three outputs past 8 KiB: - create-sg-rule outline: the `taskless ast` guidance moves to an on-demand Reading kinds section, step 4 points at it, and step 5 drops its quirk enumeration (the Quirks table lists them). - create-sg-rule pattern-quirks: the relational entries (follows/precedes, has/inside, not) move to a Relational quirks section; validate-rule names it beside the other two. - route outline: #686's note on recipes that limit questions moves to an on-demand Limits on asking section, with a one-line pointer in Preconditions.
Rebasing onto main brought in text from #682, #683, #686 and #691 that pushed three outputs past 8 KiB: - create-sg-rule outline: the `taskless ast` guidance moves to an on-demand Reading kinds section, step 4 points at it, and step 5 drops its quirk enumeration (the Quirks table lists them). - create-sg-rule pattern-quirks: the relational entries (follows/precedes, has/inside, not) move to a Relational quirks section; validate-rule names it beside the other two. - route outline: #686's note on recipes that limit questions moves to an on-demand Limits on asking section, with a one-line pointer in Preconditions.
Rule authors had no way to see a language's node kinds.
create-sg-rulesaid to read them off--debug-query=astwithout saying how to run ast-grep, so agents went hunting for the binary (~/.npm/_npx,/usr/bin/sg, which is shadow-utils on Linux) or pasted whole files as patterns.What changes
taskless ast <file>prints the file's named-node AST, parsed by the bundled ast-grep. Because it shows named nodes only, every kind it prints is onekind:accepts. Each node has its field name andline:columnrange (1-based lines, 0-based byte columns), and leaves show their source text.scan --inspect entityagainst an empty temp config), so it gets the same resolutioncheckdoes, with no second extension table. A straysgconfig.ymlwithlanguageGlobsis ignored, sincecheckdoes not use it either.--lang <Language>overrides the language (any spellinglanguage:accepts).--range <line>[:<line>]keeps only the nodes on those lines plus the nodes that enclose them.--jsongives the tree as data, validated against a zod schema.INVALID_INPUT(missing, empty, unrecognized or oversized file, bad flag),ENGINE_UNAVAILABLE,SCAN_FAILED.create-sg-rule(topic v15) tells authors to read everykind:offastand no longer mentions--debug-query. Thesg-regex-needs-kindmessage fromvalidatenow namesast.cli-astcapability and one addedcli-agent-authoringrequirement, archived in this PR (single-PR delivery).How it works, and the limit
ast-grep has no command that dumps a file's tree. The only one is
run --debug-query=ast, which dumps the pattern, so the command passes the file contents as the pattern. Measured at 0.51.1, the result is exact:$FOOstill dumps asvariable_name, so pattern preprocessing does not affect it.The parser for that text format is pinned by a test against the binary for all 29 languages.
The cost: the file goes through argv, so a file past the platform's argument limit (128 KiB per argument on Linux) is refused with a clear
INVALID_INPUT. The issue's optional--patternfilter is not included;--rangealready solves the problem of long dumps.Tests
test/ast-command.test.ts: parser, range filter and language-probe units; contract tests against the bundled binary (every language dumps, every language's extension resolves,sgconfig.ymlignored, PHP$FOO, ERROR counting); built-CLI tests for text and JSON output,--range,--lang, a YAML file that starts with-, and each refusal. Full suite: 3218 passed, 11 skipped.pnpm typecheckandpnpm lintare clean.Closes #673