diff --git a/src/fast_check/transform_dts.rs b/src/fast_check/transform_dts.rs index beab1ae49..96b09cf6e 100644 --- a/src/fast_check/transform_dts.rs +++ b/src/fast_check/transform_dts.rs @@ -498,14 +498,16 @@ impl<'a> FastCheckDtsTransformer<'a> { } fn decl_to_type_decl(&mut self, decl: Decl) -> Option { + let is_declare = self.is_top_level; match decl { Decl::Class(mut class_decl) => { class_decl.class.body = self.class_body_to_type(class_decl.class.body); - class_decl.declare = self.is_top_level; + class_decl.declare = is_declare; Some(Decl::Class(class_decl)) } Decl::Fn(mut fn_decl) => { fn_decl.function.body = None; + fn_decl.declare = is_declare; for param in &mut fn_decl.function.params { match &mut param.pat { @@ -570,7 +572,7 @@ impl<'a> FastCheckDtsTransformer<'a> { Some(Decl::Fn(fn_decl)) } Decl::Var(mut var_decl) => { - var_decl.declare = self.is_top_level; + var_decl.declare = is_declare; for decl in &mut var_decl.decls { if let Pat::Ident(ident) = &mut decl.name { @@ -602,7 +604,7 @@ impl<'a> FastCheckDtsTransformer<'a> { Some(Decl::Var(var_decl)) } Decl::TsEnum(mut ts_enum) => { - ts_enum.declare = self.is_top_level; + ts_enum.declare = is_declare; for member in &mut ts_enum.members { if let Some(init) = &member.init { @@ -619,7 +621,7 @@ impl<'a> FastCheckDtsTransformer<'a> { Some(Decl::TsEnum(ts_enum)) } Decl::TsModule(mut ts_module) => { - ts_module.declare = self.is_top_level; + ts_module.declare = is_declare; if let Some(body) = ts_module.body.clone() { ts_module.body = Some(self.transform_ts_ns_body(body)); @@ -995,7 +997,7 @@ mod tests { r#"export function foo(a: number): number { return {}; }"#, - "export function foo(a: number): number;", + "export declare function foo(a: number): number;", ) .await; transform_dts_test( @@ -1003,35 +1005,35 @@ mod tests { export function foo(a: any): number { return {}; }"#, - r#"export function foo(a: string): number;"#, + r#"export declare function foo(a: string): number;"#, ) .await; transform_dts_test( r#"export function foo(a = 2): number { return 2; }"#, - r#"export function foo(a?: number): number;"#, + r#"export declare function foo(a?: number): number;"#, ) .await; transform_dts_test( r#"export function foo(a: string = 2): number { return 2; }"#, - r#"export function foo(a?: string): number;"#, + r#"export declare function foo(a?: string): number;"#, ) .await; transform_dts_test( r#"export function foo([a, b] = [1, 2]): number { return 2; }"#, - r#"export function foo([a, b]?: [number, number]): number;"#, + r#"export declare function foo([a, b]?: [number, number]): number;"#, ) .await; transform_dts_test( r#"export function foo({a, b} = { a: 1, b: 2 }): number { return 2; }"#, - r#"export function foo({ a, b }?: { + r#"export declare function foo({ a, b }?: { a: number; b: number; }): number;"#, diff --git a/src/graph.rs b/src/graph.rs index 0b99942f4..6e1833685 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -5383,7 +5383,7 @@ mod tests { let dts = fsm.dts.unwrap(); assert_eq!( dts.text.to_string().trim(), - "export function add(a: number, b: number): number;" + "export declare function add(a: number, b: number): number;" ); assert!(dts.diagnostics.is_empty()); } diff --git a/tests/specs/graph/jsr/FastCheck_EntrypointJsWithTsDecl.txt b/tests/specs/graph/jsr/FastCheck_EntrypointJsWithTsDecl.txt index 7973a67ed..6f1228c80 100644 --- a/tests/specs/graph/jsr/FastCheck_EntrypointJsWithTsDecl.txt +++ b/tests/specs/graph/jsr/FastCheck_EntrypointJsWithTsDecl.txt @@ -87,4 +87,4 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.d.ts: {} export function getRandom(): number; --- DTS --- - export function getRandom(): number; + export declare function getRandom(): number; diff --git a/tests/specs/graph/jsr/FastCheck_Functions.txt b/tests/specs/graph/jsr/FastCheck_Functions.txt index 9d216a850..a7bdddc02 100644 --- a/tests/specs/graph/jsr/FastCheck_Functions.txt +++ b/tests/specs/graph/jsr/FastCheck_Functions.txt @@ -184,30 +184,30 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts: export declare function declareFunc(param: string): string; export declare function declareFunc(param: PublicDeclareFunc): string; --- DTS --- - export function test1(): void; - export function test2(): number; - /** Some Doc **/ export function test3(param: number): number; - export function test4(param?: number): number; - export function test5(param?: number): T; - export function test6(param?: number): T; - export function test7(param?: number): number; - export function test7(param?: number, param2?: PublicOther2): number; - function test8(param: number): number; - function test8(param: string): string; - function test8(param0?: any): any; + export declare function test1(): void; + export declare function test2(): number; + /** Some Doc **/ export declare function test3(param: number): number; + export declare function test4(param?: number): number; + export declare function test5(param?: number): T; + export declare function test6(param?: number): T; + export declare function test7(param?: number): number; + export declare function test7(param?: number, param2?: PublicOther2): number; + declare function test8(param: number): number; + declare function test8(param: string): string; + declare function test8(param0?: any): any; export { test8 }; export default function test9(param: number): number; export default function test9(param: string): string; - export function test10(...params: string[]): string[]; - export function test11(options?: { + export declare function test10(...params: string[]): string[]; + export declare function test11(options?: { copy: boolean; }): void; export interface GlobOptions { } - export function test12({}?: GlobOptions): void; - export function test13([]?: number[]): void; - export function test14(): never; - export function test15(): Generator; + export declare function test12({}?: GlobOptions): void; + export declare function test13([]?: number[]): void; + export declare function test14(): never; + export declare function test15(): Generator; declare class PublicOther { } declare class PublicOther2 { diff --git a/tests/specs/graph/jsr/FastCheck_InitIdentsAndMembers.txt b/tests/specs/graph/jsr/FastCheck_InitIdentsAndMembers.txt index 0b3495d62..d7ce0c437 100644 --- a/tests/specs/graph/jsr/FastCheck_InitIdentsAndMembers.txt +++ b/tests/specs/graph/jsr/FastCheck_InitIdentsAndMembers.txt @@ -167,7 +167,7 @@ Fast check https://jsr.io/@scope/b/1.0.0/mod.ts: export class A { } } - export function test(a?: any, C?: any): void; + export declare function test(a?: any, C?: any): void; --- DTS Diagnostics --- unable to infer type, falling back to any type at https://jsr.io/@scope/b/1.0.0/mod.ts@140 diff --git a/tests/specs/graph/jsr/FastCheck_Issue22829.txt b/tests/specs/graph/jsr/FastCheck_Issue22829.txt index 06c0ef09f..a268eed48 100644 --- a/tests/specs/graph/jsr/FastCheck_Issue22829.txt +++ b/tests/specs/graph/jsr/FastCheck_Issue22829.txt @@ -118,8 +118,8 @@ Fast check https://jsr.io/@scope/a/1.0.0/functions.ts: declare abstract class Internal { prop: T; } - function func(): void; - export function other_func(): void; + declare function func(): void; + export declare function other_func(): void; export declare const functions: { readonly other; }; diff --git a/tests/specs/graph/jsr/FastCheck_JsWithTsDecl.txt b/tests/specs/graph/jsr/FastCheck_JsWithTsDecl.txt index 0acca050e..73f5f9f79 100644 --- a/tests/specs/graph/jsr/FastCheck_JsWithTsDecl.txt +++ b/tests/specs/graph/jsr/FastCheck_JsWithTsDecl.txt @@ -136,4 +136,4 @@ Fast check https://jsr.io/@scope/a/1.0.0/random.d.ts: {} export function getRandom(): number; --- DTS --- - export function getRandom(): number; + export declare function getRandom(): number; diff --git a/tests/specs/graph/jsr/FastCheck_TypeAssertion.txt b/tests/specs/graph/jsr/FastCheck_TypeAssertion.txt index 45feda660..8a3fe966b 100644 --- a/tests/specs/graph/jsr/FastCheck_TypeAssertion.txt +++ b/tests/specs/graph/jsr/FastCheck_TypeAssertion.txt @@ -131,7 +131,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts: export declare const str: "foo" & { __brand: "foo"; }; - export function createMockApp = Record>(state?: S): Application; + export declare function createMockApp = Record>(state?: S): Application; export declare class A { prop: Public1; constructor(prop?: Public1);