Skip to content

Commit

Permalink
implement changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
lachieh committed Nov 26, 2024
1 parent 892f5aa commit 449bb45
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
37 changes: 17 additions & 20 deletions crates/js-component-bindgen/src/ts_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ struct TsBindgen {
import_object: Source,
/// TypeScript definitions which will become the export object
export_object: Source,

/// Whether or not the types should be generated for a guest module
guest: bool,
}

/// Used to generate a `*.d.ts` file for each imported and exported interface for
Expand Down Expand Up @@ -59,6 +62,7 @@ pub fn ts_bindgen(
local_names: LocalNames::default(),
import_object: Source::default(),
export_object: Source::default(),
guest: opts.guest,
};

let world = &resolve.worlds[id];
Expand Down Expand Up @@ -106,7 +110,7 @@ pub fn ts_bindgen(
match name {
WorldKey::Name(name) => {
// kebab name -> direct ns namespace import
bindgen.import_interface(resolve, name, *id, files, opts.guest);
bindgen.import_interface(resolve, name, *id, files);
}
// namespaced ns:pkg/iface
// TODO: map support
Expand Down Expand Up @@ -170,7 +174,7 @@ pub fn ts_bindgen(
// namespace imports are grouped by namespace / kebab name
// kebab name imports are direct
for (name, import_interfaces) in interface_imports {
bindgen.import_interfaces(resolve, name.as_ref(), import_interfaces, files, opts.guest);
bindgen.import_interfaces(resolve, name.as_ref(), import_interfaces, files);
}
}

Expand Down Expand Up @@ -219,7 +223,6 @@ pub fn ts_bindgen(
*id,
files,
opts.instantiation.is_some(),
opts.guest,
);
export_aliases.push((iface_name.to_lower_camel_case(), local_name));
}
Expand Down Expand Up @@ -367,10 +370,9 @@ impl TsBindgen {
name: &str,
id: InterfaceId,
files: &mut Files,
guest: bool,
) -> String {
// in case an imported type is used as an exported type
let local_name = self.generate_interface(name, resolve, id, files, guest);
let local_name = self.generate_interface(name, resolve, id, files);
uwriteln!(
self.import_object,
"{}: typeof {local_name},",
Expand All @@ -384,22 +386,21 @@ impl TsBindgen {
import_name: &str,
ifaces: Vec<(String, &InterfaceId)>,
files: &mut Files,
guest: bool,
) {
if ifaces.len() == 1 {
let (iface_name, &id) = ifaces.first().unwrap();
if iface_name == "*" {
uwrite!(self.import_object, "{}: ", maybe_quote_id(import_name));
let name = resolve.interfaces[id].name.as_ref().unwrap();
let local_name = self.generate_interface(name, resolve, id, files, guest);
let local_name = self.generate_interface(name, resolve, id, files);
uwriteln!(self.import_object, "typeof {local_name},",);
return;
}
}
uwriteln!(self.import_object, "{}: {{", maybe_quote_id(import_name));
for (iface_name, &id) in ifaces {
let name = resolve.interfaces[id].name.as_ref().unwrap();
let local_name = self.generate_interface(name, resolve, id, files, guest);
let local_name = self.generate_interface(name, resolve, id, files);
uwriteln!(
self.import_object,
"{}: typeof {local_name},",
Expand Down Expand Up @@ -431,9 +432,8 @@ impl TsBindgen {
id: InterfaceId,
files: &mut Files,
instantiation: bool,
guest: bool,
) -> String {
let local_name = self.generate_interface(export_name, resolve, id, files, guest);
let local_name = self.generate_interface(export_name, resolve, id, files);
if instantiation {
uwriteln!(
self.export_object,
Expand Down Expand Up @@ -476,7 +476,6 @@ impl TsBindgen {
resolve: &Resolve,
id: InterfaceId,
files: &mut Files,
guest: bool,
) -> String {
let iface = resolve
.interfaces
Expand Down Expand Up @@ -525,17 +524,15 @@ impl TsBindgen {
return local_name;
}

let module_or_namespace = if self.guest {
format!("declare module '{id_name}' {{")
} else {
format!("export namespace {camel} {{")
};

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

uwriteln!(
gen.src,
"{}",
if guest {
format!("declare module '{id_name}' {{")
} else {
format!("export namespace {camel} {{")
}
);
uwriteln!(gen.src, "{module_or_namespace}");
for (_, func) in resolve.interfaces[id].functions.iter() {
// Ensure that the function the world item for stability guarantees and exclude if they do not match
if !feature_gate_allowed(resolve, package, &func.stability, &func.name)
Expand Down
2 changes: 1 addition & 1 deletion src/jco.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ program.command('types')
.option('-q, --quiet', 'disable output summary')
.option('--feature <feature>', 'enable one specific WIT feature (repeatable)', collectOptions, [])
.option('--all-features', 'enable all features')
.option('--guest', 'generate types to be consumed by a guest component (module declarations)')
.option('--guest', 'generate types to be consumed by a componentize guest component (using module declarations)')
.action(asyncAction(types));

program.command('run')
Expand Down

0 comments on commit 449bb45

Please sign in to comment.