Commit bb5700a
authored
refactor(spec2cdk): use typewriter module and import system (#36345)
### Reason for this change
The spec2cdk code generation was maintaining a custom selective import system that duplicated functionality already provided by typewriter. This duplication made the code harder to maintain and limited flexibility when generating code for different packages.
### Description of changes
This refactoring removes the custom import tracking system in favor of using typewriter's built-in module and import capabilities.
The previous approach had `RelationshipDecider` track imports as side effects in an array, which were then registered with the submodule and later applied during postprocessing. This worked fine when all imports came from `aws-cdk-lib`, but it hardcoded the import location and couldn't handle generating code for other packages like mixins that need to import from different locations. By switching to typewriter's native import system, we can make the import location configurable through `RelationshipDeciderProps`, and imports are handled automatically when types are used rather than requiring manual registration and application steps.
The custom `SelectiveImport` interface and all the `registerSelectiveImports()` calls throughout the codebase were unnecessary duplication of what typewriter already provides. Typewriter's `SelectiveModuleImport` handles the same use case with proper type safety and automatic import generation. We just needed to add a feature to allow aliased selective imports, which was provided by the typewriter update in this PR. Removing this custom system eliminates an entire layer of manual import tracking and reduces the amount of code we need to maintain.
The consolidation of reference interfaces under `CDK_INTERFACES` follows the existing pattern used for other CDK modules like `CDK_CORE` and `CONSTRUCTS`. Previously, the logs builder had its own `CdkRefInterfaces` module while the main builder used a different approach. Using a single `CDK_INTERFACES` module with common reference types like `IBucketRef`, `ILogGroupRef`, and `IDeliveryStreamRef` provides consistency across all builders and makes the code easier to understand.
The `Relationship` interface now returns typewriter `Type` objects instead of string type names because typewriter can handle the import generation automatically when these types are used. This eliminates the need for the separate import tracking and application logic that was spread across multiple classes.
### Describe any new or updated permissions being added
No permission changes.
### Description of how you validated changes
Existing unit tests pass and code generation produces equivalent output with correctly generated import statements.
### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)
----
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*1 parent e2987eb commit bb5700a
File tree
26 files changed
+165
-208
lines changed- packages/@aws-cdk
- custom-resource-handlers
- test/custom-resources-framework
- expected
- node-runtime
- python-runtime
- mixins-preview
- scripts
- spec2logs
- spec2mixins
- tools/@aws-cdk/spec2cdk
- lib
- cdk
- naming
- test
- __snapshots__
26 files changed
+165
-208
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | 3 | | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | 3 | | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | 3 | | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | 3 | | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | 3 | | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/module-importer.test.ts
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
661 | 661 | | |
662 | 662 | | |
663 | 663 | | |
664 | | - | |
| 664 | + | |
665 | 665 | | |
666 | 666 | | |
667 | 667 | | |
| |||
0 commit comments