Skip to content

Commit bafb0d0

Browse files
authored
Bump to 0.9.0 & lint fix (#113)
1 parent 3d92991 commit bafb0d0

File tree

9 files changed

+80
-46
lines changed

9 files changed

+80
-46
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-gyb",
3-
"version": "0.8.0",
3+
"version": "0.9.0",
44
"description": "Generate Native API based on TS interface",
55
"repository": {
66
"type": "git",

src/cli/generate.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ export function generateWithConfig(config: Configuration): void {
1111
);
1212

1313
const namedTargets = Object.fromEntries(
14-
Object.entries(config.parsing.targets)
15-
.map(([target, targetConfig]) => ([
16-
target,
17-
generator.parseTarget(
18-
targetConfig.source,
19-
targetConfig.exportedInterfaceBases !== undefined ? new Set(targetConfig.exportedInterfaceBases) : undefined,
20-
targetConfig.tsconfigPath
21-
)
22-
]))
14+
Object.entries(config.parsing.targets).map(([target, targetConfig]) => [
15+
target,
16+
generator.parseTarget(
17+
targetConfig.source,
18+
targetConfig.exportedInterfaceBases !== undefined ? new Set(targetConfig.exportedInterfaceBases) : undefined,
19+
targetConfig.tsconfigPath
20+
),
21+
])
2322
);
2423

2524
let sharedTypes = generator.extractTargetsSharedTypes(Object.values(namedTargets));

src/cli/index.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ function run(): void {
1111
program
1212
.scriptName('ts-gyb')
1313
.command(['gen', '*'], 'generate code from a configuration file', () => {}, generate)
14-
.command('list-output', 'list all output files', (subprogram) => {
15-
subprogram
16-
.option('language', { description: 'language of the output files to list', choices: ['swift', 'kotlin'] })
17-
.option('expand', { description: 'expand directories' });
18-
}, listOutput)
14+
.command(
15+
'list-output',
16+
'list all output files',
17+
(subprogram) => {
18+
subprogram
19+
.option('language', { description: 'language of the output files to list', choices: ['swift', 'kotlin'] })
20+
.option('expand', { description: 'expand directories' });
21+
},
22+
listOutput
23+
)
1924
.option('config', {
2025
describe: 'path to the configuration file',
2126
type: 'string',
@@ -44,18 +49,22 @@ function listOutput(args: { config: string; language?: 'swift' | 'kotlin'; expan
4449
}
4550
files = renderingConfig.renders.map((render) => render.outputPath);
4651
} else {
47-
files = Object.values(config.rendering).flatMap((renderingConfig: RenderConfiguration) => renderingConfig.renders.map((render) => render.outputPath));
52+
files = Object.values(config.rendering).flatMap((renderingConfig: RenderConfiguration) =>
53+
renderingConfig.renders.map((render) => render.outputPath)
54+
);
4855
}
4956

5057
files = files.map((file) => path.resolve(file));
5158
if (args.expand) {
52-
files = files.map((filePath) => {
53-
if (!fs.lstatSync(filePath).isDirectory()) {
54-
return filePath;
55-
}
59+
files = files
60+
.map((filePath) => {
61+
if (!fs.lstatSync(filePath).isDirectory()) {
62+
return filePath;
63+
}
5664

57-
return fs.readdirSync(filePath).map((file) => path.join(filePath, file));
58-
}).flat();
65+
return fs.readdirSync(filePath).map((file) => path.join(filePath, file));
66+
})
67+
.flat();
5968
}
6069

6170
files = [...new Set(files)];

src/configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export interface RenderConfiguration {
7676
* A list of render configurations.
7777
*/
7878
renders: TargetRenderConfiguration[];
79-
79+
8080
/**
8181
* Template path for named types. Must be a mustache template.
8282
* If it is a relative path, it will be resolved based on the configuration file path.

src/generator/CodeGenerator.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import fs from 'fs';
22
import path from 'path';
3-
import { dropIPrefixInCustomTypes, extractTargetsSharedTypes, NamedTypeInfo, ParsedModule, ParsedTarget, parseTarget } from './named-types';
3+
import {
4+
dropIPrefixInCustomTypes,
5+
extractTargetsSharedTypes,
6+
NamedTypeInfo,
7+
ParsedModule,
8+
ParsedTarget,
9+
parseTarget,
10+
} from './named-types';
411
import { Parser } from '../parser/Parser';
512
import { renderCode } from '../renderer/renderer';
613
import { NamedTypeView, ModuleView, InterfaceTypeView, EnumTypeView } from '../renderer/views';
@@ -26,11 +33,17 @@ export class CodeGenerator {
2633
private readonly predefinedTypes: Set<string>,
2734
private readonly defaultCustomTags: Record<string, unknown>,
2835
private readonly skipInvalidMethods: boolean,
29-
private readonly dropInterfaceIPrefix: boolean,
36+
private readonly dropInterfaceIPrefix: boolean
3037
) {}
3138

3239
parseTarget(interfacePaths: string[], exportedInterfaceBases?: Set<string>, tsconfigPath?: string): ParsedTarget {
33-
const parser = new Parser(interfacePaths, this.predefinedTypes, this.skipInvalidMethods, exportedInterfaceBases, tsconfigPath);
40+
const parser = new Parser(
41+
interfacePaths,
42+
this.predefinedTypes,
43+
this.skipInvalidMethods,
44+
exportedInterfaceBases,
45+
tsconfigPath
46+
);
3447
const modules = parser.parse();
3548

3649
modules.forEach((module) => applyDefaultCustomTags(module, this.defaultCustomTags));
@@ -69,16 +82,17 @@ export class CodeGenerator {
6982
renderModules(modules: ParsedModule[], options: RenderOptions): void {
7083
const valueTransformer = this.getValueTransformer(options.language, options.typeNameMap);
7184

72-
const moduleViews = modules.map((module) =>
73-
this.getModuleView(module, valueTransformer)
74-
);
85+
const moduleViews = modules.map((module) => this.getModuleView(module, valueTransformer));
7586

7687
if (path.extname(options.outputPath) === '') {
7788
// The path is a directory
7889
moduleViews.forEach((moduleView) => {
7990
const renderedCode = renderCode(options.templatePath, moduleView);
8091

81-
this.writeFile(renderedCode, path.join(options.outputPath, `${moduleView.moduleName}${this.getFileExtension(options.language)}`));
92+
this.writeFile(
93+
renderedCode,
94+
path.join(options.outputPath, `${moduleView.moduleName}${this.getFileExtension(options.language)}`)
95+
);
8296
});
8397
} else {
8498
moduleViews.forEach((moduleView, index) => {
@@ -93,9 +107,7 @@ export class CodeGenerator {
93107
renderNamedTypes(sharedTypes: NamedTypeInfo[], options: RenderOptions): void {
94108
const valueTransformer = this.getValueTransformer(options.language, options.typeNameMap);
95109

96-
const namedTypesView = sharedTypes.map((namedType) =>
97-
this.getNamedTypeView(namedType, valueTransformer)
98-
);
110+
const namedTypesView = sharedTypes.map((namedType) => this.getNamedTypeView(namedType, valueTransformer));
99111
const renderedCode = renderCode(options.templatePath, namedTypesView);
100112
this.writeFile(renderedCode, options.outputPath);
101113
}
@@ -124,10 +136,7 @@ export class CodeGenerator {
124136
return namedTypeView;
125137
}
126138

127-
private getModuleView(
128-
module: ParsedModule,
129-
valueTransformer: ValueTransformer
130-
): ModuleView {
139+
private getModuleView(module: ParsedModule, valueTransformer: ValueTransformer): ModuleView {
131140
return new ModuleView(
132141
module,
133142
module.associatedTypes.map((associatedType) => this.getNamedTypeView(associatedType, valueTransformer)),

src/generator/named-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function extractTargetsSharedTypes(targets: ParsedTarget[]): NamedTypeInf
7979

8080
const sharedTypes = Object.entries(typeTargetsMap)
8181
.filter(([, [, targetSet]]) => targetSet.size > 1)
82-
.map(([, [namedType,]]) => namedType);
82+
.map(([, [namedType]]) => namedType);
8383

8484
const sharedTypeNames = new Set(sharedTypes.map(({ type }) => type.name));
8585

src/parser/Parser.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ export class Parser {
1313

1414
private valueParser: ValueParser;
1515

16-
constructor(globPatterns: string[], predefinedTypes: Set<string>, skipInvalidMethods = false, private readonly exportedInterfaceBases: Set<string> | undefined, tsconfigPath: string | undefined) {
16+
constructor(
17+
globPatterns: string[],
18+
predefinedTypes: Set<string>,
19+
skipInvalidMethods = false,
20+
private readonly exportedInterfaceBases: Set<string> | undefined,
21+
tsconfigPath: string | undefined
22+
) {
1723
const filePaths = globPatterns.flatMap((pattern) => glob.sync(pattern));
1824

1925
if (tsconfigPath !== undefined) {
@@ -33,9 +39,14 @@ export class Parser {
3339
options: {},
3440
});
3541
}
36-
42+
3743
this.checker = this.program.getTypeChecker();
38-
this.valueParser = new ValueParser(this.checker, predefinedTypes, new ParserLogger(this.checker), skipInvalidMethods);
44+
this.valueParser = new ValueParser(
45+
this.checker,
46+
predefinedTypes,
47+
new ParserLogger(this.checker),
48+
skipInvalidMethods
49+
);
3950
}
4051

4152
parse(): Module[] {
@@ -67,12 +78,13 @@ export class Parser {
6778
throw Error('Invalid module node');
6879
}
6980

70-
const exportedInterfaceBases = node.heritageClauses?.flatMap((heritageClause) => heritageClause.types.map((type) => type.getText())) ?? [];
81+
const exportedInterfaceBases =
82+
node.heritageClauses?.flatMap((heritageClause) => heritageClause.types.map((type) => type.getText())) ?? [];
7183

7284
const jsDocTagsResult = parseTypeJSDocTags(symbol);
7385

7486
if (this.exportedInterfaceBases !== undefined) {
75-
if (!(exportedInterfaceBases.some((extendedInterface) => this.exportedInterfaceBases?.has(extendedInterface)))) {
87+
if (!exportedInterfaceBases.some((extendedInterface) => this.exportedInterfaceBases?.has(extendedInterface))) {
7688
return null;
7789
}
7890
} else if (!jsDocTagsResult.shouldExport) {

src/parser/ValueParser.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ import { ParserLogger } from '../logger/ParserLogger';
2727
import { ValueParserError } from './ValueParserError';
2828

2929
export class ValueParser {
30-
constructor(private readonly checker: ts.TypeChecker, private readonly predefinedTypes: Set<string>, private readonly logger: ParserLogger, private readonly skipInvalidMethods: boolean) {}
30+
constructor(
31+
private readonly checker: ts.TypeChecker,
32+
private readonly predefinedTypes: Set<string>,
33+
private readonly logger: ParserLogger,
34+
private readonly skipInvalidMethods: boolean
35+
) {}
3136

3237
parseFunctionReturnType(methodSignature: ts.SignatureDeclarationBase): [ValueType | null, boolean] {
3338
if (methodSignature.type === undefined) {

0 commit comments

Comments
 (0)