Skip to content

Commit

Permalink
fix: generate typescript private resource constructors when none are …
Browse files Browse the repository at this point in the history
…defined (#548)
  • Loading branch information
OmarTawfik authored Jan 25, 2025
1 parent eefa6f3 commit 5b2617d
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions crates/js-component-bindgen/src/ts_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct TsInterface<'a> {
src: Source,
is_root: bool,
resolve: &'a Resolve,
has_constructor: bool,
needs_ty_option: bool,
needs_ty_result: bool,
local_names: LocalNames,
Expand Down Expand Up @@ -140,7 +141,7 @@ pub fn ts_bindgen(
continue;
}

let mut gen = bindgen.ts_interface(resolve, true);
let mut gen = TsInterface::new(resolve, true);
gen.docs(&ty.docs);
match &ty.kind {
TypeDefKind::Record(record) => {
Expand Down Expand Up @@ -418,7 +419,7 @@ impl TsBindgen {
_files: &mut Files,
) {
uwriteln!(self.import_object, "{}: {{", maybe_quote_id(import_name));
let mut gen = self.ts_interface(resolve, false);
let mut gen = TsInterface::new(resolve, false);
gen.ts_func(func, true, false);
let src = gen.finish();
self.import_object.push_str(&src);
Expand Down Expand Up @@ -462,7 +463,7 @@ impl TsBindgen {
_files: &mut Files,
declaration: bool,
) {
let mut gen = self.ts_interface(resolve, false);
let mut gen = TsInterface::new(resolve, false);
for (_, func) in funcs {
gen.ts_func(func, false, declaration);
}
Expand Down Expand Up @@ -530,7 +531,7 @@ impl TsBindgen {
format!("export namespace {camel} {{")
};

let mut gen = self.ts_interface(resolve, false);
let mut gen = TsInterface::new(resolve, false);

uwriteln!(gen.src, "{module_or_namespace}");
for (_, func) in resolve.interfaces[id].functions.iter() {
Expand Down Expand Up @@ -566,18 +567,6 @@ impl TsBindgen {

local_name
}

fn ts_interface<'b>(&'b mut self, resolve: &'b Resolve, is_root: bool) -> TsInterface<'b> {
TsInterface {
is_root,
src: Source::default(),
resources: BTreeMap::new(),
local_names: LocalNames::default(),
resolve,
needs_ty_option: false,
needs_ty_result: false,
}
}
}

impl<'a> TsInterface<'a> {
Expand All @@ -588,6 +577,7 @@ impl<'a> TsInterface<'a> {
resources: BTreeMap::new(),
local_names: LocalNames::default(),
resolve,
has_constructor: false,
needs_ty_option: false,
needs_ty_result: false,
}
Expand All @@ -600,6 +590,12 @@ impl<'a> TsInterface<'a> {
"\nexport class {} {{",
resource.to_upper_camel_case()
);
if !source.has_constructor {
uwriteln!(self.src, "/**");
uwriteln!(self.src, " * This type does not have a public constructor.");
uwriteln!(self.src, " */");
uwriteln!(self.src, "private constructor();");
}
self.src.push_str(&source.src);
uwriteln!(self.src, "}}")
}
Expand Down Expand Up @@ -793,7 +789,10 @@ impl<'a> TsInterface<'a> {
iface.src.push_str(&format!("static '{out_name}'"))
}
}
FunctionKind::Constructor(_) => iface.src.push_str("constructor"),
FunctionKind::Constructor(_) => {
iface.has_constructor = true;
iface.src.push_str("constructor");
}
}
} else if is_js_identifier(&out_name) {
iface.src.push_str(&out_name);
Expand Down

0 comments on commit 5b2617d

Please sign in to comment.