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(compiler-config): Implement automatic script target detection and identifier validation (#19)
The changes in this commit introduce a new `CompilerConfig` singleton that manages the TypeScript compiler options and script target configuration for the codegen system.
The key changes are:
1. Automatic script target detection: The system now automatically determines the appropriate TypeScript script target from the `ts-morph` Project's compiler options.
2. Identifier validation: Property names in generated TypeBox objects are validated using TypeScript's built-in utilities (`ts.isIdentifierStart()` and `ts.isIdentifierPart()`). This ensures compatibility with the detected script target.
3. Configuration management: The `CompilerConfig` singleton provides a centralized way to manage the script target configuration, allowing it to be overridden per-project as needed.
4. Default behavior: When no explicit target is specified, the system falls back to `ts.ScriptTarget.Latest`, providing maximum compatibility with modern JavaScript features.
5. Integration points: The configuration system is integrated with various components of the codegen system, including the Input Handler, Code Generation, Identifier Utils, and Object Handlers.
These changes improve the overall robustness and flexibility of the codegen system, ensuring that the generated code is compatible with the target JavaScript environment.
The `CompilerConfig` singleton manages script target configuration:
33
+
34
+
-**Singleton Pattern** - Ensures consistent configuration across the application
35
+
-**Environment Detection** - Automatically detects appropriate targets from TypeScript version
36
+
-**Project Override** - Respects explicit targets from ts-morph Project configuration
37
+
-**Runtime Configuration** - Allows manual target specification when needed
38
+
39
+
## Environment-Based Target Detection
40
+
41
+
When no explicit target is specified in the project configuration, the system automatically detects an appropriate target based on the TypeScript version:
42
+
43
+
-**TypeScript 5.2+** → ES2023
44
+
-**TypeScript 5.0+** → ES2022
45
+
-**TypeScript 4.9+** → ES2022
46
+
-**TypeScript 4.7+** → ES2021
47
+
-**TypeScript 4.5+** → ES2020
48
+
-**TypeScript 4.2+** → ES2019
49
+
-**TypeScript 4.1+** → ES2018
50
+
-**TypeScript 4.0+** → ES2017
51
+
-**TypeScript 3.8+** → ES2017
52
+
-**TypeScript 3.6+** → ES2016
53
+
-**TypeScript 3.4+** → ES2015
54
+
-**Older versions** → ES5
55
+
56
+
This ensures generated code uses language features that are supported by the available TypeScript compiler, avoiding compatibility issues.
57
+
58
+
## Integration Points
59
+
60
+
The configuration system integrates with:
61
+
62
+
-**Input Handler** - Initializes config when creating source files
63
+
-**Code Generation** - Uses config for output file creation
64
+
-**Identifier Utils** - Validates property names with correct target
65
+
-**Object Handlers** - Determines property name formatting
Copy file name to clipboardExpand all lines: docs/handler-system.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,14 @@ export abstract class BaseTypeHandler {
30
30
-`ObjectTypeHandler` - { prop: T }
31
31
-`InterfaceTypeHandler` - interface references
32
32
33
+
Object property names are extracted using the TypeScript compiler API through `PropertySignature.getNameNode()`. The system handles different property name formats:
34
+
35
+
-**Identifiers** (`prop`) - extracted using `nameNode.getText()` and preserved as identifiers
36
+
-**String literals** (`'prop-name'`, `"prop name"`) - extracted using `nameNode.getLiteralValue()` and validated for identifier compatibility
37
+
-**Numeric literals** (`123`) - extracted using `nameNode.getLiteralValue().toString()` and treated as identifiers
38
+
39
+
The system uses TypeScript's built-in character validation utilities (`ts.isIdentifierStart` and `ts.isIdentifierPart`) with runtime-determined script targets to determine if property names can be safely used as unquoted identifiers in the generated code. The script target is automatically determined from the ts-morph Project's compiler options, ensuring compatibility with the target JavaScript environment while maintaining optimal output format.
Validates JavaScript identifiers using TypeScript's built-in utilities with full Unicode support, including characters outside the Basic Multilingual Plane.
0 commit comments