Skip to content

Commit

Permalink
refactor: rename all L1 enum-like members to ALL_CAPS (#3024)
Browse files Browse the repository at this point in the history
Dropped the exemption from L1 constructs as all public static properties
should be named using ALL_CAPS.

This is in accordance with the new construct library guidelines.

BREAKING CHANGE:
* **core** `cfnResourceTypeName` is now `CFN_RESOURCE_TYPE_NAME` in generated code.
* **aws-sam** `requiredTransform` is now `REQUIRED_TRANSFORM` in generated code.
  • Loading branch information
shivlaks authored and Elad Ben-Israel committed Jun 24, 2019
1 parent 46b3292 commit 758d496
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function uppercaseProperties(props: Properties): Properties {

function renderResourceType(resourceType?: string) {
if (!resourceType) {
return CfnCustomResource.cfnResourceTypeName;
return CfnCustomResource.CFN_RESOURCE_TYPE_NAME;
}

if (!resourceType.startsWith('Custom::')) {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ export class Vpc extends VpcBase {
}

// These values will be used to recover the config upon provider import
const includeResourceTypes = [CfnSubnet.cfnResourceTypeName];
const includeResourceTypes = [CfnSubnet.CFN_RESOURCE_TYPE_NAME];
subnet.node.applyAspect(new Tag(SUBNETNAME_TAG, subnetConfig.name, {includeResourceTypes}));
subnet.node.applyAspect(new Tag(SUBNETTYPE_TAG, subnetTypeTagValue(subnetConfig.subnetType), {includeResourceTypes}));
});
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ec2/test/test.vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ export = {

const vpc = new Vpc(stack, 'TheVPC');
// overwrite to set propagate
vpc.node.applyAspect(new Tag('BusinessUnit', 'Marketing', {includeResourceTypes: [CfnVPC.cfnResourceTypeName]}));
vpc.node.applyAspect(new Tag('BusinessUnit', 'Marketing', {includeResourceTypes: [CfnVPC.CFN_RESOURCE_TYPE_NAME]}));
vpc.node.applyAspect(new Tag('VpcType', 'Good'));
expect(stack).to(haveResource("AWS::EC2::VPC", hasTags(toCfnTags(allTags))));
const taggables = ['Subnet', 'InternetGateway', 'NatGateway', 'RouteTable'];
Expand Down
3 changes: 0 additions & 3 deletions tools/awslint/lib/rules/public-static-properties.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { Property } from 'jsii-reflect';
import { Linter } from '../linter';
import { CoreTypes } from './core-types';

const UPPER_SNAKE_CASE_ALLOWED_PATTERN = new RegExp('^[A-Z0-9][A-Z0-9_]*[A-Z0-9]+$');

export const publicStaticPropertiesLinter = new Linter(assembly => {
const result = new Array<Property>();
for (const c of assembly.classes) {
// L1 classes are exempted
if (CoreTypes.isCfnResource(c)) { continue; }
for (const property of c.allProperties) {
if (property.const && property.static) {
result.push(property);
Expand Down
10 changes: 5 additions & 5 deletions tools/cfn2ts/lib/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ export default class CodeGenerator {
this.code.line('/**');
this.code.line(` * The CloudFormation resource type name for this resource class.`);
this.code.line(' */');
this.code.line(`public static readonly cfnResourceTypeName = ${cfnResourceTypeName};`);
this.code.line(`public static readonly CFN_RESOURCE_TYPE_NAME = ${cfnResourceTypeName};`);

if (spec.RequiredTransform) {
this.code.line('/**');
this.code.line(' * The `Transform` a template must use in order to use this resource');
this.code.line(' */');
this.code.line(`public static readonly requiredTransform = ${JSON.stringify(spec.RequiredTransform)};`);
this.code.line(`public static readonly REQUIRED_TRANSFORM = ${JSON.stringify(spec.RequiredTransform)};`);
}

//
Expand Down Expand Up @@ -260,7 +260,7 @@ export default class CodeGenerator {
const optionalProps = spec.Properties && !Object.values(spec.Properties).some(p => p.Required || false);
const propsArgument = propsType ? `, props: ${propsType.className}${optionalProps ? ' = {}' : ''}` : '';
this.code.openBlock(`constructor(scope: ${CONSTRUCT_CLASS}, id: string${propsArgument})`);
this.code.line(`super(scope, id, { type: ${resourceName.className}.cfnResourceTypeName${propsType ? ', properties: props' : ''} });`);
this.code.line(`super(scope, id, { type: ${resourceName.className}.CFN_RESOURCE_TYPE_NAME${propsType ? ', properties: props' : ''} });`);
// verify all required properties
if (spec.Properties) {
for (const propName of Object.keys(spec.Properties)) {
Expand All @@ -271,14 +271,14 @@ export default class CodeGenerator {
}
}
if (spec.RequiredTransform) {
const transformField = `${resourceName.className}.requiredTransform`;
const transformField = `${resourceName.className}.REQUIRED_TRANSFORM`;
this.code.line('// If a different transform than the required one is in use, this resource cannot be used');
this.code.openBlock(`if (this.stack.templateOptions.transform && this.stack.templateOptions.transform !== ${transformField})`);
// tslint:disable-next-line:max-line-length
this.code.line(`throw new Error(\`The \${JSON.stringify(${transformField})} transform is required when using ${resourceName.className}, but the \${JSON.stringify(this.stack.templateOptions.transform)} is used.\`);`);
this.code.closeBlock();
this.code.line('// Automatically configure the required transform');
this.code.line(`this.stack.templateOptions.transform = ${resourceName.className}.requiredTransform;`);
this.code.line(`this.stack.templateOptions.transform = ${resourceName.className}.REQUIRED_TRANSFORM;`);
}

// initialize all attribute properties
Expand Down

0 comments on commit 758d496

Please sign in to comment.