Skip to content

Commit

Permalink
chore(codegen): add Type Annotation (aws#3294)
Browse files Browse the repository at this point in the history
  • Loading branch information
tokkiyaa authored and rix0rrr committed Jul 12, 2019
1 parent ceb857d commit 44e6645
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
16 changes: 8 additions & 8 deletions tools/cfn2ts/lib/augmentation-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export class AugmentationGenerator {
/**
* Saves the generated file.
*/
public async save(dir: string) {
public async save(dir: string): Promise<string[]> {
this.code.closeFile(this.outputFile);
return await this.code.save(dir);
}

private emitMetricAugmentations(resourceTypeName: string, metrics: schema.ResourceMetricAugmentations, options?: schema.AugmentationOptions) {
private emitMetricAugmentations(resourceTypeName: string, metrics: schema.ResourceMetricAugmentations, options?: schema.AugmentationOptions): void {
const cfnName = SpecName.parse(resourceTypeName);
const resourceName = genspec.CodeName.forCfnResource(cfnName, this.affix);
const l2ClassName = resourceName.className.replace(/^Cfn/, '');
Expand Down Expand Up @@ -78,14 +78,14 @@ export class AugmentationGenerator {
}
}

private emitMetricFunctionDeclaration(resource: SpecName) {
private emitMetricFunctionDeclaration(resource: SpecName): void {
this.code.line(`/**`);
this.code.line(` * Return the given named metric for this ${resource.resourceName}`);
this.code.line(` */`);
this.code.line(`metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric;`);
}

private emitMetricFunction(className: string, metrics: schema.ResourceMetricAugmentations) {
private emitMetricFunction(className: string, metrics: schema.ResourceMetricAugmentations): void {
this.code.line(`${className}.prototype.metric = function(metricName: string, props?: cloudwatch.MetricOptions) {`);
this.code.line(` return new cloudwatch.Metric({`);
this.code.line(` namespace: '${metrics.namespace}',`);
Expand All @@ -102,7 +102,7 @@ export class AugmentationGenerator {
this.code.line('};');
}

private emitSpecificMetricFunctionDeclaration(metric: schema.ResourceMetric) {
private emitSpecificMetricFunctionDeclaration(metric: schema.ResourceMetric): void {
this.code.line(`/**`);
this.code.line(` * ${metric.documentation}`);
this.code.line(` *`);
Expand All @@ -111,18 +111,18 @@ export class AugmentationGenerator {
this.code.line(`metric${metricFunctionName(metric)}(props?: cloudwatch.MetricOptions): cloudwatch.Metric;`);
}

private emitSpecificMetricFunction(className: string, metric: schema.ResourceMetric) {
private emitSpecificMetricFunction(className: string, metric: schema.ResourceMetric): void {
this.code.line(`${className}.prototype.metric${metricFunctionName(metric)} = function(props?: cloudwatch.MetricOptions) {`);
this.code.line(` return this.metric('${metric.name}', { statistic: '${metricStatistic(metric)}', ...props });`);
this.code.line('};');
}
}

function metricFunctionName(metric: schema.ResourceMetric) {
function metricFunctionName(metric: schema.ResourceMetric): string {
return metric.name.replace(/[^a-zA-Z0-9]/g, '');
}

function metricStatistic(metric: schema.ResourceMetric) {
function metricStatistic(metric: schema.ResourceMetric): string {
switch (metric.type) {
case schema.MetricType.Attrib:
case undefined:
Expand Down
24 changes: 12 additions & 12 deletions tools/cfn2ts/lib/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class CodeGenerator {
return false;
}

public emitCode() {
public emitCode(): void {
for (const name of Object.keys(this.spec.ResourceTypes).sort()) {
const resourceType = this.spec.ResourceTypes[name];

Expand All @@ -81,15 +81,15 @@ export default class CodeGenerator {
/**
* Saves the generated file.
*/
public async save(dir: string) {
public async save(dir: string): Promise<string[]> {
this.code.closeFile(this.outputFile);
return await this.code.save(dir);
}

/**
* Emits classes for all property types
*/
private emitPropertyTypes(resourceName: string, resourceClass: genspec.CodeName) {
private emitPropertyTypes(resourceName: string, resourceClass: genspec.CodeName): void {
const prefix = `${resourceName}.`;
for (const name of Object.keys(this.spec.PropertyTypes).sort()) {
if (!name.startsWith(prefix)) { continue; }
Expand All @@ -102,7 +102,7 @@ export default class CodeGenerator {
}
}

private openClass(name: genspec.CodeName, superClasses?: string) {
private openClass(name: genspec.CodeName, superClasses?: string): string {
const extendsPostfix = superClasses ? ` extends ${superClasses}` : '';
this.code.openBlock(`export class ${name.className}${extendsPostfix}`);
return name.className;
Expand Down Expand Up @@ -321,7 +321,7 @@ export default class CodeGenerator {
*
* Since resolve() deep-resolves, we only need to do this once.
*/
private emitCloudFormationProperties(propsType: genspec.CodeName, propMap: Dictionary<string>, taggable: boolean) {
private emitCloudFormationProperties(propsType: genspec.CodeName, propMap: Dictionary<string>, taggable: boolean): void {
this.code.openBlock('protected get cfnProperties(): { [key: string]: any } ');
this.code.indent('return {');
for (const prop of Object.values(propMap)) {
Expand Down Expand Up @@ -447,7 +447,7 @@ export default class CodeGenerator {
private emitValidator(resource: genspec.CodeName,
typeName: genspec.CodeName,
propSpecs: { [name: string]: schema.Property },
nameConversionTable: Dictionary<string>) {
nameConversionTable: Dictionary<string>): void {
const validatorName = genspec.validatorName(typeName);

this.code.line('/**');
Expand Down Expand Up @@ -546,7 +546,7 @@ export default class CodeGenerator {

}

private beginNamespace(type: genspec.CodeName) {
private beginNamespace(type: genspec.CodeName): void {
if (type.namespace) {
const parts = type.namespace.split('.');
for (const part of parts) {
Expand All @@ -555,7 +555,7 @@ export default class CodeGenerator {
}
}

private endNamespace(type: genspec.CodeName) {
private endNamespace(type: genspec.CodeName): void {
if (type.namespace) {
const parts = type.namespace.split('.');
for (const _ of parts) {
Expand All @@ -564,7 +564,7 @@ export default class CodeGenerator {
}
}

private emitPropertyType(resourceContext: genspec.CodeName, typeName: genspec.CodeName, propTypeSpec: schema.PropertyBag) {
private emitPropertyType(resourceContext: genspec.CodeName, typeName: genspec.CodeName, propTypeSpec: schema.PropertyBag): void {
this.code.line();
this.beginNamespace(typeName);

Expand Down Expand Up @@ -661,11 +661,11 @@ export default class CodeGenerator {
return this.findNativeType(context, specType);
}

private renderTypeUnion(context: genspec.CodeName, types: genspec.CodeName[]) {
private renderTypeUnion(context: genspec.CodeName, types: genspec.CodeName[]): string {
return types.map(t => this.renderCodeName(context, t)).join(' | ');
}

private docLink(link: string | undefined, ...before: string[]) {
private docLink(link: string | undefined, ...before: string[]): void {
if (!link && before.length === 0) { return; }
this.code.line('/**');
before.forEach(line => this.code.line(` * ${line}`.trimRight()));
Expand All @@ -689,7 +689,7 @@ function quoteCode(code: string): string {
return '`' + code + '`';
}

function tokenizableType(alternatives: string[]) {
function tokenizableType(alternatives: string[]): boolean {
if (alternatives.length > 1) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions tools/cfn2ts/lib/genspec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class CodeName {
* Simply returns the top-level declaration name, but reads better at the call site if
* we're generating a function instead of a class.
*/
public get functionName() {
public get functionName(): string {
return this.className;
}

Expand All @@ -66,7 +66,7 @@ export class CodeName {
*
* (When referred to it from the same package)
*/
public get fqn() {
public get fqn(): string {
return util.joinIf(this.namespace, '.', util.joinIf(this.className, '.', this.methodName));
}

Expand Down Expand Up @@ -125,7 +125,7 @@ export function packageName(module: SpecName | string): string {
/**
* Overrides special-case namespaces like serverless=>sam
*/
function overridePackageName(name: string) {
function overridePackageName(name: string): string {
if (name === 'serverless') {
return 'sam';
}
Expand Down
2 changes: 1 addition & 1 deletion tools/cfn2ts/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AugmentationGenerator } from './augmentation-generator';
import CodeGenerator from './codegen';
import { packageName } from './genspec';

export default async function(scopes: string | string[], outPath: string) {
export default async function(scopes: string | string[], outPath: string): Promise<void> {
if (outPath !== '.') { await fs.mkdirp(outPath); }

if (typeof scopes === 'string') { scopes = [scopes]; }
Expand Down
4 changes: 2 additions & 2 deletions tools/cfn2ts/lib/spec-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class SpecName {
constructor(readonly module: string, readonly resourceName: string) {
}

public get fqn() {
public get fqn(): string {
return this.module + '::' + this.resourceName;
}

Expand Down Expand Up @@ -65,7 +65,7 @@ export class PropertyAttributeName extends SpecName {
super(module, resourceName);
}

public get fqn() {
public get fqn(): string {
return joinIf(super.fqn, '.', this.propAttrName);
}
}
Expand Down

0 comments on commit 44e6645

Please sign in to comment.