Skip to content

Commit bb5700a

Browse files
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

26 files changed

+165
-208
lines changed

packages/@aws-cdk/custom-resource-handlers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"@types/jest": "^29.5.14",
4949
"aws-sdk-client-mock": "4.1.0",
5050
"aws-sdk-client-mock-jest": "4.1.0",
51-
"@cdklabs/typewriter": "^0.0.12",
51+
"@cdklabs/typewriter": "^0.0.14",
5252
"jest": "^29.7.0",
5353
"sinon": "^9.2.4",
5454
"nock": "^13.5.6",

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/node-runtime/custom-resource-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable prettier/prettier, @stylistic/max-len */
22
import * as path from "path";
33
import { Construct } from "constructs";
4-
import { Stack, CustomResourceProviderBase, CustomResourceProviderOptions, determineLatestNodeRuntimeName } from "../../../core";
4+
import { CustomResourceProviderBase, CustomResourceProviderOptions, determineLatestNodeRuntimeName, Stack } from "../../../core";
55

66
export class TestProvider extends CustomResourceProviderBase {
77
/**

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/node-runtime/function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable prettier/prettier, @stylistic/max-len */
22
import * as path from "path";
3-
import { Construct } from "constructs";
43
import * as lambda from "../../../aws-lambda";
4+
import { Construct } from "constructs";
55

66
export class TestFunction extends lambda.Function {
77
public constructor(scope: Construct, id: string, props?: lambda.FunctionOptions) {

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/node-runtime/singleton-function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable prettier/prettier, @stylistic/max-len */
22
import * as path from "path";
3-
import { Construct } from "constructs";
43
import * as lambda from "../../../aws-lambda";
4+
import { Construct } from "constructs";
55

66
export class TestSingletonFunction extends lambda.SingletonFunction {
77
public constructor(scope: Construct, id: string, props: TestSingletonFunctionProps) {

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/python-runtime/custom-resource-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable prettier/prettier, @stylistic/max-len */
22
import * as path from "path";
33
import { Construct } from "constructs";
4-
import { Stack, CustomResourceProviderBase, CustomResourceProviderOptions } from "../../../core";
4+
import { CustomResourceProviderBase, CustomResourceProviderOptions, Stack } from "../../../core";
55

66
export class TestProvider extends CustomResourceProviderBase {
77
/**

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/python-runtime/function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable prettier/prettier, @stylistic/max-len */
22
import * as path from "path";
3-
import { Construct } from "constructs";
43
import * as lambda from "../../../aws-lambda";
4+
import { Construct } from "constructs";
55

66
export class TestFunction extends lambda.Function {
77
public constructor(scope: Construct, id: string, props?: lambda.FunctionOptions) {

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/python-runtime/singleton-function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable prettier/prettier, @stylistic/max-len */
22
import * as path from "path";
3-
import { Construct } from "constructs";
43
import * as lambda from "../../../aws-lambda";
4+
import { Construct } from "constructs";
55

66
export class TestSingletonFunction extends lambda.SingletonFunction {
77
public constructor(scope: Construct, id: string, props: TestSingletonFunctionProps) {

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/expected/singleton-function-eval-nodejs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable prettier/prettier, @stylistic/max-len */
22
import * as path from "path";
3-
import { Construct } from "constructs";
43
import * as lambda from "../../../aws-lambda";
4+
import { Construct } from "constructs";
55

66
export class EvalNodejsSingletonFunction extends lambda.SingletonFunction {
77
public constructor(scope: Construct, id: string, props: EvalNodejsSingletonFunctionProps) {

packages/@aws-cdk/custom-resource-handlers/test/custom-resources-framework/module-importer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('module importer', () => {
100100

101101
// THEN
102102
const result = fs.readFileSync(path.resolve(tmpDir, 'result.ts'), 'utf-8');
103-
const expected = '/* eslint-disable prettier/prettier, @stylistic/max-len */\nimport * as lambda from "../../../aws-lambda";\nimport { Stack, CustomResourceProviderBase } from "../../../core";';
103+
const expected = '/* eslint-disable prettier/prettier, @stylistic/max-len */\nimport * as lambda from "../../../aws-lambda";\nimport { CustomResourceProviderBase, Stack } from "../../../core";';
104104
expect(result).toEqual(expected);
105105
});
106106

packages/@aws-cdk/mixins-preview/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@
661661
"@aws-cdk/service-spec-types": "^0.0.195",
662662
"@aws-cdk/spec2cdk": "0.0.0",
663663
"@cdklabs/tskb": "^0.0.4",
664-
"@cdklabs/typewriter": "^0.0.12",
664+
"@cdklabs/typewriter": "^0.0.14",
665665
"@types/jest": "^29.5.14",
666666
"aws-cdk-lib": "0.0.0",
667667
"constructs": "^10.0.0",

0 commit comments

Comments
 (0)