From 44e6645a86cec7ac82161d998b6cdee9e8d44b6b Mon Sep 17 00:00:00 2001 From: tokkiyaa Date: Fri, 12 Jul 2019 22:20:50 +0900 Subject: [PATCH] chore(codegen): add Type Annotation (#3294) --- tools/cfn2ts/lib/augmentation-generator.ts | 16 +++++++-------- tools/cfn2ts/lib/codegen.ts | 24 +++++++++++----------- tools/cfn2ts/lib/genspec.ts | 6 +++--- tools/cfn2ts/lib/index.ts | 2 +- tools/cfn2ts/lib/spec-utils.ts | 4 ++-- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/tools/cfn2ts/lib/augmentation-generator.ts b/tools/cfn2ts/lib/augmentation-generator.ts index ce5ba1308153d..fffa114f3461c 100644 --- a/tools/cfn2ts/lib/augmentation-generator.ts +++ b/tools/cfn2ts/lib/augmentation-generator.ts @@ -34,12 +34,12 @@ export class AugmentationGenerator { /** * Saves the generated file. */ - public async save(dir: string) { + public async save(dir: string): Promise { 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/, ''); @@ -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}',`); @@ -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(` *`); @@ -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: diff --git a/tools/cfn2ts/lib/codegen.ts b/tools/cfn2ts/lib/codegen.ts index dd50906c78846..9b7e4fcdfe097 100644 --- a/tools/cfn2ts/lib/codegen.ts +++ b/tools/cfn2ts/lib/codegen.ts @@ -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]; @@ -81,7 +81,7 @@ export default class CodeGenerator { /** * Saves the generated file. */ - public async save(dir: string) { + public async save(dir: string): Promise { this.code.closeFile(this.outputFile); return await this.code.save(dir); } @@ -89,7 +89,7 @@ export default class CodeGenerator { /** * 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; } @@ -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; @@ -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, taggable: boolean) { + private emitCloudFormationProperties(propsType: genspec.CodeName, propMap: Dictionary, taggable: boolean): void { this.code.openBlock('protected get cfnProperties(): { [key: string]: any } '); this.code.indent('return {'); for (const prop of Object.values(propMap)) { @@ -447,7 +447,7 @@ export default class CodeGenerator { private emitValidator(resource: genspec.CodeName, typeName: genspec.CodeName, propSpecs: { [name: string]: schema.Property }, - nameConversionTable: Dictionary) { + nameConversionTable: Dictionary): void { const validatorName = genspec.validatorName(typeName); this.code.line('/**'); @@ -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) { @@ -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) { @@ -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); @@ -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())); @@ -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; } diff --git a/tools/cfn2ts/lib/genspec.ts b/tools/cfn2ts/lib/genspec.ts index 463fd1f8f3eb1..849d6d9fe4050 100644 --- a/tools/cfn2ts/lib/genspec.ts +++ b/tools/cfn2ts/lib/genspec.ts @@ -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; } @@ -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)); } @@ -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'; } diff --git a/tools/cfn2ts/lib/index.ts b/tools/cfn2ts/lib/index.ts index 19564a7352da9..f21821c450b1f 100644 --- a/tools/cfn2ts/lib/index.ts +++ b/tools/cfn2ts/lib/index.ts @@ -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 { if (outPath !== '.') { await fs.mkdirp(outPath); } if (typeof scopes === 'string') { scopes = [scopes]; } diff --git a/tools/cfn2ts/lib/spec-utils.ts b/tools/cfn2ts/lib/spec-utils.ts index aa9c3c0e3aab7..42453374bf464 100644 --- a/tools/cfn2ts/lib/spec-utils.ts +++ b/tools/cfn2ts/lib/spec-utils.ts @@ -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; } @@ -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); } }