From 449bb453f1d9156ac9ce74e2e38acdcb3b53deec Mon Sep 17 00:00:00 2001 From: Lachlan Heywood Date: Tue, 26 Nov 2024 17:59:34 -0500 Subject: [PATCH] implement changes from code review --- crates/js-component-bindgen/src/ts_bindgen.rs | 37 +++++++++---------- src/jco.js | 2 +- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/crates/js-component-bindgen/src/ts_bindgen.rs b/crates/js-component-bindgen/src/ts_bindgen.rs index 8faf1af17..8cda37a5f 100644 --- a/crates/js-component-bindgen/src/ts_bindgen.rs +++ b/crates/js-component-bindgen/src/ts_bindgen.rs @@ -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 @@ -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]; @@ -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 @@ -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); } } @@ -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)); } @@ -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},", @@ -384,14 +386,13 @@ 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; } @@ -399,7 +400,7 @@ impl TsBindgen { 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},", @@ -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, @@ -476,7 +476,6 @@ impl TsBindgen { resolve: &Resolve, id: InterfaceId, files: &mut Files, - guest: bool, ) -> String { let iface = resolve .interfaces @@ -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) diff --git a/src/jco.js b/src/jco.js index 0ac34c329..b3ff255d8 100755 --- a/src/jco.js +++ b/src/jco.js @@ -79,7 +79,7 @@ program.command('types') .option('-q, --quiet', 'disable output summary') .option('--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')