From 2094625b9f7226db835e64e536d5d74d80a999c3 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 11 Apr 2024 11:28:57 +0200 Subject: [PATCH 1/3] fix(fast_check): properly handle rest parameters in overloads --- src/fast_check/transform.rs | 42 ++++++--- tests/specs/graph/jsr/FastCheck_Overloads.txt | 89 +++++++++++++++++++ 2 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 tests/specs/graph/jsr/FastCheck_Overloads.txt diff --git a/src/fast_check/transform.rs b/src/fast_check/transform.rs index d2db747b8..dd009ad0b 100644 --- a/src/fast_check/transform.rs +++ b/src/fast_check/transform.rs @@ -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()); } diff --git a/tests/specs/graph/jsr/FastCheck_Overloads.txt b/tests/specs/graph/jsr/FastCheck_Overloads.txt new file mode 100644 index 000000000..253a8a7aa --- /dev/null +++ b/tests/specs/graph/jsr/FastCheck_Overloads.txt @@ -0,0 +1,89 @@ +# 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 {} +} + +function overloadSpread(...args: string[]); +function overloadSpread(...args: number[]); +export function overloadSpread(...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": 314, + "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; + } + } + function overloadSpread(...args: string[]); + function overloadSpread(...args: number[]); + export function overloadSpread(...param0?: any): any { + return {} as never; + } + --- DTS --- + export declare class argsToArray { + argsToArray(..._args: string[]): void; + argsToArray(..._args: number[]): void; + } + declare function overloadSpread(...args: string[]); + declare function overloadSpread(...args: number[]); + export declare function overloadSpread(...param0?: any): any; From 9e51f76df2fa44f99e2fbca9b1ea16112c7fc139 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 11 Apr 2024 11:39:47 +0200 Subject: [PATCH 2/3] fix --- tests/specs/graph/jsr/FastCheck_Overloads.txt | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/specs/graph/jsr/FastCheck_Overloads.txt b/tests/specs/graph/jsr/FastCheck_Overloads.txt index 253a8a7aa..cc7276546 100644 --- a/tests/specs/graph/jsr/FastCheck_Overloads.txt +++ b/tests/specs/graph/jsr/FastCheck_Overloads.txt @@ -11,8 +11,8 @@ export class argsToArray { argsToArray(..._args: unknown[]): void {} } -function overloadSpread(...args: string[]); -function overloadSpread(...args: number[]); +export function overloadSpread(...args: string[]); +export function overloadSpread(...args: number[]); export function overloadSpread(...args: unknown[]) { return args; } @@ -52,7 +52,7 @@ import 'jsr:@scope/a' }, { "kind": "esm", - "size": 314, + "size": 328, "mediaType": "TypeScript", "specifier": "https://jsr.io/@scope/a/1.0.0/mod.ts" } @@ -74,8 +74,8 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts: return {} as never; } } - function overloadSpread(...args: string[]); - function overloadSpread(...args: number[]); + export function overloadSpread(...args: string[]); + export function overloadSpread(...args: number[]); export function overloadSpread(...param0?: any): any { return {} as never; } @@ -84,6 +84,5 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts: argsToArray(..._args: string[]): void; argsToArray(..._args: number[]): void; } - declare function overloadSpread(...args: string[]); - declare function overloadSpread(...args: number[]); - export declare function overloadSpread(...param0?: any): any; + export declare function overloadSpread(...args: string[]); + export declare function overloadSpread(...args: number[]); From b540bff6bc3978d3c62b31eb3286e23b729abb5e Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 11 Apr 2024 15:47:17 -0400 Subject: [PATCH 3/3] Add one more test --- tests/specs/graph/jsr/FastCheck_Overloads.txt | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/specs/graph/jsr/FastCheck_Overloads.txt b/tests/specs/graph/jsr/FastCheck_Overloads.txt index cc7276546..b34053113 100644 --- a/tests/specs/graph/jsr/FastCheck_Overloads.txt +++ b/tests/specs/graph/jsr/FastCheck_Overloads.txt @@ -17,6 +17,12 @@ 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' @@ -52,7 +58,7 @@ import 'jsr:@scope/a' }, { "kind": "esm", - "size": 328, + "size": 549, "mediaType": "TypeScript", "specifier": "https://jsr.io/@scope/a/1.0.0/mod.ts" } @@ -79,6 +85,11 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts: 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; @@ -86,3 +97,5 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts: } 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[]);