From bafb0d0ce466c6d80b7b0691e22400045738f3b8 Mon Sep 17 00:00:00 2001 From: Haowen Chen Date: Mon, 20 Feb 2023 17:38:34 +0800 Subject: [PATCH] Bump to 0.9.0 & lint fix (#113) --- package-lock.json | 4 ++-- package.json | 2 +- src/cli/generate.ts | 17 ++++++++-------- src/cli/index.ts | 33 +++++++++++++++++++----------- src/configuration.ts | 2 +- src/generator/CodeGenerator.ts | 37 +++++++++++++++++++++------------- src/generator/named-types.ts | 2 +- src/parser/Parser.ts | 22 +++++++++++++++----- src/parser/ValueParser.ts | 7 ++++++- 9 files changed, 80 insertions(+), 46 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5d41a77..1ff9595 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ts-gyb", - "version": "0.8.0", + "version": "0.9.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "ts-gyb", - "version": "0.8.0", + "version": "0.9.0", "license": "MIT", "dependencies": { "chalk": "^4.1.1", diff --git a/package.json b/package.json index 919a51b..79a3612 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-gyb", - "version": "0.8.0", + "version": "0.9.0", "description": "Generate Native API based on TS interface", "repository": { "type": "git", diff --git a/src/cli/generate.ts b/src/cli/generate.ts index e14be8a..9f7c3eb 100644 --- a/src/cli/generate.ts +++ b/src/cli/generate.ts @@ -11,15 +11,14 @@ export function generateWithConfig(config: Configuration): void { ); const namedTargets = Object.fromEntries( - Object.entries(config.parsing.targets) - .map(([target, targetConfig]) => ([ - target, - generator.parseTarget( - targetConfig.source, - targetConfig.exportedInterfaceBases !== undefined ? new Set(targetConfig.exportedInterfaceBases) : undefined, - targetConfig.tsconfigPath - ) - ])) + Object.entries(config.parsing.targets).map(([target, targetConfig]) => [ + target, + generator.parseTarget( + targetConfig.source, + targetConfig.exportedInterfaceBases !== undefined ? new Set(targetConfig.exportedInterfaceBases) : undefined, + targetConfig.tsconfigPath + ), + ]) ); let sharedTypes = generator.extractTargetsSharedTypes(Object.values(namedTargets)); diff --git a/src/cli/index.ts b/src/cli/index.ts index 1e02e9d..ea9cf05 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -11,11 +11,16 @@ function run(): void { program .scriptName('ts-gyb') .command(['gen', '*'], 'generate code from a configuration file', () => {}, generate) - .command('list-output', 'list all output files', (subprogram) => { - subprogram - .option('language', { description: 'language of the output files to list', choices: ['swift', 'kotlin'] }) - .option('expand', { description: 'expand directories' }); - }, listOutput) + .command( + 'list-output', + 'list all output files', + (subprogram) => { + subprogram + .option('language', { description: 'language of the output files to list', choices: ['swift', 'kotlin'] }) + .option('expand', { description: 'expand directories' }); + }, + listOutput + ) .option('config', { describe: 'path to the configuration file', type: 'string', @@ -44,18 +49,22 @@ function listOutput(args: { config: string; language?: 'swift' | 'kotlin'; expan } files = renderingConfig.renders.map((render) => render.outputPath); } else { - files = Object.values(config.rendering).flatMap((renderingConfig: RenderConfiguration) => renderingConfig.renders.map((render) => render.outputPath)); + files = Object.values(config.rendering).flatMap((renderingConfig: RenderConfiguration) => + renderingConfig.renders.map((render) => render.outputPath) + ); } files = files.map((file) => path.resolve(file)); if (args.expand) { - files = files.map((filePath) => { - if (!fs.lstatSync(filePath).isDirectory()) { - return filePath; - } + files = files + .map((filePath) => { + if (!fs.lstatSync(filePath).isDirectory()) { + return filePath; + } - return fs.readdirSync(filePath).map((file) => path.join(filePath, file)); - }).flat(); + return fs.readdirSync(filePath).map((file) => path.join(filePath, file)); + }) + .flat(); } files = [...new Set(files)]; diff --git a/src/configuration.ts b/src/configuration.ts index bf272eb..907dbbb 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -76,7 +76,7 @@ export interface RenderConfiguration { * A list of render configurations. */ renders: TargetRenderConfiguration[]; - + /** * Template path for named types. Must be a mustache template. * If it is a relative path, it will be resolved based on the configuration file path. diff --git a/src/generator/CodeGenerator.ts b/src/generator/CodeGenerator.ts index c3cbf51..0e8c937 100644 --- a/src/generator/CodeGenerator.ts +++ b/src/generator/CodeGenerator.ts @@ -1,6 +1,13 @@ import fs from 'fs'; import path from 'path'; -import { dropIPrefixInCustomTypes, extractTargetsSharedTypes, NamedTypeInfo, ParsedModule, ParsedTarget, parseTarget } from './named-types'; +import { + dropIPrefixInCustomTypes, + extractTargetsSharedTypes, + NamedTypeInfo, + ParsedModule, + ParsedTarget, + parseTarget, +} from './named-types'; import { Parser } from '../parser/Parser'; import { renderCode } from '../renderer/renderer'; import { NamedTypeView, ModuleView, InterfaceTypeView, EnumTypeView } from '../renderer/views'; @@ -26,11 +33,17 @@ export class CodeGenerator { private readonly predefinedTypes: Set, private readonly defaultCustomTags: Record, private readonly skipInvalidMethods: boolean, - private readonly dropInterfaceIPrefix: boolean, + private readonly dropInterfaceIPrefix: boolean ) {} parseTarget(interfacePaths: string[], exportedInterfaceBases?: Set, tsconfigPath?: string): ParsedTarget { - const parser = new Parser(interfacePaths, this.predefinedTypes, this.skipInvalidMethods, exportedInterfaceBases, tsconfigPath); + const parser = new Parser( + interfacePaths, + this.predefinedTypes, + this.skipInvalidMethods, + exportedInterfaceBases, + tsconfigPath + ); const modules = parser.parse(); modules.forEach((module) => applyDefaultCustomTags(module, this.defaultCustomTags)); @@ -69,16 +82,17 @@ export class CodeGenerator { renderModules(modules: ParsedModule[], options: RenderOptions): void { const valueTransformer = this.getValueTransformer(options.language, options.typeNameMap); - const moduleViews = modules.map((module) => - this.getModuleView(module, valueTransformer) - ); + const moduleViews = modules.map((module) => this.getModuleView(module, valueTransformer)); if (path.extname(options.outputPath) === '') { // The path is a directory moduleViews.forEach((moduleView) => { const renderedCode = renderCode(options.templatePath, moduleView); - this.writeFile(renderedCode, path.join(options.outputPath, `${moduleView.moduleName}${this.getFileExtension(options.language)}`)); + this.writeFile( + renderedCode, + path.join(options.outputPath, `${moduleView.moduleName}${this.getFileExtension(options.language)}`) + ); }); } else { moduleViews.forEach((moduleView, index) => { @@ -93,9 +107,7 @@ export class CodeGenerator { renderNamedTypes(sharedTypes: NamedTypeInfo[], options: RenderOptions): void { const valueTransformer = this.getValueTransformer(options.language, options.typeNameMap); - const namedTypesView = sharedTypes.map((namedType) => - this.getNamedTypeView(namedType, valueTransformer) - ); + const namedTypesView = sharedTypes.map((namedType) => this.getNamedTypeView(namedType, valueTransformer)); const renderedCode = renderCode(options.templatePath, namedTypesView); this.writeFile(renderedCode, options.outputPath); } @@ -124,10 +136,7 @@ export class CodeGenerator { return namedTypeView; } - private getModuleView( - module: ParsedModule, - valueTransformer: ValueTransformer - ): ModuleView { + private getModuleView(module: ParsedModule, valueTransformer: ValueTransformer): ModuleView { return new ModuleView( module, module.associatedTypes.map((associatedType) => this.getNamedTypeView(associatedType, valueTransformer)), diff --git a/src/generator/named-types.ts b/src/generator/named-types.ts index 8b310f4..61b8ced 100644 --- a/src/generator/named-types.ts +++ b/src/generator/named-types.ts @@ -79,7 +79,7 @@ export function extractTargetsSharedTypes(targets: ParsedTarget[]): NamedTypeInf const sharedTypes = Object.entries(typeTargetsMap) .filter(([, [, targetSet]]) => targetSet.size > 1) - .map(([, [namedType,]]) => namedType); + .map(([, [namedType]]) => namedType); const sharedTypeNames = new Set(sharedTypes.map(({ type }) => type.name)); diff --git a/src/parser/Parser.ts b/src/parser/Parser.ts index 164d2df..3efa17c 100644 --- a/src/parser/Parser.ts +++ b/src/parser/Parser.ts @@ -13,7 +13,13 @@ export class Parser { private valueParser: ValueParser; - constructor(globPatterns: string[], predefinedTypes: Set, skipInvalidMethods = false, private readonly exportedInterfaceBases: Set | undefined, tsconfigPath: string | undefined) { + constructor( + globPatterns: string[], + predefinedTypes: Set, + skipInvalidMethods = false, + private readonly exportedInterfaceBases: Set | undefined, + tsconfigPath: string | undefined + ) { const filePaths = globPatterns.flatMap((pattern) => glob.sync(pattern)); if (tsconfigPath !== undefined) { @@ -33,9 +39,14 @@ export class Parser { options: {}, }); } - + this.checker = this.program.getTypeChecker(); - this.valueParser = new ValueParser(this.checker, predefinedTypes, new ParserLogger(this.checker), skipInvalidMethods); + this.valueParser = new ValueParser( + this.checker, + predefinedTypes, + new ParserLogger(this.checker), + skipInvalidMethods + ); } parse(): Module[] { @@ -67,12 +78,13 @@ export class Parser { throw Error('Invalid module node'); } - const exportedInterfaceBases = node.heritageClauses?.flatMap((heritageClause) => heritageClause.types.map((type) => type.getText())) ?? []; + const exportedInterfaceBases = + node.heritageClauses?.flatMap((heritageClause) => heritageClause.types.map((type) => type.getText())) ?? []; const jsDocTagsResult = parseTypeJSDocTags(symbol); if (this.exportedInterfaceBases !== undefined) { - if (!(exportedInterfaceBases.some((extendedInterface) => this.exportedInterfaceBases?.has(extendedInterface)))) { + if (!exportedInterfaceBases.some((extendedInterface) => this.exportedInterfaceBases?.has(extendedInterface))) { return null; } } else if (!jsDocTagsResult.shouldExport) { diff --git a/src/parser/ValueParser.ts b/src/parser/ValueParser.ts index 06ff90c..682c325 100644 --- a/src/parser/ValueParser.ts +++ b/src/parser/ValueParser.ts @@ -27,7 +27,12 @@ import { ParserLogger } from '../logger/ParserLogger'; import { ValueParserError } from './ValueParserError'; export class ValueParser { - constructor(private readonly checker: ts.TypeChecker, private readonly predefinedTypes: Set, private readonly logger: ParserLogger, private readonly skipInvalidMethods: boolean) {} + constructor( + private readonly checker: ts.TypeChecker, + private readonly predefinedTypes: Set, + private readonly logger: ParserLogger, + private readonly skipInvalidMethods: boolean + ) {} parseFunctionReturnType(methodSignature: ts.SignatureDeclarationBase): [ValueType | null, boolean] { if (methodSignature.type === undefined) {