Skip to content

Lift C# member aliases out of their type and nest JS classes as statics [patch] - #227

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/179-member-alias-nested-class
Oct 14, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/179-member-alias-nested-class

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #179

What was wrong

A UsingAlias or nested ClassDeclaration inside a class came out where neither target accepts it:

  • C#: GenerateUsingAlias wrote using Id = int; inside the type body. The build failed with CS1513, CS1529 and CS1022, even though the method's own remark said member aliases would be "said rather than written".
  • JavaScript: the class body got const Id = int; and a bare class Inner { … }. node --check rejects both with SyntaxError: Unexpected identifier.

Change

  • C#: a type's member aliases, including those of types nested in it, are now written in the enclosing namespace or file, ahead of every other member there. That is the only place C# accepts a using alias, so names like Id still resolve inside the type. The scope's own aliases move up as well, so an alias declared after a type no longer fails with CS1529. A type generated on its own (no enclosing scope) writes its aliases just before itself. The remarks are updated to say this.
    namespace Shapes
    {
        using Id = int;
        using Count = long;
    
        public class Holder
        {
            public Id id;
        }
        ...
  • JavaScript: a nested class becomes static Inner = class Inner { … }; and a member alias becomes static Id = int;. Nested enums already used this shape. The class heading and member loop are split out of GenerateClassDeclaration so the nested form can reuse them.

Tests

  • New TypeMemberDeclarationTests (5 tests):
    • C# alias placement for a standalone type and for a namespace.
    • A dotnet build of a namespace containing a class-member alias, a nested class with its own alias, and a namespace alias declared after a type.
    • JS static-member shape.
    • node --check of a class holding an alias and a class holding a nested class.
  • With the generator changes stashed, all 5 fail. The C# build shows the issue's CS1513, CS1529 and CS1022 errors. With the changes, all 5 pass.
  • Full Coder.Test suite: 1269 passed, 0 failed. dotnet format --verify-no-changes on the touched files reports only the CHARSET/IMPORTS findings that main already has.

Known limit

Hoisting a member alias to the enclosing scope means two types in the same scope can't declare same-named aliases to different types; that would now be a duplicate alias. C# has no member-scoped alias, so the alternative is writing the aliased type in place of the name at every use. I didn't do that here because no reported case needs it.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NW9fJSB9VKsuPv7M2UmznC


Generated by Claude Code

…cs [patch]

A class member that is a UsingAlias or a nested ClassDeclaration came out
where neither target accepts it. CSharpGenerator.GenerateUsingAlias wrote
`using Id = int;` inside the type body (CS1513, CS1529, CS1022), despite
its own remark. JavaScriptGenerator wrote `const Id = int;` and a bare
`class Inner { }` into the class body, both syntax errors there.

C#: a type's aliases, and those of the types nested in it, are now
written in the namespace or file that encloses it, ahead of everything
else in that scope, which is the only place C# takes a using alias. The
scope's own aliases move there too, so one declared after a type no
longer fails with CS1529. A type generated on its own writes its aliases
in front of itself.

JavaScript: a nested class becomes `static Inner = class Inner { ... };`
and a member alias `static Id = int;`, the shape a nested enum already
takes.

TypeMemberDeclarationTests compiles the C# with dotnet build and parses
the JavaScript with node --check.

Fixes #179

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NW9fJSB9VKsuPv7M2UmznC
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit eba14c5 into main Oct 14, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/179-member-alias-nested-class branch October 14, 2026 06:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A type alias or nested class inside a class produces C# and JavaScript that fails to parse

2 participants