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(typebox): add support for template literal types (#4)
The changes in this commit add support for template literal types in the TypeBox library. The new tests cover various scenarios, including:
- One string literal
- Multiple string literals
- Concatenation with a literal at the start
- Concatenation with a literal at the end
- Concatenation with a numeric type
- Concatenation before and after a string type
These changes ensure that the TypeBox library can accurately represent and handle template literal types, which are a powerful feature in TypeScript.
Copy file name to clipboardExpand all lines: ARCHITECTURE.md
+15-21Lines changed: 15 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -163,15 +163,11 @@ const result = await generateCode({
163
163
164
164
## Utility Functions and Modules
165
165
166
-
-**<mcfilename="typebox-call.ts"path="src/utils/typebox-call.ts"></mcfile>**: This module contains the core logic for converting TypeScript type nodes into TypeBox `Type` expressions. Its primary function, `getTypeBoxType`, takes a TypeScript `TypeNode` as input and returns a `ts.Node` representing the equivalent TypeBox schema. This is a crucial part of the transformation process, handling various TypeScript types like primitives, arrays, objects, and unions.
167
-
168
-
-**<mcfilename="add-static-type-alias.ts"path="src/utils/add-static-type-alias.ts"></mcfile>**: This utility function is responsible for generating and adding the `export type [TypeName] = Static<typeof [TypeName]>` declaration to the output source file. This declaration is essential for enabling TypeScript's static type inference from the dynamically generated TypeBox schemas, ensuring type safety at compile time.
169
-
170
-
-**<mcfilename="typebox-codegen-utils.ts"path="src/utils/typebox-codegen-utils.ts"></mcfile>**: This file likely contains general utility functions that support the TypeBox code generation process, such as helper functions for string manipulation or AST node creation.
171
-
172
-
-**<mcfilename="typescript-ast-parser.ts"path="src/utils/typescript-ast-parser.ts"></mcfile>**: This module is responsible for parsing TypeScript source code and extracting relevant Abstract Syntax Tree (AST) information. It might provide functions to navigate the AST and identify specific nodes like type aliases, interfaces, or enums.
173
-
174
-
-**<mcfilename="typescript-ast-types.ts"path="src/utils/typescript-ast-types.ts"></mcfile>**: This file likely defines custom types or interfaces that represent the structured AST information extracted by `typescript-ast-parser.ts`, providing a consistent data model for further processing.
166
+
- <mcfilename="typebox-call.ts"path="src/utils/typebox-call.ts"></mcfile>: Contains the core logic for converting TypeScript type nodes into TypeBox `Type` expressions. `getTypeBoxType` takes a `TypeNode` as input and returns a `ts.Node` representing the equivalent TypeBox schema.
167
+
- <mcfilename="add-static-type-alias.ts"path="src/utils/add-static-type-alias.ts"></mcfile>: Generates and adds the `export type [TypeName] = Static<typeof [TypeName]>` declaration to the output source file. This declaration is essential for enabling TypeScript's static type inference from the dynamically generated TypeBox schemas, ensuring type safety at compile time.
168
+
- <mcfilename="typebox-codegen-utils.ts"path="src/utils/typebox-codegen-utils.ts"></mcfile>: Contains general utility functions that support the TypeBox code generation process, such as helper functions for string manipulation or AST node creation.
169
+
- <mcfilename="typescript-ast-parser.ts"path="src/utils/typescript-ast-parser.ts"></mcfile>: Responsible for parsing TypeScript source code and extracting relevant Abstract Syntax Tree (AST) information. It provides functions to navigate the AST and identify specific nodes like type aliases, interfaces, or enums.
170
+
- <mcfilename="typescript-ast-types.ts"path="src/utils/typescript-ast-types.ts"></mcfile>: Defines custom types and interfaces that represent the structured AST information extracted by `typescript-ast-parser.ts`, providing a consistent data model for further processing.
175
171
176
172
### Handlers Directory
177
173
@@ -189,13 +185,13 @@ This directory contains a collection of specialized handler modules, each respon
- <mcfilename="function-type-handler.ts"path="src/handlers/typebox/function-type-handler.ts"></mcfile>: Handles TypeScript function types and function declarations, including parameter types, optional parameters, and return types.
- <mcfilename="type-operator-handler.ts"path="src/handlers/typebox/type-operator-handler.ts"></mcfile>: Handles TypeScript type operators like `keyof`, `typeof`.
196
192
- <mcfilename="type-reference-handler.ts"path="src/handlers/typebox/type-reference-handler.ts"></mcfile>: Handles references to other types (e.g., `MyType`).
197
193
- <mcfilename="typebox-type-handler.ts"path="src/handlers/typebox/typebox-type-handler.ts"></mcfile>: A generic handler for TypeBox types.
198
-
- <mcfilename="typebox-type-handlers.ts"path="src/handlers/typebox/typebox-type-handlers.ts"></mcfile>: This file likely orchestrates the use of the individual type handlers, acting as a dispatcher based on the type of AST node encountered.
194
+
- <mcfilename="typebox-type-handlers.ts"path="src/handlers/typebox/typebox-type-handlers.ts"></mcfile>: Orchestrates the use of the individual type handlers, acting as a dispatcher based on the type of AST node encountered.
199
195
- <mcfilename="union-type-handler.ts"path="src/handlers/typebox/union-type-handler.ts"></mcfile>: Handles TypeScript union types (e.g., `string | number`).
200
196
201
197
### Parsers Directory
@@ -273,7 +269,7 @@ The optimizations maintain full backward compatibility and test reliability whil
273
269
274
270
### Performance Testing
275
271
276
-
To ensure the dependency collection system performs efficiently under various scenarios, comprehensive performance tests have been implemented in <mcfilename="dependency-collector.performance.test.ts"path="tests/ts-morph/dependency-collector.performance.test.ts"></mcfile>. These tests specifically target potential bottlenecks in dependency collection and import processing.
272
+
To ensure the dependency collection system performs efficiently under various scenarios, comprehensive performance tests have been implemented in <mcfilename="dependency-collector.performance.test.ts"path="tests/dependency-collector.performance.test.ts"></mcfile>. These tests specifically target potential bottlenecks in dependency collection and import processing.
277
273
278
274
## Process Overview
279
275
@@ -303,29 +299,27 @@ The project uses Bun as the test runner. Here are the key commands for running t
303
299
bun test
304
300
305
301
# Run a specific test file
306
-
bun test tests/ts-morph/function-types.test.ts
302
+
bun test tests/handlers/typebox/function-types.test.ts
307
303
308
304
# Run tests in a specific directory
309
-
bun test tests/ts-morph/
305
+
bun test tests/
310
306
```
311
307
312
308
### TDD Workflow for New Features
313
309
314
310
When implementing new type handlers or features:
315
311
316
-
1.**Start with Tests**: Create test cases in the appropriate test file (e.g., `tests/ts-morph/function-types.test.ts` for function-related features)
317
-
2.**Run Tests First**: Execute `bun test tests/ts-morph/[test-file]` to confirm tests fail as expected
312
+
1.**Start with Tests**: Create test cases in the appropriate test file (e.g., `tests/handlers/typebox/function-types.test.ts` for function-related features)
313
+
2.**Run Tests First**: Execute `bun test tests/handlers/typebox/[test-file]` to confirm tests fail as expected
318
314
3.**Implement Handler**: Create or modify the type handler to make tests pass
319
315
4.**Verify Implementation**: Run tests again to ensure they pass
320
316
5.**Integration Testing**: Run the full test suite with `bun test` to ensure no regressions
321
-
6.**Manual Verification**: Test with integration examples like `tests/integration/wikibase/wikibase.ts`
322
317
323
318
### Test Organization
324
319
325
320
Tests are organized into several categories:
326
321
327
-
-**Unit Tests** (`tests/ts-morph/`): Test individual type handlers and parsers
328
-
-**Integration Tests** (`tests/integration/`): Test end-to-end functionality with real-world examples
322
+
-**Unit Tests** (`tests/handlers/`): Test individual type handlers and parsers
329
323
-**Performance Tests**: Validate performance characteristics of complex operations
330
324
331
325
### Best Practices
@@ -337,8 +331,8 @@ Tests are organized into several categories:
337
331
- Include edge cases and error conditions
338
332
- Run specific tests frequently during development
339
333
- Run the full test suite before committing changes
340
-
- Run any specific tests with path like `bun test tests/ts-morph/function-types.test.ts`
341
-
- Run any specific test cases using command like `bun test tests/ts-morph/function-types.test.ts -t function types`
334
+
- Run any specific tests with path like `bun test tests/handlers/typebox/function-types.test.ts`
335
+
- Run any specific test cases using command like `bun test tests/handlers/typebox/function-types.test.ts -t function types`
342
336
- If tests keep failing, take help from tsc, lint commands to detect for any issues
0 commit comments