rewrite-kotlin: extend recipe DSL rewrite { } to { } arity from 2 to 12#7890
Merged
Conversation
…o 12
Adds `rewrite` overloads + `RewriteAdviceN` carriers for arities 3 through 12, and registers the corresponding `RewriteAdviceN.to` FQNs with the K2 IR generation pass.
## Why
The DSL's two-parameter ceiling forced agents (and humans) generating recipes for common Java patterns to either skip higher-arity cases or split them into separate templates per arity. A single `Arrays.asList(a, b, c)` -> `List.of(a, b, c)` pattern couldn't be expressed — `rewrite { a: Any, b: Any, c: Any -> java.util.Arrays.asList(a, b, c) }` fails to resolve because no 3-param overload exists. K2 then reports "none of the following candidates is applicable" and the whole `recipe(...) { edit { ... } }` block fails to compile, taking the valid 1- and 2-arg rewrites in the same edit block down with it.
The agent-tools-bench copilot sweeps exercising the Kotlin DSL produce templates with arities up to 6 routinely; arities 7-12 are uncommon but cheap to add now while the change is fresh, and head off the same failure for `String.format(...)`-shape patterns where format-arg count drives the rewrite arity directly.
## What changed
- `RecipeDsl.kt`: nine new `rewrite(...)` overloads (single + vararg) for arities 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, plus matching `RewriteAdviceN<P1..Pn, R>` carriers with `infix fun to(after: (P1..Pn) -> R2)`.
- `RecipeIrGenerationExtension.kt`: `REWRITE_ADVICE_TO_FQNS` switches from a hand-enumerated set to `(0..12).map { "org.openrewrite.RewriteAdvice$it.to" }.toSet()`.
The IR-walking code in `validateBeforeLambda` and the `to`-call dispatch in `processRecipeBuilder` already read parameter counts off `fn.valueParameters` generically, so no further pass changes are needed — the only arity-bound spot was the FQN allow-list.
## Test plan
- [x] `:rewrite-kotlin:compileKotlin` (no errors against the new arities)
- [x] `:rewrite-kotlin:test --tests "*RecipeDsl*"` passes
- [ ] End-to-end: compile an arity-3 recipe in a downstream consumer (moderne-cli's pattern_replace tool) and confirm it produces changes against a target codebase
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.
Summary
Adds
rewriteoverloads +RewriteAdviceNcarriers for arities 3 through 12, and registers the correspondingRewriteAdviceN.toFQNs with the K2 IR generation pass.Why
The DSL's two-parameter ceiling forced anyone generating Kotlin recipes — including LLM agents that synthesize templates from natural-language prompts — to either skip higher-arity patterns or split them into separate templates per arity. A single
Arrays.asList(a, b, c)→List.of(a, b, c)pattern couldn't be expressed, because:rewrite { a: Any, b: Any, c: Any -> java.util.Arrays.asList(a, b, c) } to { a, b, c -> java.util.List.of(a, b, c) }fails to resolve — no 3-param
rewriteoverload exists. K2 reportsnone of the following candidates is applicableand the wholerecipe(...) { edit { ... } }block fails to compile, taking the valid 1- and 2-arg rewrites in the same edit block down with it.The agent-tools-bench copilot sweeps that exercise the Kotlin DSL produce templates with arities up to 6 routinely; arities 7–12 are uncommon but cheap to add while the change is fresh, and head off the same failure for
String.format(...)-shape patterns where the format-arg count drives the rewrite arity.What changed
RecipeDsl.kt— 9 newrewrite(...)overload pairs (single + vararg) for arities 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, plus matchingRewriteAdviceN<P1..Pn, R>carriers withinfix fun to(after: (P1..Pn) -> R2). Follows the samepluginRequired()stub pattern as the existing arities 0/1/2.RecipeIrGenerationExtension.kt—REWRITE_ADVICE_TO_FQNSswitches from a hand-enumerated set of three constants to(0..12).map { "org.openrewrite.RewriteAdvice$it.to" }.toSet().The IR-walking code in
validateBeforeLambdaand theto-call dispatch inprocessRecipeBuilderalready read parameter counts offfn.valueParametersgenerically — the only arity-bound location in the plugin was the FQN allow-list.Test plan
:rewrite-kotlin:compileKotlin— clean against the new arities:rewrite-kotlin:test --tests "*RecipeDsl*"— existing surface tests pass