Skip to content

Commit

Permalink
fix(fast_check): properly handle rest parameters in overloads (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored Apr 11, 2024
1 parent 88a1147 commit 523d1e2
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/fast_check/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,18 +911,38 @@ impl<'a> FastCheckTransformer<'a> {
}
if is_overload {
for (i, param) in n.params.iter_mut().enumerate() {
*param = Param {
span: DUMMY_SP,
decorators: Vec::new(),
pat: Pat::Ident(BindingIdent {
id: Ident {
if param.pat.is_rest() {
*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: true,
},
type_ann: None,
})),
}),
};
} else {
*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()),
}),
};
}
}
n.return_type = Some(any_type_ann());
}
Expand Down
101 changes: 101 additions & 0 deletions tests/specs/graph/jsr/FastCheck_Overloads.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# https://jsr.io/@scope/a/meta.json
{"versions": { "1.0.0": {} } }

# https://jsr.io/@scope/a/1.0.0_meta.json
{ "exports": { ".": "./mod.ts" } }

# https://jsr.io/@scope/a/1.0.0/mod.ts
export class argsToArray {
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: unknown[]) {
return args;
}

export function overloadLast(value: string, ...args: string[]);
export function overloadLast(value: number, ...args: number[]);
export function overloadLast(value: string | number, ...args: unknown[]) {
return args;
}

# mod.ts
import 'jsr:@scope/a'

# output
{
"roots": [
"file:///mod.ts"
],
"modules": [
{
"kind": "esm",
"dependencies": [
{
"specifier": "jsr:@scope/a",
"code": {
"specifier": "jsr:@scope/a",
"span": {
"start": {
"line": 0,
"character": 7
},
"end": {
"line": 0,
"character": 21
}
}
}
}
],
"size": 22,
"mediaType": "TypeScript",
"specifier": "file:///mod.ts"
},
{
"kind": "esm",
"size": 549,
"mediaType": "TypeScript",
"specifier": "https://jsr.io/@scope/a/1.0.0/mod.ts"
}
],
"redirects": {
"jsr:@scope/a": "https://jsr.io/@scope/a/1.0.0/mod.ts"
},
"packages": {
"@scope/a": "@scope/a@1.0.0"
}
}

Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
{}
export class argsToArray {
argsToArray(..._args: string[]): void;
argsToArray(..._args: number[]): void;
argsToArray(...param0?: any): any {
return {} as never;
}
}
export function overloadSpread(...args: string[]);
export function overloadSpread(...args: number[]);
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 {
return {} as never;
}
--- DTS ---
export declare class argsToArray {
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[]);

0 comments on commit 523d1e2

Please sign in to comment.