aft icon indicating copy to clipboard operation
aft copied to clipboard

ast_grep_replace: dryRun previews invalid rewrites as success; $$$ captures flatten multi-line matches

Open null-axiom opened this issue 1 month ago • 2 comments

AFT: 0.62.0, OpenCode 2.1.4, macOS.

Two ast_grep_replace problems:

  1. dryRun previews an invalid rewrite as success, while apply refuses it. Pattern foo($A, $B), rewrite bar($B, $A (unbalanced), lang: typescript, dryRun: true → "Would replace 2 match(es)" with a diff containing bar(2, 1;. Without dryRun an invalid rewrite fails with "rewritten code failed syntax validation; no files were written". The preview should run the same validation, so a preview never promises a change apply will reject.
  2. Variadic captures flatten multi-line matches. Source foo(\n 3,\n 4\n);, pattern foo($$$A), rewrite bar($$$A) → bar(3,\n4); (indentation lost; dryRun and apply agree). Expected the captured nodes to keep their original separators/indentation. Relatedly ast_grep_search displays the capture as $A: 1,,,2.

Working as expected: $$$ matching, ignoring matches in strings/comments, globs and negated globs, dryRun leaving files untouched, apply matching the preview for valid rewrites.

null-axiom avatar Oct 23 '26 21:10 null-axiom

Rechecked on 0.63.5. The dry-run part is fixed: an invalid rewrite is now refused in the preview, and search shows $A: 1, 2. The $$$ rewrite output is still flattened.

Pattern foo($$$ARGS), rewrite bar($$$ARGS), TypeScript, dryRun: true:

foo(
  3,
  4
);

previews as

bar(3,
4);

Nested in a function, the same call previews as bar(3,\n 4);, so the first argument moves up onto the call line and the original indentation of the remaining lines is lost. I expected the captured text to come back as it was: bar(\n 3,\n 4\n);.

The 0.63.4 notes say $$$ captures are preserved, but issue_827_828_repro_test.rs only covers the search display and the dry-run refusal, not the rewritten output.

null-axiom avatar Nov 07 '26 15:11 null-axiom

Confirmed on current main: invalid rewrites are refused in preview, and variadic search display is repaired, but multiline $$$ARGS replacements still flatten the original layout. The reported example previews and applies as bar(3,\n4);. Keeping this open for the rewrite-formatting portion.

aft-alfonso[bot] avatar Nov 11 '26 19:11 aft-alfonso[bot]