You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(ts-morph-codegen): Implement interface processing order (#7)
This change introduces a new utility function `getInterfaceProcessingOrder` that
determines the order in which interfaces should be processed. This is necessary
to ensure that interfaces that depend on other interfaces are processed in the
correct order.
The changes include:
- Importing the `getInterfaceProcessingOrder` function from the
`@daxserver/validation-schema-codegen/utils/interface-processing-order` module.
- Updating the code that processes interfaces to use the new function to
determine the processing order.
- Refactoring the interface processing loop to use `forEach` instead of a `for`
loop.
These changes improve the reliability and maintainability of the code generation
process by ensuring that interfaces are processed in the correct order.
Copy file name to clipboardExpand all lines: ARCHITECTURE.md
+29-2Lines changed: 29 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,7 @@ The main logic for code generation resides in the <mcfile name="ts-morph-codegen
61
61
62
62
6.**Type Alias Processing**: The `TypeAliasParser` is instantiated and iterates through all `type alias` declarations in the input `sourceFile`. For each type alias, its underlying type node is converted into a TypeBox-compatible type representation, a TypeBox schema is generated, and a corresponding static type alias is added.
63
63
64
-
7.**Interface Processing**: The `InterfaceParser` is instantiated and iterates through all `interface` declarations in the input `sourceFile`. For each interface, its properties and methods are converted into TypeBox object schemaswith corresponding static type aliases.
64
+
7.**Interface Processing**: The `InterfaceParser` is instantiated and iterates through all `interface` declarations in the input `sourceFile`. Interfaces are processed in dependency order to handle inheritance properly. For each interface, its properties and methods are converted into TypeBox object schemas. Interfaces that extend other interfaces are generated using `Type.Intersect` to combine the base interface with additional properties, ensuring proper inheritance semantics.
65
65
66
66
8.**Function Declaration Processing**: The `FunctionDeclarationParser` is instantiated and iterates through all function declarations in the input `sourceFile`. For each function, its parameters, optional parameters, and return type are converted into TypeBox function schemas with corresponding static type aliases.
67
67
@@ -103,6 +103,32 @@ The import resolution process works in two phases:
103
103
104
104
This approach ensures that complex import scenarios work correctly and generated code compiles without dependency errors.
105
105
106
+
## Interface Inheritance Support
107
+
108
+
The codebase provides comprehensive support for TypeScript interface inheritance through a sophisticated dependency resolution and code generation system:
109
+
110
+
### Dependency-Ordered Processing
111
+
112
+
Interfaces are processed in dependency order using a topological sort algorithm implemented in <mcfilename="ts-morph-codegen.ts"path="src/ts-morph-codegen.ts"></mcfile>:
113
+
114
+
1.**Dependency Analysis**: The `getInterfaceProcessingOrder` function analyzes all interfaces to identify inheritance relationships
115
+
2.**Topological Sorting**: Interfaces are sorted to ensure base interfaces are processed before extended interfaces
116
+
3.**Circular Dependency Detection**: The algorithm detects and handles circular inheritance scenarios gracefully
117
+
118
+
### TypeBox.Composite Generation
119
+
120
+
Interface inheritance is implemented using TypeBox's `Type.Composite` functionality:
121
+
122
+
-**Base Interface Reference**: Extended interfaces reference their base interfaces by name as identifiers
123
+
-**Property Combination**: The `InterfaceTypeHandler` generates `Type.Composite([BaseInterface, Type.Object({...})])` for extended interfaces
124
+
-**Type Safety**: Generated code maintains full TypeScript type safety through proper static type aliases
125
+
126
+
### Implementation Details
127
+
128
+
-**Heritage Clause Processing**: The <mcfilename="interface-type-handler.ts"path="src/handlers/typebox/object/interface-type-handler.ts"></mcfile> processes `extends` clauses by extracting referenced type names
129
+
-**Identifier Generation**: Base interface references are converted to TypeScript identifiers rather than attempting recursive type resolution
130
+
-**Error Prevention**: The dependency ordering prevents "No handler found for type" errors that occur when extended interfaces are processed before their base interfaces
131
+
106
132
## Input Handling System
107
133
108
134
The <mcfilename="input-handler.ts"path="src/input-handler.ts"></mcfile> module provides flexible input processing capabilities for the code generation system. It supports multiple input methods and handles various edge cases related to file resolution and import validation.
@@ -191,7 +217,7 @@ This directory contains a collection of specialized handler modules, each respon
191
217
**Object-Like Type Handlers** (extend `ObjectLikeBaseHandler`):
192
218
193
219
- <mcfilename="object-type-handler.ts"path="src/handlers/typebox/object/object-type-handler.ts"></mcfile>: Handles TypeScript object types and type literals.
- <mcfilename="interface-type-handler.ts"path="src/handlers/typebox/object/interface-type-handler.ts"></mcfile>: Handles TypeScript interface declarations, including support for interface inheritance using `Type.Composite` to combine base interfaces with extended properties.
195
221
196
222
**Collection Type Handlers** (extend `CollectionBaseHandler`):
197
223
@@ -227,6 +253,7 @@ This directory contains a collection of parser classes, each extending the `Base
227
253
- <mcfilename="parse-imports.ts"path="src/parsers/parse-imports.ts"></mcfile>: Implements the `ImportParser` class, responsible for resolving and processing TypeScript import declarations.
228
254
- <mcfilename="parse-type-aliases.ts"path="src/parsers/parse-type-aliases.ts"></mcfile>: Implements the `TypeAliasParser` class, responsible for processing TypeScript `type alias` declarations.
229
255
- <mcfilename="parse-function-declarations.ts"path="src/parsers/parse-function-declarations.ts"></mcfile>: Implements the `FunctionDeclarationParser` class, responsible for processing TypeScript function declarations and converting them to TypeBox function schemas.
256
+
- <mcfilename="parse-interfaces.ts"path="src/parsers/parse-interfaces.ts"></mcfile>: Implements the `InterfaceParser` class, responsible for processing TypeScript interface declarations with support for inheritance through dependency ordering and `Type.Composite` generation.
0 commit comments