openrewrite / rewrite-templating

Automated templating using code snippets.
Apache License 2.0
16 stars 7 forks source link

Verify equality of template signatures #108

Open Bananeweizen opened 2 months ago

Bananeweizen commented 2 months ago

Make sure that all templates belonging to a recipe have the same number, order and type for all the method arguments. This avoids accidental copy/paste errors which otherwise can lead to compiling recipe code, but wrong results at runtime.

Checklist

Stephan202 commented 2 months ago

Heads up: this may quite significantly reduce the number of Error Prone Support Refaster rules for which a corresponding OpenRewrite recipe is generated.

Stock Refaster requires that @AfterTemplate method parameter names are also present for each @BeforeTemplate method, but not vice versa, and does not require that the types match. We rely on latter to keep rules compact, while the former is valid in case a rule drops an unused subexpression.

Bananeweizen commented 2 months ago

Then the only thing remaining to raise an error for would be after.args.size() > before.args.size(). However, that would have found just 1 out of the 3 or 4 copy/paste errors I made in recent weeks that all motivated me to have the strict check. :/

Stephan202 commented 2 months ago

Yeah, that's painful :(. I recognize that it's soo easy to make copy-paste errors when creating a bunch of similar Refaster rules.

This motivated us to write a test framework (where each rule has before- and after-example code), but as-is said framework isn't maximally user friendly, requiring changes in three files (example: PicnicSupermarket/error-prone-support#1290). At some point I looked into creating a "DSL" that would have example code be "nested" inside the relevant Refaster rule. Something to return to, one of these days :)

knutwannheden commented 2 months ago

It seems that we can at least also verify that the parameters of the after templates correspond to a parameter in each before template, where we perform the check by name.