From 92080a22fdda6f68d17a32a7efa27b841c8afcdb Mon Sep 17 00:00:00 2001 From: Niranjan Jayakar Date: Sun, 4 Oct 2020 16:03:28 +0100 Subject: [PATCH] chore(core): migrate constructs to use "constructs" module (#10655) With all of CDK construct libraries in the repo migrated[1],[2],[3] to use "constructs" module, finally move "core" module also to use the same pattern. [1]: https://github.com/aws/aws-cdk/commit/c17969926a2644fc9607dd9b5f105450ed64309a [2]: https://github.com/aws/aws-cdk/commit/60c782fe173449ebf912f509de7db6df89985915 [3]: https://github.com/aws/aws-cdk/commit/a76428bd8559e497cd631d17ff9af2a01792b4c2 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/core/lib/asset-staging.ts | 8 ++++++-- .../core/lib/cfn-codedeploy-blue-green-hook.ts | 2 +- packages/@aws-cdk/core/lib/cfn-condition.ts | 2 +- packages/@aws-cdk/core/lib/cfn-element.ts | 16 ++++++++++------ packages/@aws-cdk/core/lib/cfn-hook.ts | 2 +- packages/@aws-cdk/core/lib/cfn-include.ts | 2 +- packages/@aws-cdk/core/lib/cfn-json.ts | 8 ++++++-- packages/@aws-cdk/core/lib/cfn-mapping.ts | 2 +- packages/@aws-cdk/core/lib/cfn-output.ts | 2 +- packages/@aws-cdk/core/lib/cfn-parameter.ts | 2 +- packages/@aws-cdk/core/lib/cfn-resource.ts | 8 ++++---- packages/@aws-cdk/core/lib/cfn-rule.ts | 2 +- .../custom-resource-provider.ts | 8 ++++++-- packages/@aws-cdk/core/lib/custom-resource.ts | 2 +- packages/@aws-cdk/core/lib/nested-stack.ts | 10 +++++++--- packages/@aws-cdk/core/lib/stack.ts | 10 +++++++--- packages/@aws-cdk/core/lib/stage.ts | 9 ++++++--- packages/@aws-cdk/core/lib/tag-aspect.ts | 2 +- packages/@aws-cdk/core/lib/util.ts | 2 +- packages/@aws-cdk/core/package.json | 5 ++++- packages/awslint/lib/rules/construct.ts | 2 +- 21 files changed, 68 insertions(+), 38 deletions(-) diff --git a/packages/@aws-cdk/core/lib/asset-staging.ts b/packages/@aws-cdk/core/lib/asset-staging.ts index 53f607f9588a0..ca051c2c48165 100644 --- a/packages/@aws-cdk/core/lib/asset-staging.ts +++ b/packages/@aws-cdk/core/lib/asset-staging.ts @@ -2,14 +2,18 @@ import * as crypto from 'crypto'; import * as os from 'os'; import * as path from 'path'; import * as cxapi from '@aws-cdk/cx-api'; +import { Construct } from 'constructs'; import * as fs from 'fs-extra'; import { AssetHashType, AssetOptions } from './assets'; import { BundlingOptions } from './bundling'; -import { Construct } from './construct-compat'; import { FileSystem, FingerprintOptions } from './fs'; import { Stack } from './stack'; import { Stage } from './stage'; +// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch. +// eslint-disable-next-line +import { Construct as CoreConstruct } from './construct-compat'; + /** * Initialization properties for `AssetStaging`. */ @@ -38,7 +42,7 @@ export interface AssetStagingProps extends FingerprintOptions, AssetOptions { * The file/directory are staged based on their content hash (fingerprint). This * means that only if content was changed, copy will happen. */ -export class AssetStaging extends Construct { +export class AssetStaging extends CoreConstruct { /** * The directory inside the bundling container into which the asset sources will be mounted. * @experimental diff --git a/packages/@aws-cdk/core/lib/cfn-codedeploy-blue-green-hook.ts b/packages/@aws-cdk/core/lib/cfn-codedeploy-blue-green-hook.ts index 67f1b1b489fc5..c0950afb258af 100644 --- a/packages/@aws-cdk/core/lib/cfn-codedeploy-blue-green-hook.ts +++ b/packages/@aws-cdk/core/lib/cfn-codedeploy-blue-green-hook.ts @@ -1,7 +1,7 @@ +import { Construct } from 'constructs'; import { CfnHook } from './cfn-hook'; import { FromCloudFormationOptions } from './cfn-parse'; import { CfnResource } from './cfn-resource'; -import { Construct } from './construct-compat'; /** * The possible types of traffic shifting for the blue-green deployment configuration. diff --git a/packages/@aws-cdk/core/lib/cfn-condition.ts b/packages/@aws-cdk/core/lib/cfn-condition.ts index 354ad1f32d122..db3fe5915e512 100644 --- a/packages/@aws-cdk/core/lib/cfn-condition.ts +++ b/packages/@aws-cdk/core/lib/cfn-condition.ts @@ -1,5 +1,5 @@ +import { Construct } from 'constructs'; import { CfnElement } from './cfn-element'; -import { Construct } from './construct-compat'; import { IResolvable, IResolveContext } from './resolvable'; export interface CfnConditionProps { diff --git a/packages/@aws-cdk/core/lib/cfn-element.ts b/packages/@aws-cdk/core/lib/cfn-element.ts index 7aadf278f13cd..d381441f9a19e 100644 --- a/packages/@aws-cdk/core/lib/cfn-element.ts +++ b/packages/@aws-cdk/core/lib/cfn-element.ts @@ -1,14 +1,17 @@ import * as cxschema from '@aws-cdk/cloud-assembly-schema'; -import { Construct } from './construct-compat'; +import { Construct, Node } from 'constructs'; + +// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch. +// eslint-disable-next-line +import { Construct as CoreConstruct } from './construct-compat'; import { Lazy } from './lazy'; -import { Token } from './token'; const CFN_ELEMENT_SYMBOL = Symbol.for('@aws-cdk/core.CfnElement'); /** * An element of a CloudFormation stack. */ -export abstract class CfnElement extends Construct { +export abstract class CfnElement extends CoreConstruct { /** * Returns `true` if a construct is a stack element (i.e. part of the * synthesized cloudformation template). @@ -58,10 +61,10 @@ export abstract class CfnElement extends Construct { this.stack = Stack.of(this); this.logicalId = Lazy.stringValue({ produce: () => this.synthesizeLogicalId() }, { - displayHint: `${notTooLong(this.node.path)}.LogicalID`, + displayHint: `${notTooLong(Node.of(this).path)}.LogicalID`, }); - this.node.addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, this.constructor); + Node.of(this).addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, this.constructor); } /** @@ -78,7 +81,7 @@ export abstract class CfnElement extends Construct { * node +internal+ entries filtered. */ public get creationStack(): string[] { - const trace = this.node.metadata.find(md => md.type === cxschema.ArtifactMetadataEntryType.LOGICAL_ID)!.trace; + const trace = Node.of(this).metadata.find(md => md.type === cxschema.ArtifactMetadataEntryType.LOGICAL_ID)!.trace; if (!trace) { return []; } @@ -161,3 +164,4 @@ function notTooLong(x: string) { import { CfnReference } from './private/cfn-reference'; import { Stack } from './stack'; +import { Token } from './token'; diff --git a/packages/@aws-cdk/core/lib/cfn-hook.ts b/packages/@aws-cdk/core/lib/cfn-hook.ts index 8e83ae4e8da28..3860e5afbc701 100644 --- a/packages/@aws-cdk/core/lib/cfn-hook.ts +++ b/packages/@aws-cdk/core/lib/cfn-hook.ts @@ -1,5 +1,5 @@ +import { Construct } from 'constructs'; import { CfnElement } from './cfn-element'; -import { Construct } from './construct-compat'; import { ignoreEmpty } from './util'; /** diff --git a/packages/@aws-cdk/core/lib/cfn-include.ts b/packages/@aws-cdk/core/lib/cfn-include.ts index 61f70072f47a6..4cfedc1c03c25 100644 --- a/packages/@aws-cdk/core/lib/cfn-include.ts +++ b/packages/@aws-cdk/core/lib/cfn-include.ts @@ -1,5 +1,5 @@ +import { Construct } from 'constructs'; import { CfnElement } from './cfn-element'; -import { Construct } from './construct-compat'; export interface CfnIncludeProps { /** diff --git a/packages/@aws-cdk/core/lib/cfn-json.ts b/packages/@aws-cdk/core/lib/cfn-json.ts index eee32038a0099..0e5801ed8ade9 100644 --- a/packages/@aws-cdk/core/lib/cfn-json.ts +++ b/packages/@aws-cdk/core/lib/cfn-json.ts @@ -1,4 +1,4 @@ -import { Construct } from './construct-compat'; +import { Construct } from 'constructs'; import { CustomResource } from './custom-resource'; import { CfnUtilsProvider } from './private/cfn-utils-provider'; import { CfnUtilsResourceType } from './private/cfn-utils-provider/consts'; @@ -7,6 +7,10 @@ import { IResolvable, IResolveContext } from './resolvable'; import { Stack } from './stack'; import { captureStackTrace } from './stack-trace'; +// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch. +// eslint-disable-next-line +import { Construct as CoreConstruct } from './construct-compat'; + export interface CfnJsonProps { /** * The value to resolve. Can be any JavaScript object, including tokens and @@ -29,7 +33,7 @@ export interface CfnJsonProps { * * This construct is backed by a custom resource. */ -export class CfnJson extends Construct implements IResolvable { +export class CfnJson extends CoreConstruct implements IResolvable { public readonly creationStack: string[] = []; /** diff --git a/packages/@aws-cdk/core/lib/cfn-mapping.ts b/packages/@aws-cdk/core/lib/cfn-mapping.ts index 7399d29f31e81..196baf3aadb96 100644 --- a/packages/@aws-cdk/core/lib/cfn-mapping.ts +++ b/packages/@aws-cdk/core/lib/cfn-mapping.ts @@ -1,6 +1,6 @@ +import { Construct } from 'constructs'; import { CfnRefElement } from './cfn-element'; import { Fn } from './cfn-fn'; -import { Construct } from './construct-compat'; import { Token } from './token'; export interface CfnMappingProps { diff --git a/packages/@aws-cdk/core/lib/cfn-output.ts b/packages/@aws-cdk/core/lib/cfn-output.ts index 3f2784f890073..ae1a52159244c 100644 --- a/packages/@aws-cdk/core/lib/cfn-output.ts +++ b/packages/@aws-cdk/core/lib/cfn-output.ts @@ -1,5 +1,5 @@ +import { Construct } from 'constructs'; import { CfnElement } from './cfn-element'; -import { Construct } from './construct-compat'; export interface CfnOutputProps { /** diff --git a/packages/@aws-cdk/core/lib/cfn-parameter.ts b/packages/@aws-cdk/core/lib/cfn-parameter.ts index c9f219d17ae72..18182f5ef7de2 100644 --- a/packages/@aws-cdk/core/lib/cfn-parameter.ts +++ b/packages/@aws-cdk/core/lib/cfn-parameter.ts @@ -1,5 +1,5 @@ +import { Construct } from 'constructs'; import { CfnElement } from './cfn-element'; -import { Construct } from './construct-compat'; import { CfnReference } from './private/cfn-reference'; import { IResolvable, IResolveContext } from './resolvable'; import { Token } from './token'; diff --git a/packages/@aws-cdk/core/lib/cfn-resource.ts b/packages/@aws-cdk/core/lib/cfn-resource.ts index 4cb8934617c2b..c899d95b657c8 100644 --- a/packages/@aws-cdk/core/lib/cfn-resource.ts +++ b/packages/@aws-cdk/core/lib/cfn-resource.ts @@ -4,7 +4,7 @@ import { CfnCondition } from './cfn-condition'; /* eslint-disable import/order */ import { CfnRefElement } from './cfn-element'; import { CfnCreationPolicy, CfnDeletionPolicy, CfnUpdatePolicy } from './cfn-resource-policy'; -import { Construct, IConstruct } from './construct-compat'; +import { Construct, IConstruct, Node } from 'constructs'; import { addDependency } from './deps'; import { CfnReference } from './private/cfn-reference'; import { Reference } from './reference'; @@ -92,8 +92,8 @@ export class CfnResource extends CfnRefElement { // if aws:cdk:enable-path-metadata is set, embed the current construct's // path in the CloudFormation template, so it will be possible to trace // back to the actual construct path. - if (this.node.tryGetContext(cxapi.PATH_METADATA_ENABLE_CONTEXT)) { - this.addMetadata(cxapi.PATH_METADATA_KEY, this.node.path); + if (Node.of(this).tryGetContext(cxapi.PATH_METADATA_ENABLE_CONTEXT)) { + this.addMetadata(cxapi.PATH_METADATA_KEY, Node.of(this).path); } } @@ -238,7 +238,7 @@ export class CfnResource extends CfnRefElement { return; } - addDependency(this, target, `"${this.node.path}" depends on "${target.node.path}"`); + addDependency(this, target, `"${Node.of(this).path}" depends on "${Node.of(target).path}"`); } /** diff --git a/packages/@aws-cdk/core/lib/cfn-rule.ts b/packages/@aws-cdk/core/lib/cfn-rule.ts index 847c67982192b..f3141ca636ce2 100644 --- a/packages/@aws-cdk/core/lib/cfn-rule.ts +++ b/packages/@aws-cdk/core/lib/cfn-rule.ts @@ -1,6 +1,6 @@ +import { Construct } from 'constructs'; import { ICfnConditionExpression } from './cfn-condition'; import { CfnRefElement } from './cfn-element'; -import { Construct } from './construct-compat'; import { capitalizePropertyNames } from './util'; /** diff --git a/packages/@aws-cdk/core/lib/custom-resource-provider/custom-resource-provider.ts b/packages/@aws-cdk/core/lib/custom-resource-provider/custom-resource-provider.ts index 10b53033f3f7c..821307e466a77 100644 --- a/packages/@aws-cdk/core/lib/custom-resource-provider/custom-resource-provider.ts +++ b/packages/@aws-cdk/core/lib/custom-resource-provider/custom-resource-provider.ts @@ -1,9 +1,9 @@ import * as fs from 'fs'; import * as path from 'path'; +import { Construct } from 'constructs'; import { AssetStaging } from '../asset-staging'; import { FileAssetPackaging } from '../assets'; import { CfnResource } from '../cfn-resource'; -import { Construct } from '../construct-compat'; import { Duration } from '../duration'; import { Size } from '../size'; import { Stack } from '../stack'; @@ -12,6 +12,10 @@ import { Token } from '../token'; const ENTRYPOINT_FILENAME = '__entrypoint__'; const ENTRYPOINT_NODEJS_SOURCE = path.join(__dirname, 'nodejs-entrypoint.js'); +// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch. +// eslint-disable-next-line +import { Construct as CoreConstruct } from '../construct-compat'; + /** * Initialization properties for `CustomResourceProvider`. * @@ -75,7 +79,7 @@ export enum CustomResourceProviderRuntime { * * @experimental */ -export class CustomResourceProvider extends Construct { +export class CustomResourceProvider extends CoreConstruct { /** * Returns a stack-level singleton ARN (service token) for the custom resource * provider. diff --git a/packages/@aws-cdk/core/lib/custom-resource.ts b/packages/@aws-cdk/core/lib/custom-resource.ts index 6926d8fc31a56..7fbf9505cf971 100644 --- a/packages/@aws-cdk/core/lib/custom-resource.ts +++ b/packages/@aws-cdk/core/lib/custom-resource.ts @@ -1,5 +1,5 @@ +import { Construct } from 'constructs'; import { CfnResource } from './cfn-resource'; -import { Construct } from './construct-compat'; import { RemovalPolicy } from './removal-policy'; import { Resource } from './resource'; import { Token } from './token'; diff --git a/packages/@aws-cdk/core/lib/nested-stack.ts b/packages/@aws-cdk/core/lib/nested-stack.ts index 7e52427467039..395d3bd63c8c5 100644 --- a/packages/@aws-cdk/core/lib/nested-stack.ts +++ b/packages/@aws-cdk/core/lib/nested-stack.ts @@ -1,10 +1,10 @@ import * as crypto from 'crypto'; +import { Construct, Node } from 'constructs'; import { FileAssetPackaging } from './assets'; import { Fn } from './cfn-fn'; import { Aws } from './cfn-pseudo'; import { CfnResource } from './cfn-resource'; import { CfnStack } from './cloudformation.generated'; -import { Construct } from './construct-compat'; import { Duration } from './duration'; import { Lazy } from './lazy'; import { IResolveContext } from './resolvable'; @@ -12,6 +12,10 @@ import { Stack } from './stack'; import { NestedStackSynthesizer } from './stack-synthesizers'; import { Token } from './token'; +// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch. +// eslint-disable-next-line +import { Construct as CoreConstruct } from './construct-compat'; + const NESTED_STACK_SYMBOL = Symbol.for('@aws-cdk/core.NestedStack'); /** @@ -105,7 +109,7 @@ export class NestedStack extends Stack { this._parentStack = parentStack; // @deprecate: remove this in v2.0 (redundent) - const parentScope = new Construct(scope, id + '.NestedStack'); + const parentScope = new CoreConstruct(scope, id + '.NestedStack'); Object.defineProperty(this, NESTED_STACK_SYMBOL, { value: true }); @@ -223,7 +227,7 @@ function findParentStack(scope: Construct): Stack { throw new Error('Nested stacks cannot be defined as a root construct'); } - const parentStack = scope.node.scopes.reverse().find(p => Stack.isStack(p)); + const parentStack = Node.of(scope).scopes.reverse().find(p => Stack.isStack(p)); if (!parentStack) { throw new Error('Nested stacks must be defined within scope of another non-nested stack'); } diff --git a/packages/@aws-cdk/core/lib/stack.ts b/packages/@aws-cdk/core/lib/stack.ts index 8d9c57fe91942..88f407ef01839 100644 --- a/packages/@aws-cdk/core/lib/stack.ts +++ b/packages/@aws-cdk/core/lib/stack.ts @@ -2,7 +2,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as cxschema from '@aws-cdk/cloud-assembly-schema'; import * as cxapi from '@aws-cdk/cx-api'; -import { IConstruct, Node } from 'constructs'; +import { IConstruct, Construct, Node } from 'constructs'; import { Annotations } from './annotations'; import { App } from './app'; import { Arn, ArnComponents } from './arn'; @@ -11,7 +11,7 @@ import { CfnElement } from './cfn-element'; import { Fn } from './cfn-fn'; import { Aws, ScopedAws } from './cfn-pseudo'; import { CfnResource, TagType } from './cfn-resource'; -import { Construct, ISynthesisSession } from './construct-compat'; +import { ISynthesisSession } from './construct-compat'; import { ContextProvider } from './context-provider'; import { Environment } from './environment'; import { FeatureFlags } from './feature-flags'; @@ -20,6 +20,10 @@ import { LogicalIDs } from './private/logical-id'; import { resolve } from './private/resolve'; import { makeUniqueId } from './private/uniqueid'; +// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch. +// eslint-disable-next-line +import { Construct as CoreConstruct } from './construct-compat'; + const STACK_SYMBOL = Symbol.for('@aws-cdk/core.Stack'); const MY_STACK_CACHE = Symbol.for('@aws-cdk/core.Stack.myStack'); @@ -140,7 +144,7 @@ export interface StackProps { /** * A root construct which represents a single CloudFormation stack. */ -export class Stack extends Construct implements ITaggable { +export class Stack extends CoreConstruct implements ITaggable { /** * Return whether the given object is a Stack. * diff --git a/packages/@aws-cdk/core/lib/stage.ts b/packages/@aws-cdk/core/lib/stage.ts index bf18e89d89313..50ef0b7c5b081 100644 --- a/packages/@aws-cdk/core/lib/stage.ts +++ b/packages/@aws-cdk/core/lib/stage.ts @@ -1,9 +1,12 @@ import * as cxapi from '@aws-cdk/cx-api'; -import { IConstruct, Node } from 'constructs'; -import { Construct } from './construct-compat'; +import { IConstruct, Construct, Node } from 'constructs'; import { Environment } from './environment'; import { synthesize } from './private/synthesis'; +// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch. +// eslint-disable-next-line +import { Construct as CoreConstruct } from './construct-compat'; + /** * Initialization props for a stage. */ @@ -65,7 +68,7 @@ export interface StageProps { * copies of your application which should be be deployed to different * environments. */ -export class Stage extends Construct { +export class Stage extends CoreConstruct { /** * Return the stage this construct is contained with, if available. If called * on a nested stage, returns its parent. diff --git a/packages/@aws-cdk/core/lib/tag-aspect.ts b/packages/@aws-cdk/core/lib/tag-aspect.ts index 34d9a6dfd4e40..d56c28b551d87 100644 --- a/packages/@aws-cdk/core/lib/tag-aspect.ts +++ b/packages/@aws-cdk/core/lib/tag-aspect.ts @@ -1,8 +1,8 @@ // import * as cxapi from '@aws-cdk/cx-api'; +import { Annotations } from './annotations'; import { IAspect, Aspects } from './aspect'; import { Construct, IConstruct } from './construct-compat'; import { ITaggable, TagManager } from './tag-manager'; -import { Annotations } from './annotations'; /** * Properties for a tag diff --git a/packages/@aws-cdk/core/lib/util.ts b/packages/@aws-cdk/core/lib/util.ts index 6924b197667af..e626600a206cd 100644 --- a/packages/@aws-cdk/core/lib/util.ts +++ b/packages/@aws-cdk/core/lib/util.ts @@ -1,4 +1,4 @@ -import { IConstruct } from './construct-compat'; +import { IConstruct } from 'constructs'; import { Intrinsic } from './private/intrinsic'; import { IPostProcessor, IResolveContext } from './resolvable'; import { Stack } from './stack'; diff --git a/packages/@aws-cdk/core/package.json b/packages/@aws-cdk/core/package.json index 87db86c38a8e1..130dc095e88eb 100644 --- a/packages/@aws-cdk/core/package.json +++ b/packages/@aws-cdk/core/package.json @@ -141,7 +141,10 @@ "cfn2ts-core-import": ".", "pre": [ "rm -rf test/fs/fixtures && cd test/fs && tar -xzf fixtures.tar.gz" - ] + ], + "env": { + "AWSLINT_BASE_CONSTRUCT": "true" + } }, "nyc": { "statements": 55, diff --git a/packages/awslint/lib/rules/construct.ts b/packages/awslint/lib/rules/construct.ts index 0cdb50eebded6..8e591e1f27d8f 100644 --- a/packages/awslint/lib/rules/construct.ts +++ b/packages/awslint/lib/rules/construct.ts @@ -97,7 +97,7 @@ constructLinter.add({ const expectedParams = new Array(); let baseType; - if (process.env.AWSLINT_BASE_CONSTRUCT && !initializer.parentType.name.startsWith('Cfn')) { + if (process.env.AWSLINT_BASE_CONSTRUCT && !CoreTypes.isCfnResource(e.ctx.classType)) { baseType = e.ctx.core.baseConstructClass; } else { baseType = e.ctx.core.constructClass;