dart/web/a0d3ad17d7a1d4ac127a707e3074af6c24e73598 js_interop_gen: many fixes moving towards supporting large, complex D.TS files (#549)
feat(js_interop_gen): support large, complex TS declaration files with robust merging and type safety
Introduce an architectural overhaul to resolve various type-safety, namespace-flattening, interface-merging, and runtime-casting bugs, preparing the generator to reliably translate complex real-world TypeScript declaration files into modern Dart JS interop bindings.
### 🏗️ AST Architecture & Safety Refactoring
- **Generic AST Nodes**: Refactored `Node` in `base.dart` to be generic (`Node<T>`), enforcing strict compile-time safety for type emission returns.
- **Modular Decomposition**: Decoupled the monolithic `transformer.dart` by extracting core operations into three specialized, highly-maintainable components:
- `NamespaceFlattener` to handle target namespace flattening and scoping.
- `DependencyWalker` to recursively walk references and construct complete trees.
- `TypeResolver` (moved to `type_resolver.dart`) to map TS types to Dart interop types.
- **State Cleanup**: Replaced global mutable options state (`GlobalOptions`) with explicit options propagation (`DeclarationOptions` and `TypeOptions`) through emitter pathways.
### 👥 Namespace Flattening & Generic Interface Merging
- **Generic ID Stability**: Shifted `GenericType.id` uniqueness logic from utilizing the namer-suffixed `parent.id` to the stable `parent.qualifiedName` (or `.name`). This ensures stable, matching type parameters when deduplicating properties across multiple merged declarations of a generic interface.
- **Declaration Identity Caching**: Introduced a unique mapping cache (`declarationToEmittedName` in `ReferredType`) mapping every parsed TS `Declaration` to its final unique name allocated by the namer, resolving import-path naming conflicts and target resolution drift in flattened namespaces.
### 🔢 Literal Unions & Wide Casting
- **Literal Cast Conversions**: Fixed invalid runtime casts on union getters of literal types (such as `true`, string literals, and numbers) by correctly resolving their JS representation equivalents and building safe `.toDart`, `.toDartInt`, and `.toDartDouble` cast pathways.
- **Mixed Union Widening**: Corrected mixed union LCA (Lowest Common Ancestor) resolution to correctly widen to `JSAny` using a thorough topological sorting pass.
- **Union Casting Safety**: Addressed unchecked type safety warnings on nullable `JSAny` and `void` references by integrating safe null check guards inside union getters.
### 🛡️ Generics & Runtime Guardrails
- **Generics Padding**: Integrated automatic padding on generic type references to correctly align with target parameter bounds.
- **Type Mapping Alignment**: Unified the mapping of `void` generic type parameters to `JSAny` to satisfy `JSAny?` bounds.
- **Recursion & Crash Protection**:
- Implemented an infinite recursion detection check to protect the generator against stack overflows on mutually recursive types.
- Replaced unsafe `.reduce` blocks on declarations with a robust, empty-safe `.expand().toList()` flattener preventing runtime `StateError`s.
- Statically type-promoted `TSSymbol` in `getTypeFromSymbol` using early null validation checks.
100 files changed