Skip to content

C#: Replace RewriteRule with CSharpTemplate.Rewrite()#7134

Merged
knutwannheden merged 1 commit intomainfrom
csharp-template-rewrite
Mar 25, 2026
Merged

C#: Replace RewriteRule with CSharpTemplate.Rewrite()#7134
knutwannheden merged 1 commit intomainfrom
csharp-template-rewrite

Conversation

@knutwannheden
Copy link
Contributor

Motivation

The IRewriteRule / RewriteRule API was an intermediate abstraction between raw Match/Apply and a full visitor. After simplifying it down to just TryOn() and ToVisitor(), the remaining indirection doesn't carry its weight — ToVisitor() can be a static factory on CSharpTemplate directly, and TryOn() is just pat.Match() + tmpl.Apply() which is already clean enough in C#.

Examples

// Simple pattern → template (was: RewriteRule.Rewrite(pat, tmpl).ToVisitor())
return CSharpTemplate.Rewrite(
    CSharpPattern.Expression($"Console.Write({expr})"),
    CSharpTemplate.Expression($"Console.WriteLine({expr})"));

// Multiple patterns, shared template
return CSharpTemplate.Rewrite(
    [
        CSharpPattern.Expression($"Console.Write({expr})"),
        CSharpPattern.Expression($"Console.Error.Write({expr})"),
    ],
    CSharpTemplate.Expression($"Console.WriteLine({expr})"));

// Multiple (pattern, template) pairs — first match wins
return CSharpTemplate.Rewrite(
    (CSharpPattern.Expression($"{x} == null"), CSharpTemplate.Expression($"{x} is null")),
    (CSharpPattern.Expression($"{x} != null"), CSharpTemplate.Expression($"{x} is not null")));

Summary

  • Remove IRewriteRule interface, RewriteRule static class, and all internal implementation classes (RewriteRuleImpl, RewriteRuleVisitor)
  • Add CSharpTemplate.Rewrite() with three overloads: single pair, multiple patterns with shared template, and multiple (pattern, template) pairs
  • Move CreateBlockFlattener<P>() and BlockFlattener<P> to CSharpTemplate
  • Update all test recipes to use the new API or raw Match/Apply

Test plan

  • All 18 RewriteRuleTests pass with the new API
  • AttributeRenameViaRewriteRule in TemplateApplyTests passes
  • Full template test suite (235 tests) passes
  • No remaining references to IRewriteRule, RewriteRule, TryOn, or ToVisitor

Consolidate the declarative pattern→template rewriting API into
CSharpTemplate as static Rewrite() factory methods that directly
return a CSharpVisitor<ExecutionContext>.
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Mar 25, 2026
@knutwannheden knutwannheden changed the title C#: Replace IRewriteRule/RewriteRule with CSharpTemplate.Rewrite() C#: Replace RewriteRule with CSharpTemplate.Rewrite() Mar 25, 2026
@knutwannheden knutwannheden merged commit a056143 into main Mar 25, 2026
1 check passed
@knutwannheden knutwannheden deleted the csharp-template-rewrite branch March 25, 2026 08:08
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant