Skip to content

Commit 813bece

Browse files
author
Kamil Sobol
authored
test: handle var args in api check (#943)
* test: handle var args in api check * ehh * add that
1 parent 48ca0e7 commit 813bece

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

scripts/components/api-changes-validator/api_usage_generator.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ export const someFunction1: () => void;
8181
export const someFunction2: (param1: string, param2?: number) => string;
8282
export const someFunction3: (param1: string, param2: number = 1) => string;
8383
export const someFunction4: <T1, T2, T3>(param1: T1, param2?: T2) => Promise<T3>;
84+
export const someFunction5: (param1: string, ...param2: number) => string;
8485
`,
8586
expectedApiUsage: `
8687
import { someFunction1 } from 'samplePackageName';
8788
import { someFunction2 } from 'samplePackageName';
8889
import { someFunction3 } from 'samplePackageName';
8990
import { someFunction4 } from 'samplePackageName';
91+
import { someFunction5 } from 'samplePackageName';
9092
9193
const someFunction1UsageFunction = () => {
9294
someFunction1();
@@ -107,6 +109,11 @@ const someFunction4UsageFunction = <T1, T2, T3>(param1: T1, param2?: T2) => {
107109
const returnValue: Promise<T3> = someFunction4(param1);
108110
someFunction4(param1, param2);
109111
}
112+
113+
const someFunction5UsageFunction = (param1: string, ...param2: number) => {
114+
const returnValue: string = someFunction5(param1, ...param2);
115+
someFunction5(param1, ...param2);
116+
}
110117
`,
111118
},
112119
{
@@ -135,6 +142,9 @@ export class SomeClass7<T1 extends SomeClass1, T2, T3, T4, T5, T6> {
135142
someMethod: (param1: T3, param2?: T4) => T5;
136143
someProperty: T6;
137144
}
145+
export class SomeClass8 {
146+
someMethod: (param1: string, ...param2: string) => string;
147+
}
138148
export abstract class SomeAbstractClass1 {
139149
constructor(param1: string, param2?: string);
140150
someMethod: (param1: string, param2?: string) => string;
@@ -154,6 +164,7 @@ import { SomeClass4 } from 'samplePackageName';
154164
import { SomeClass5 } from 'samplePackageName';
155165
import { SomeClass6 } from 'samplePackageName';
156166
import { SomeClass7 } from 'samplePackageName';
167+
import { SomeClass8 } from 'samplePackageName';
157168
import { SomeAbstractClass1 } from 'samplePackageName';
158169
import { SomeAbstractClass2 } from 'samplePackageName';
159170
@@ -207,6 +218,14 @@ const someClass7SomePropertyUsageOuterFunction = <T1 extends SomeClass1, T2, T3,
207218
}
208219
209220
221+
const someClass8SomeMethodUsageOuterFunction = (someClass8SomeMethodUsageOuterFunctionParameter: SomeClass8) => {
222+
const SomeClass8SomeMethodUsageInnerFunction = (param1: string, ...param2: string) => {
223+
const returnValue: string = someClass8SomeMethodUsageOuterFunctionParameter.someMethod(param1, ...param2);
224+
someClass8SomeMethodUsageOuterFunctionParameter.someMethod(param1, ...param2);
225+
}
226+
}
227+
228+
210229
class SomeAbstractClass1DerivedUsageClass extends SomeAbstractClass1{
211230
constructor(param1: string, param2?: string) {
212231
super(param1, param2);

scripts/components/api-changes-validator/api_usage_statements_generators.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,13 @@ export class CallableParameterUsageStatementsGenerator
628628
return true;
629629
}
630630
})
631-
.map((parameter) => parameter.name.getText())
631+
.map((parameter) => {
632+
if (parameter.dotDotDotToken) {
633+
// expand var arg
634+
return `...${parameter.name.getText()}`;
635+
}
636+
return parameter.name.getText();
637+
})
632638
.join(', ');
633639
return {
634640
usageStatement,

scripts/components/api-changes-validator/test-resources/test-projects/without-breaks/project-without-breaks/API.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ export class SomeDerivedClass2<T1, T2 extends SampleType, T3, T4, T5, T6>
5959
someProperty: T1;
6060
}
6161

62+
export class SomeClassWithTemplateMethodAndVarArg {
63+
someTemplateMethodWithVarArg: <T extends Record<string | number, string>>(...objects: T[]) => void;
64+
}
65+
6266
export const someFunction1: () => void;
6367
export const someFunction2: (param1: string, param2?: number) => string;
6468
export const someFunction3: (param1: string, param2: number = 1) => string;
6569
export const someFunction4: <T1, T2, T3>(
6670
param1: T1,
6771
param2?: T2
6872
) => Promise<T3>;
73+
export const someFunction5: (param1: string, ...param2: number[]) => string;
6974

7075
export type SampleTypeUsingClass = {
7176
sampleMethod: (param: SampleClass1) => void;

scripts/components/api-changes-validator/test-resources/test-projects/without-breaks/project-without-breaks/src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ export class SomeDerivedClass2<T1, T2 extends SampleType, T3, T4, T5, T6>
7676
someProperty: T1;
7777
}
7878

79+
export class SomeClassWithTemplateMethodAndVarArg {
80+
someTemplateMethodWithVarArg = <T extends Record<string | number, string>>(
81+
...objects: T[]
82+
): void => {
83+
throw new Error();
84+
};
85+
}
86+
7987
export const someFunction1 = (): void => {
8088
throw new Error();
8189
};
@@ -91,6 +99,9 @@ export const someFunction4 = <T1, T2, T3>(
9199
): Promise<T3> => {
92100
throw new Error();
93101
};
102+
export const someFunction5 = (param1: string, ...param2: number[]): string => {
103+
throw new Error();
104+
};
94105

95106
export type SampleTypeUsingClass = {
96107
sampleMethod: (param: SampleClass1) => void;

0 commit comments

Comments
 (0)