Skip to content

Commit 356e21d

Browse files
authored
fix: use interface in type declarations (#51)
Declares the `createActor` function return type using the interface instead of the actual class name.
1 parent 7c96fc6 commit 356e21d

File tree

7 files changed

+21
-27
lines changed

7 files changed

+21
-27
lines changed

src/core/generate/rs/src/bindings/typescript_native/compile_interface.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::conversion_functions_generator::TypeConverter;
2-
use super::new_typescript_native_types::{add_type_definitions, create_interface_from_service};
2+
use super::new_typescript_native_types::{
3+
add_type_definitions, create_interface_from_service, service_interface_ident,
4+
};
35
use super::preamble::imports::interface_imports;
46
use super::preamble::options::interface_options_utils;
57
use super::utils::EnumDeclarations;
@@ -161,14 +163,11 @@ pub fn interface_actor_var(module: &mut Module, type_id: &str, service_name: &st
161163
let interface = TsInterfaceDecl {
162164
span: DUMMY_SP,
163165
declare: false,
164-
id: get_ident_guarded(&format!("{}Interface", service_name)),
166+
id: service_interface_ident(service_name),
165167
type_params: None,
166168
extends: vec![TsExprWithTypeArgs {
167169
span: DUMMY_SP,
168-
expr: Box::new(Expr::Ident(get_ident_guarded(&format!(
169-
"{}Interface",
170-
type_id
171-
)))),
170+
expr: Box::new(Expr::Ident(service_interface_ident(type_id))),
172171
type_args: None,
173172
}],
174173
body: TsInterfaceBody {
@@ -185,12 +184,6 @@ pub fn interface_actor_var(module: &mut Module, type_id: &str, service_name: &st
185184
}
186185

187186
fn add_create_actor_interface_exports(module: &mut Module, service_name: &str) {
188-
let capitalized_service_name = service_name
189-
.chars()
190-
.next()
191-
.map_or(String::new(), |c| c.to_uppercase().collect::<String>())
192-
+ &service_name[1..];
193-
194187
let type_process_error_fn = super::preamble::actor::process_error_fn_type();
195188
module
196189
.body
@@ -283,7 +276,7 @@ fn add_create_actor_interface_exports(module: &mut Module, service_name: &str) {
283276
span: DUMMY_SP,
284277
type_ann: Box::new(TsType::TsTypeRef(TsTypeRef {
285278
span: DUMMY_SP,
286-
type_name: TsEntityName::Ident(get_ident_guarded(&capitalized_service_name)),
279+
type_name: TsEntityName::Ident(service_interface_ident(service_name)),
287280
type_params: None,
288281
})),
289282
})),

src/core/generate/rs/src/bindings/typescript_native/compile_wrapper.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use swc_core::common::{DUMMY_SP, SyntaxContext};
77
use swc_core::ecma::ast::*;
88

99
use super::conversion_functions_generator::convert_multi_return_from_candid;
10-
use super::new_typescript_native_types::convert_type_with_converter;
10+
use super::new_typescript_native_types::{convert_type_with_converter, service_interface_ident};
1111

1212
use super::new_typescript_native_types::add_type_definitions;
1313
use super::preamble::imports::wrapper_imports;
@@ -377,10 +377,7 @@ fn create_actor_class(
377377
super_type_params: None,
378378
implements: vec![TsExprWithTypeArgs {
379379
span: DUMMY_SP,
380-
expr: Box::new(Expr::Ident(get_ident_guarded(&format!(
381-
"{}Interface",
382-
service_name
383-
)))),
380+
expr: Box::new(Expr::Ident(service_interface_ident(service_name))),
384381
type_args: None,
385382
}],
386383
is_abstract: false,

src/core/generate/rs/src/bindings/typescript_native/new_typescript_native_types.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn is_recursive_optional(
4747
pub fn create_interface_from_service(
4848
top_level_nodes: &mut TopLevelNodes,
4949
env: &TypeEnv,
50-
id: &str,
50+
service_name: &str,
5151
syntax: Option<&IDLType>,
5252
serv: &[(String, Type)],
5353
) -> TsInterfaceDecl {
@@ -90,7 +90,7 @@ pub fn create_interface_from_service(
9090
TsInterfaceDecl {
9191
span: DUMMY_SP,
9292
declare: false,
93-
id: get_ident_guarded(&format!("{}Interface", id)),
93+
id: service_interface_ident(service_name),
9494
type_params: None,
9595
extends: vec![],
9696
body: TsInterfaceBody {
@@ -701,8 +701,8 @@ pub fn add_type_definitions(
701701
TypeInner::Var(inner_id) => {
702702
let inner_type = env.rec_find_type(inner_id).unwrap();
703703
let inner_name = match inner_type.as_ref() {
704-
TypeInner::Service(_) => format!("{}Interface", inner_id),
705-
_ => inner_id.as_str().to_string(),
704+
TypeInner::Service(_) => service_interface_ident(inner_id.as_str()),
705+
_ => get_ident_guarded(inner_id.as_str()),
706706
};
707707
let type_alias = TsTypeAliasDecl {
708708
span: DUMMY_SP,
@@ -711,7 +711,7 @@ pub fn add_type_definitions(
711711
type_params: None,
712712
type_ann: Box::new(TsType::TsTypeRef(TsTypeRef {
713713
span: DUMMY_SP,
714-
type_name: TsEntityName::Ident(get_ident_guarded(&inner_name)),
714+
type_name: TsEntityName::Ident(inner_name),
715715
type_params: None,
716716
})),
717717
};
@@ -1103,3 +1103,7 @@ fn create_function_type_ref() -> TsType {
11031103
.collect(),
11041104
})
11051105
}
1106+
1107+
pub fn service_interface_ident(service_name: &str) -> Ident {
1108+
get_ident_guarded(&format!("{}Interface", service_name))
1109+
}

tests/snapshots/generate/example/example.d.ts.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,4 @@ export interface CreateActorOptions {
215215
agentOptions?: HttpAgentOptions;
216216
actorOptions?: ActorConfig;
217217
}
218-
export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): Example;
218+
export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): exampleInterface;

tests/snapshots/generate/hello_world/hello_world.d.ts.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ export interface CreateActorOptions {
2626
agentOptions?: HttpAgentOptions;
2727
actorOptions?: ActorConfig;
2828
}
29-
export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): Hello_world;
29+
export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): hello_worldInterface;

tests/snapshots/wasm-generate/example/example.d.ts.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,4 @@ export interface CreateActorOptions {
207207
agentOptions?: HttpAgentOptions;
208208
actorOptions?: ActorConfig;
209209
}
210-
export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): Example;
210+
export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): exampleInterface;

tests/snapshots/wasm-generate/hello_world/hello_world.d.ts.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ export interface CreateActorOptions {
1818
agentOptions?: HttpAgentOptions;
1919
actorOptions?: ActorConfig;
2020
}
21-
export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): Hello_world;
21+
export declare function createActor(canisterId: string, options: CreateActorOptions = {}, processError?: ProcessErrorFn): hello_worldInterface;

0 commit comments

Comments
 (0)