-
Notifications
You must be signed in to change notification settings - Fork 1
CompilerEvolution
Transform the current TypeScriptEmitter from a syntax-based transpiler into a semantic-aware, extensible compiler capable of handling complex business logic and producing optimized, type-safe TypeScript/JavaScript.
To prevent "spaghetti code" and unsupported operations in the browser, the compiler will enforce strict boundaries, inspired by Next.js (Server/Client split) and Flutter (Constraints).
-
Server Components (
.csonly): Can useSystem.IO,DbContext, and any .NET library. Renders static HTML or initial state. -
Client Components (
StatefulComponent/StatelessComponent):-
Allowed: UI Logic, State Management,
System.Linq(via compilation), Basic Types (string,int,DateTime). -
Forbidden:
System.IO,System.Net.Http(direct), blocking.Wait(). -
Bridge: Data fetching MUST use
ServerActions(RPC style).
-
Allowed: UI Logic, State Management,
The compiler will reject code that breaks these rules before emitting JS:
- ❌
File.ReadAllText()in aStatefulComponent. - ❌ Direct SQL queries in
OnMount. - ✅ Calling a method annotated with
[ServerAction].
Goal: Replace hardcoded method translations with a data-driven registry. Implementation:
- Create
TypeMappingRegistryclass. - Support key-value mapping for types (
DateTime->Date) and methods (Console.WriteLine->console.log). - Replace
switchstatements inCSharpToJsConverterwith registry lookups.
Goal: Eliminate StringBuilder fragility and ensure syntactically correct output.
Implementation:
- Create
TypeScriptCodeBuilderclass. - Methods for
Class(),Method(),If(),Block(), avoiding manual indentation management.
Goal: Resolve types to handle overloads and extension methods correctly. Implementation:
- Update
ComponentCompilerto compile C# trees (RoslynCSharpCompilation). - Pass
SemanticModeltoCSharpToJsConverter. - Use symbol information to distinguish between
List.Addand otherAddmethods.
Goal: Translate C# LINQ to JS Array methods. Implementation:
- Map
.Where->.filter - Map
.Select->.map - Map
.First->.find
Goal: Robust Task handling.
Implementation:
- Ensure all
Taskreturning methods are markedasyncin JS. - Automatically await calls to these methods if they are awaited in C#.
The new sheet model went through eqc on day one and its vitest twin failed three ways — each a GENERAL fix, none a workaround:
-
Char arithmetic. A C#
charin+ - * / %promotes to int and computes on the code unit; the transpiled char is a 1-length string, so'A' + colconcatenated ("A01") andtext[i] - '0'went NaN. When the RESULT type is numeric, char operands now lower to code units (constant literals fold to the number; expressions readcharCodeAt(0)), and(char)numericlowers toString.fromCharCode— previously the cast silently vanished.char + stringstays concatenation: its result type is string, so the branch never sees it. -
Dictionary enumeration. A transpiled primitive-keyed
Dictionaryis a plain object — not iterable — soforeachover one (andnew List<KeyValuePair<,>>(dict)) emitted dead code. Both now lower through$eq.entries(obj, numericKeys): pairs that destructure as[key, value]AND answer.key/.value(both C# consumption shapes), with numeric keys restored as numbers (Object.entries strings them, and a stringified key turns the nextkey + 1into concatenation). Record/struct-keyed dictionaries keep theirvalueMaplowering — the conformance suite caught the wrap swallowing those, and only primitive keys qualify. -
Generic item annotations.
new List<KeyValuePair<int,float>>(…)annotated itsletwith the bare C# name (KeyValuePair[]), which names nothing in TS; generic items now leave the annotation to inference.