Skip to content

Commit

Permalink
fix(generator): fix optional function parameters processing
Browse files Browse the repository at this point in the history
  • Loading branch information
tsofist committed Dec 18, 2024
1 parent 5f0d502 commit 2bd7c02
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,38 @@ describe('generator for a3', () => {
expect(props.propWithGeneric).toBeTruthy();
expect(props.propWithGeneric.$ref).toStrictEqual('#/definitions/NonEmptyString');
});
it('optional args in API methods should works', () => {
{
const props = forgeSchemaResult!.schema.definitions?.API_methodG0_Args;
expect(props).toBeTruthy();
expect(props.minItems).toStrictEqual(0);
expect(props.maxItems).toStrictEqual(1);
}
{
const props = forgeSchemaResult!.schema.definitions?.API_methodG1_Args;
expect(props).toBeTruthy();
expect(props.minItems).toStrictEqual(0);
expect(props.maxItems).toStrictEqual(2);
}
{
const props = forgeSchemaResult!.schema.definitions?.API_methodG2_Args;
expect(props).toBeTruthy();
expect(props.minItems).toStrictEqual(1);
expect(props.maxItems).toStrictEqual(2);
}
// {
// const props = forgeSchemaResult!.schema.definitions?.API_methodG3_Args;
// expect(props).toBeTruthy();
// expect(props.minItems).toStrictEqual(0);
// expect(props.maxItems).toBeUndefined();
// }
// {
// const props = forgeSchemaResult!.schema.definitions?.API_methodG4_Args;
// expect(props).toBeTruthy();
// expect(props.minItems).toStrictEqual(1);
// expect(props.maxItems).toBeUndefined();
// }
});

it('extends should works', () => {
const props = forgeSchemaResult!.schema.definitions?.BAPI_InterfaceDeclaration.properties;
Expand All @@ -336,6 +368,9 @@ describe('generator for a3', () => {
'methodE1',
'methodE2',
'methodF',
'methodG0',
'methodG1',
'methodG2',
'methodY',
]);
});
Expand Down
16 changes: 14 additions & 2 deletions src/generator/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
isPropertySignature,
isStringLiteral,
isTypeAliasDeclaration,
NodeArray,
ParameterDeclaration,
Program,
SignatureDeclarationBase,
SourceFile,
Expand Down Expand Up @@ -281,7 +283,7 @@ function processAPIInterfaceDeclaration(
memberDescription: string | undefined,
deprecated: string | undefined,
) {
const minArgsNum = method.parameters.filter((param) => !param.questionToken).length;
const minArgsNum = countRequiredParams(method.parameters);
const maxArgsNum = method.parameters.length;
const argsNames: string[] = [];
const argsTypesText = method.parameters
Expand Down Expand Up @@ -332,7 +334,7 @@ function processAPIInterfaceDeclaration(
` * @description Arguments for ${comment} ${argsNames.length ? `(${argsNamesText})` : ''}`,
` * @comment ${comment}`,
` *`,
minArgsNum > 0 ? ` * @minItems ${minArgsNum}` : ` *`,
` * @minItems ${minArgsNum}`,
` * @maxItems ${maxArgsNum}`,
` */`,
`export type ${definitionNameArgs} = readonly [\n${argsTypesText}\n];`,
Expand Down Expand Up @@ -438,3 +440,13 @@ function processAPIInterfaceDeclaration(
context.fileContent.push(interfaceText.stringify('\n'));
}
}

function countRequiredParams(params: NodeArray<ParameterDeclaration>) {
let result = 0;
for (const param of params) {
if (param.questionToken != null) break;
if (param.dotDotDotToken != null) raise('Rest arguments are not supported yet');
result++;
}
return result;
}
18 changes: 18 additions & 0 deletions test-sources/a3/service-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export interface API {
methodE1(): Promise<undefined>;
methodE2(): Promise<null>;
methodF(): Promise<null | number>;
methodG0(arg0?: boolean): Promise<never>;
methodG1(arg0?: boolean, arg1?: boolean): Promise<never>;
methodG2(arg0: boolean, arg1?: boolean): Promise<never>;
// todo supports for rest args
// methodG3(...args: boolean[]): Promise<never>;
// methodG4(arg0: string, ...args: boolean[]): Promise<never>;
// methodG5( todo array with jsdoc
}

/**
Expand All @@ -36,8 +43,19 @@ export interface API {
*/
export interface BAPI extends API {
methodY(arg1: string): Promise<void>;
// methodG(arg0: boolean): Promise<never>; // todo supports for method override
}

// todo supports for extends with omitted methods
// /**
// * @public
// * @api
// * @description BAPI descriptions
// */
// export interface BAPIOmit extends Omit<API, 'methodG0' | 'methodG1' | 'methodG2'> {
// methodY(arg1: string): Promise<void>;
// }

interface IndoorInterface {
a1: AdditionalType;
a2: AdditionalType2;
Expand Down

0 comments on commit 2bd7c02

Please sign in to comment.