Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fast_check): correctly handle rest parameters #435

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions src/fast_check/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,18 +734,38 @@ impl<'a> FastCheckTransformer<'a> {
let is_overload = self.public_ranges.is_impl_with_overloads(&n.range());
if is_overload {
for (i, param) in n.params.iter_mut().enumerate() {
*param = ParamOrTsParamProp::Param(Param {
span: DUMMY_SP,
decorators: Vec::new(),
pat: Pat::Ident(BindingIdent {
id: Ident {
if param.as_param().map(|p| p.pat.is_rest()).unwrap_or(false) {
*param = ParamOrTsParamProp::Param(Param {
span: DUMMY_SP,
decorators: Vec::new(),
pat: Pat::Rest(RestPat {
span: DUMMY_SP,
sym: format!("param{}", i).into(),
optional: true,
},
type_ann: Some(any_type_ann()),
}),
});
dot3_token: DUMMY_SP,
type_ann: Some(any_type_ann()),
arg: Box::new(Pat::Ident(BindingIdent {
id: Ident {
span: DUMMY_SP,
sym: format!("param{}", i).into(),
optional: false,
},
type_ann: None,
})),
}),
});
} else {
*param = ParamOrTsParamProp::Param(Param {
span: DUMMY_SP,
decorators: Vec::new(),
pat: Pat::Ident(BindingIdent {
id: Ident {
span: DUMMY_SP,
sym: format!("param{}", i).into(),
optional: true,
},
type_ann: Some(any_type_ann()),
}),
});
}
}
}

Expand Down Expand Up @@ -925,7 +945,7 @@ impl<'a> FastCheckTransformer<'a> {
id: Ident {
span: DUMMY_SP,
sym: format!("param{}", i).into(),
optional: true,
optional: false,
},
type_ann: None,
})),
Expand Down
41 changes: 25 additions & 16 deletions tests/specs/graph/jsr/FastCheck_Overloads.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@

# https://jsr.io/@scope/a/1.0.0/mod.ts
export class argsToArray {
constructor(..._args: string[]);
constructor(..._args: number[]);
constructor(..._args: unknown[]) {}

argsToArray(..._args: string[]): void;
argsToArray(..._args: number[]): void;
argsToArray(..._args: unknown[]): void {}
}

export function overloadSpread(...args: string[]);
export function overloadSpread(...args: number[]);
export function overloadSpread(...args: string[]): void;
export function overloadSpread(...args: number[]): void;
export function overloadSpread(...args: unknown[]) {
return args;
}

export function overloadLast(value: string, ...args: string[]);
export function overloadLast(value: number, ...args: number[]);
export function overloadLast(value: string, ...args: string[]): void;
export function overloadLast(value: number, ...args: number[]): void;
export function overloadLast(value: string | number, ...args: unknown[]) {
return args;
}
Expand Down Expand Up @@ -58,7 +62,7 @@ import 'jsr:@scope/a'
},
{
"kind": "esm",
"size": 549,
"size": 682,
"mediaType": "TypeScript",
"specifier": "https://jsr.io/@scope/a/1.0.0/mod.ts"
}
Expand All @@ -74,28 +78,33 @@ import 'jsr:@scope/a'
Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
{}
export class argsToArray {
constructor(..._args: string[]);
constructor(..._args: number[]);
constructor(...param0: any){}
argsToArray(..._args: string[]): void;
argsToArray(..._args: number[]): void;
argsToArray(...param0?: any): any {
argsToArray(...param0: any): any {
return {} as never;
}
}
export function overloadSpread(...args: string[]);
export function overloadSpread(...args: number[]);
export function overloadSpread(...param0?: any): any {
export function overloadSpread(...args: string[]): void;
export function overloadSpread(...args: number[]): void;
export function overloadSpread(...param0: any): any {
return {} as never;
}
export function overloadLast(value: string, ...args: string[]);
export function overloadLast(value: number, ...args: number[]);
export function overloadLast(param0?: any, ...param1?: any): any {
export function overloadLast(value: string, ...args: string[]): void;
export function overloadLast(value: number, ...args: number[]): void;
export function overloadLast(param0?: any, ...param1: any): any {
return {} as never;
}
--- DTS ---
export declare class argsToArray {
constructor(..._args: string[]);
constructor(..._args: number[]);
argsToArray(..._args: string[]): void;
argsToArray(..._args: number[]): void;
}
export declare function overloadSpread(...args: string[]);
export declare function overloadSpread(...args: number[]);
export declare function overloadLast(value: string, ...args: string[]);
export declare function overloadLast(value: number, ...args: number[]);
export declare function overloadSpread(...args: string[]): void;
export declare function overloadSpread(...args: number[]): void;
export declare function overloadLast(value: string, ...args: string[]): void;
export declare function overloadLast(value: number, ...args: number[]): void;