Skip to content

C#: Reintroduce separate externs, usings, attributeLists on CompilationUnit/NamespaceDeclaration#7152

Merged
knutwannheden merged 2 commits intomainfrom
jolly-condor
Mar 26, 2026
Merged

C#: Reintroduce separate externs, usings, attributeLists on CompilationUnit/NamespaceDeclaration#7152
knutwannheden merged 2 commits intomainfrom
jolly-condor

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

@knutwannheden knutwannheden commented Mar 26, 2026

Motivation

  • PR C#: Consolidate namespace and compilation unit model #6981 consolidated externs, usings, and attributeLists into a single members list on CompilationUnit and NamespaceDeclaration. However, the C# language specification requires a strict ordering of top-level elements (extern_alias* → using_directive* → global_attributes* → namespace_member*). Having separate fields makes it straightforward for recipes to add/remove elements without needing to find the correct insertion position within a flat members list.

Summary

  • Reintroduced externs (List<JRightPadded<ExternAlias>>), usings (List<JRightPadded<UsingDirective>>), and attributeLists (List<AttributeList>) as separate fields on CompilationUnit (both Java and C# sides)
  • Reintroduced externs and usings as separate fields on NamespaceDeclaration
  • Removed Statement interface from ExternAlias, UsingDirective, and AttributeList — they now live in dedicated typed lists and don't need to be statements
  • Removed unused Java-only AST types (Cs.DestructorDeclaration, Cs.NewClass, Cs.MethodDeclaration, Cs.ArrayType) that had no C# counterpart and were never created by the parser
  • Moved ExpressionStatement from J.cs to Cs.cs to match the Java-side Cs.ExpressionStatement type
  • File-scoped namespaces are now represented as NamespaceDeclaration with a Semicolon marker (instead of Package + flattened members)

Test plan

  • All 1728 C# xUnit tests pass
  • Java-side RPC integration tests pass (gw :rewrite-csharp:test)
  • Java compilation passes
  • Field order matches C# language spec: externs → usings → attributeLists → members
  • RPC send/receive order is consistent across all four endpoints (Java sender/receiver, C# sender/receiver)

…mpilationUnit and NamespaceDeclaration

The C# language specification requires a strict ordering of top-level
elements (extern aliases → using directives → global attributes →
member declarations). Modeling these as separate fields makes it
straightforward for recipes to add/remove elements without needing
to find the correct insertion position within a flat members list.

Also removes Statement interface from ExternAlias, UsingDirective,
and AttributeList since they now live in dedicated typed lists.

Removes unused Java-only AST types (DestructorDeclaration, NewClass,
MethodDeclaration, ArrayType) that had no C# counterpart.

Moves ExpressionStatement from J.cs to Cs.cs to match the Java-side
Cs.ExpressionStatement type.
The convention-based resolver already handles Cs$ConstrainedTypeParameter
→ OpenRewrite.CSharp.ConstrainedTypeParameter correctly.
@knutwannheden knutwannheden changed the title C#: Reintroduce separate externs, usings, attributeLists on CompilationUnit/NamespaceDeclaration C#: Reintroduce separate externs, usings, attributeLists on CompilationUnit/NamespaceDeclaration Mar 26, 2026
@knutwannheden knutwannheden merged commit c1e3dec into main Mar 26, 2026
1 check passed
@knutwannheden knutwannheden deleted the jolly-condor branch March 26, 2026 08:53
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant