diff --git a/crates/js-component-bindgen-component/src/lib.rs b/crates/js-component-bindgen-component/src/lib.rs index 20617aec1..fe7bd6659 100644 --- a/crates/js-component-bindgen-component/src/lib.rs +++ b/crates/js-component-bindgen-component/src/lib.rs @@ -49,10 +49,7 @@ impl Guest for JsComponentBindgenComponent { name: options.name, no_typescript: options.no_typescript.unwrap_or(false), instantiation: options.instantiation.unwrap_or(false), - map: match options.map { - Some(map) => Some(map.into_iter().collect()), - None => None, - }, + map: options.map.map(|map| map.into_iter().collect()), no_nodejs_compat: options.no_nodejs_compat.unwrap_or(false), base64_cutoff: options.base64_cutoff.unwrap_or(5000) as usize, tla_compat: options @@ -110,10 +107,7 @@ impl Guest for JsComponentBindgenComponent { }; let id = resolve.push(pkg).map_err(|e| e.to_string())?; - let world_string = match &opts.world { - Some(world) => Some(world.to_string()), - None => None, - }; + let world_string = opts.world.map(|world| world.to_string()); let world = resolve .select_world(id, world_string.as_deref()) .map_err(|e| e.to_string())?; @@ -123,10 +117,7 @@ impl Guest for JsComponentBindgenComponent { no_typescript: false, no_nodejs_compat: false, instantiation: opts.instantiation.unwrap_or(false), - map: match opts.map { - Some(map) => Some(map.into_iter().collect()), - None => None, - }, + map: opts.map.map(|map| map.into_iter().collect()), tla_compat: opts.tla_compat.unwrap_or(false), valid_lifting_optimization: false, base64_cutoff: 0, diff --git a/crates/js-component-bindgen/src/core.rs b/crates/js-component-bindgen/src/core.rs index 311e5b568..7766c79d8 100644 --- a/crates/js-component-bindgen/src/core.rs +++ b/crates/js-component-bindgen/src/core.rs @@ -82,15 +82,17 @@ pub enum AugmentedOp { impl<'a> Translation<'a> { pub fn new(translation: ModuleTranslation<'a>) -> Result> { - let mut features = WasmFeatures::default(); - features.multi_memory = false; - match Validator::new_with_features(features).validate_all(&translation.wasm) { + let mut features = WasmFeatures { + multi_memory: false, + ..Default::default() + }; + match Validator::new_with_features(features).validate_all(translation.wasm) { // This module validates without multi-memory, no need to augment // it Ok(_) => return Ok(Translation::Normal(translation)), Err(e) => { features.multi_memory = true; - match Validator::new_with_features(features).validate_all(&translation.wasm) { + match Validator::new_with_features(features).validate_all(translation.wasm) { // This module validates with multi-memory, so fall through // to augmentation. Ok(_) => {} @@ -609,7 +611,7 @@ macro_rules! define_translate { // the cases below. ($(@$p:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident)*) => { $( - #[allow(clippy::drop_copy)] + #[allow(dropping_copy_types)] fn $visit(&mut self $(, $($arg: $argty),*)?) { #[allow(unused_imports)] use wasm_encoder::Instruction::*; diff --git a/crates/js-component-bindgen/src/esm_bindgen.rs b/crates/js-component-bindgen/src/esm_bindgen.rs index a86d675e3..605cae6ae 100644 --- a/crates/js-component-bindgen/src/esm_bindgen.rs +++ b/crates/js-component-bindgen/src/esm_bindgen.rs @@ -114,14 +114,14 @@ impl EsmBindgen { } /// get the exports (including exported aliases) from the bindgen - pub fn exports<'a>(&'a self) -> Vec<(&'a str, &'a str)> { + pub fn exports(&self) -> Vec<(&str, &str)> { self.export_aliases .iter() .map(|(alias, name)| (alias.as_ref(), name.as_ref())) .chain( self.exports - .iter() - .map(|(name, _)| (name.as_ref(), name.as_ref())), + .keys() + .map(|name| (name.as_ref(), name.as_ref())), ) .collect() } @@ -146,7 +146,7 @@ impl EsmBindgen { continue; }; let (local_name, _) = - local_names.get_or_create(&format!("export:{}", export_name), &export_name); + local_names.get_or_create(&format!("export:{export_name}"), export_name); uwriteln!(output, "const {local_name} = {{"); for (func_name, export) in iface { let Binding::Local(local_name) = export else { @@ -173,7 +173,7 @@ impl EsmBindgen { Binding::Interface(_) => local_names.get(&format!("export:{}", export_name)), }; let alias_maybe_quoted = maybe_quote_id(alias); - if local_name == &alias_maybe_quoted { + if local_name == alias_maybe_quoted { output.push_str(local_name); } else if instantiation { uwrite!(output, "{alias_maybe_quoted}: {local_name}"); @@ -192,7 +192,7 @@ impl EsmBindgen { Binding::Interface(_) => local_names.get(&format!("export:{}", export_name)), }; let export_name_maybe_quoted = maybe_quote_id(export_name); - if local_name == &export_name_maybe_quoted { + if local_name == export_name_maybe_quoted { output.push_str(local_name); } else if instantiation { uwrite!(output, "{export_name_maybe_quoted}: {local_name}"); @@ -230,7 +230,7 @@ impl EsmBindgen { } match binding { Binding::Interface(bindings) => { - if !imports_object.is_some() && bindings.len() == 1 { + if imports_object.is_none() && bindings.len() == 1 { let (import_name, import) = bindings.iter().next().unwrap(); if import_name == "default" { let local_name = match import { @@ -267,12 +267,10 @@ impl EsmBindgen { }; if external_name == local_name { uwrite!(output, "{external_name}"); + } else if imports_object.is_some() { + uwrite!(output, "{external_name}: {local_name}"); } else { - if imports_object.is_some() { - uwrite!(output, "{external_name}: {local_name}"); - } else { - uwrite!(output, "{external_name} as {local_name}"); - } + uwrite!(output, "{external_name} as {local_name}"); } } if !first { diff --git a/crates/js-component-bindgen/src/files.rs b/crates/js-component-bindgen/src/files.rs index e6bc3256f..b1f8ede54 100644 --- a/crates/js-component-bindgen/src/files.rs +++ b/crates/js-component-bindgen/src/files.rs @@ -18,14 +18,11 @@ impl Files { } pub fn get_size(&mut self, name: &str) -> Option { - match self.files.get(name) { - Some(data) => Some(data.len()), - None => None, - } + self.files.get(name).map(|data| data.len()) } pub fn remove(&mut self, name: &str) -> Option> { - return self.files.remove(name); + self.files.remove(name) } pub fn iter(&self) -> impl Iterator { diff --git a/crates/js-component-bindgen/src/function_bindgen.rs b/crates/js-component-bindgen/src/function_bindgen.rs index 950e76aca..289e38c9d 100644 --- a/crates/js-component-bindgen/src/function_bindgen.rs +++ b/crates/js-component-bindgen/src/function_bindgen.rs @@ -92,7 +92,7 @@ impl FunctionBindgen<'_> { fn intrinsic(&mut self, intrinsic: Intrinsic) -> String { self.intrinsics.insert(intrinsic); - return intrinsic.name().to_string(); + intrinsic.name().to_string() } fn clamp_guest(&mut self, results: &mut Vec, operands: &[String], min: T, max: T) @@ -149,7 +149,7 @@ impl Bindgen for FunctionBindgen<'_> { type Operand = String; fn sizes(&self) -> &SizeAlign { - &self.sizes + self.sizes } fn push_block(&mut self) { @@ -351,7 +351,7 @@ impl Bindgen for FunctionBindgen<'_> { for (field, op) in record.fields.iter().zip(operands) { result.push_str(&format!("{}: {},\n", field.name.to_lower_camel_case(), op)); } - result.push_str("}"); + result.push('}'); results.push(result); } @@ -534,7 +534,7 @@ impl Bindgen for FunctionBindgen<'_> { assert!(block_results.len() == 1); uwriteln!(self.src, " val: {}", block_results[0]); } else { - assert!(block_results.len() == 0); + assert!(block_results.is_empty()); } uwriteln!( self.src, @@ -612,7 +612,7 @@ impl Bindgen for FunctionBindgen<'_> { Instruction::OptionLift { payload, .. } => { let (some, some_results) = self.blocks.pop().unwrap(); let (none, none_results) = self.blocks.pop().unwrap(); - assert!(none_results.len() == 0); + assert!(none_results.is_empty()); assert!(some_results.len() == 1); let some_result = &some_results[0]; @@ -716,14 +716,14 @@ impl Bindgen for FunctionBindgen<'_> { let (ok, ok_results) = self.blocks.pop().unwrap(); let ok_result = if result.ok.is_some() { assert_eq!(ok_results.len(), 1); - format!("{}", ok_results[0]) + ok_results[0].to_string() } else { assert_eq!(ok_results.len(), 0); String::from("undefined") }; let err_result = if result.err.is_some() { assert_eq!(err_results.len(), 1); - format!("{}", err_results[0]) + err_results[0].to_string() } else { assert_eq!(err_results.len(), 0); String::from("undefined") @@ -1072,7 +1072,7 @@ impl Bindgen for FunctionBindgen<'_> { } // after a high level call, we need to deactivate the component resource borrows - if self.cur_resource_borrows.len() > 0 { + if !self.cur_resource_borrows.is_empty() { let symbol_resource_handle = self.intrinsic(Intrinsic::SymbolResourceHandle); for resource in &self.cur_resource_borrows { uwriteln!(self.src, "{resource}[{symbol_resource_handle}] = null;"); diff --git a/crates/js-component-bindgen/src/names.rs b/crates/js-component-bindgen/src/names.rs index 295f83c9f..c888008dc 100644 --- a/crates/js-component-bindgen/src/names.rs +++ b/crates/js-component-bindgen/src/names.rs @@ -24,13 +24,13 @@ impl<'a> LocalNames { let goal_name = if let Some(last_char) = goal_name.rfind('/') { &goal_name[last_char + 1..] } else { - &goal_name + goal_name }; let mut goal = to_js_identifier(goal_name); if self.local_names.contains(&goal) { let mut idx = 1; loop { - let valid_name_suffixed = format!("{goal}${}", idx.to_string()); + let valid_name_suffixed = format!("{goal}${idx}"); if !self.local_names.contains(&valid_name_suffixed) { goal = valid_name_suffixed; break; @@ -75,7 +75,7 @@ pub fn to_js_identifier(goal_name: &str) -> String { let goal = goal_name.to_lower_camel_case(); let mut identifier = String::new(); for char in goal.chars() { - let valid_char = if identifier.len() == 0 { + let valid_char = if identifier.is_empty() { is_js_identifier_start(char) } else { is_js_identifier_char(char) @@ -108,7 +108,7 @@ pub fn is_js_identifier(s: &str) -> bool { } else { return false; } - while let Some(char) = chars.next() { + for char in chars { if !is_js_identifier_char(char) { return false; } @@ -119,21 +119,21 @@ pub fn is_js_identifier(s: &str) -> bool { // https://tc39.es/ecma262/#prod-IdentifierStartChar // Unicode ID_Start | "$" | "_" fn is_js_identifier_start(code: char) -> bool { - return match code { + match code { 'A'..='Z' | 'a'..='z' | '$' | '_' => true, // leaving out non-ascii for now... _ => false, - }; + } } // https://tc39.es/ecma262/#prod-IdentifierPartChar // Unicode ID_Continue | "$" | U+200C | U+200D fn is_js_identifier_char(code: char) -> bool { - return match code { + match code { '0'..='9' | 'A'..='Z' | 'a'..='z' | '$' | '_' => true, // leaving out non-ascii for now... _ => false, - }; + } } pub fn maybe_quote_id(name: &str) -> String { @@ -154,7 +154,7 @@ pub fn maybe_quote_member(name: &str) -> String { } } -pub(crate) const RESERVED_KEYWORDS: &'static [&'static str] = &[ +pub(crate) const RESERVED_KEYWORDS: &[&str] = &[ "await", "break", "case", diff --git a/crates/js-component-bindgen/src/transpile_bindgen.rs b/crates/js-component-bindgen/src/transpile_bindgen.rs index 3ec82448b..d6dbd4b73 100644 --- a/crates/js-component-bindgen/src/transpile_bindgen.rs +++ b/crates/js-component-bindgen/src/transpile_bindgen.rs @@ -179,31 +179,29 @@ impl<'a> JsBindgen<'a> { compilation_promises, "const {local_name} = compileCore('{name_idx}');" ); + } else if files.get_size(&name_idx).unwrap() < self.opts.base64_cutoff { + assert!(removed.insert(i)); + let data = files.remove(&name_idx).unwrap(); + uwriteln!( + compilation_promises, + "const {local_name} = {}('{}');", + self.intrinsic(Intrinsic::Base64Compile), + general_purpose::STANDARD_NO_PAD.encode(&data), + ); } else { - if files.get_size(&name_idx).unwrap() < self.opts.base64_cutoff { - assert!(removed.insert(i)); + // Maintain numerical file orderings when a previous file was + // inlined + if let Some(&replacement) = removed.iter().next() { + assert!(removed.remove(&replacement) && removed.insert(i)); let data = files.remove(&name_idx).unwrap(); - uwriteln!( - compilation_promises, - "const {local_name} = {}('{}');", - self.intrinsic(Intrinsic::Base64Compile), - general_purpose::STANDARD_NO_PAD.encode(&data), - ); - } else { - // Maintain numerical file orderings when a previous file was - // inlined - if let Some(&replacement) = removed.iter().next() { - assert!(removed.remove(&replacement) && removed.insert(i)); - let data = files.remove(&name_idx).unwrap(); - name_idx = core_file_name(name, replacement as u32); - files.push(&name_idx, &data); - } - uwriteln!( - compilation_promises, - "const {local_name} = {}(new URL('./{name_idx}', import.meta.url));", - self.intrinsic(Intrinsic::FetchCompile) - ); + name_idx = core_file_name(name, replacement as u32); + files.push(&name_idx, &data); } + uwriteln!( + compilation_promises, + "const {local_name} = {}(new URL('./{name_idx}', import.meta.url));", + self.intrinsic(Intrinsic::FetchCompile) + ); } } @@ -302,7 +300,7 @@ impl<'a> JsBindgen<'a> { fn intrinsic(&mut self, intrinsic: Intrinsic) -> String { self.all_intrinsics.insert(intrinsic); - return intrinsic.name().to_string(); + intrinsic.name().to_string() } } @@ -533,7 +531,7 @@ impl<'a> Instantiator<'a, '_> { if (handleEntry.own) {{ {}(handleEntry.rep); }}", - self.core_def(&dtor) + self.core_def(dtor) ), ) } else { @@ -677,7 +675,7 @@ impl<'a> Instantiator<'a, '_> { {}(handleEntry.rep); }} ", - self.core_def(&dtor) + self.core_def(dtor) ) } else { "".into() @@ -775,7 +773,7 @@ impl<'a> Instantiator<'a, '_> { } imports.push_str("},\n"); } - imports.push_str("}"); + imports.push('}'); } let i = self.instances.push(idx); @@ -817,7 +815,7 @@ impl<'a> Instantiator<'a, '_> { let world_key = &self.imports[import_name]; // nested interfaces only currently possible through mapping - let (import_specifier, maybe_iface_member) = map_import(&self.gen.opts.map, &import_name); + let (import_specifier, maybe_iface_member) = map_import(&self.gen.opts.map, import_name); let (func, func_name, iface_name) = match &self.resolve.worlds[self.world].imports[world_key] { @@ -971,18 +969,16 @@ impl<'a> Instantiator<'a, '_> { unused, ); } + } else if let Some(import_binding) = import_binding { + self.gen.esm_bindgen.add_import_binding( + &[import_specifier, import_binding], + local_name, + unused, + ); } else { - if let Some(import_binding) = import_binding { - self.gen.esm_bindgen.add_import_binding( - &[import_specifier, import_binding], - local_name, - unused, - ); - } else { - self.gen - .esm_bindgen - .add_import_binding(&[import_specifier], local_name, unused); - } + self.gen + .esm_bindgen + .add_import_binding(&[import_specifier], local_name, unused); } } @@ -1129,10 +1125,8 @@ impl<'a> Instantiator<'a, '_> { (TypeDefKind::Variant(t1), InterfaceType::Variant(t2)) => { let t2 = &self.types[*t2]; for (f1, f2) in t1.cases.iter().zip(t2.cases.iter()) { - if let Some(t1) = &f1.ty { - if let Type::Id(id) = t1 { - self.connect_resource_types(*id, f2.ty.as_ref().unwrap(), is_exports); - } + if let Some(Type::Id(id)) = &f1.ty { + self.connect_resource_types(*id, f2.ty.as_ref().unwrap(), is_exports); } } } @@ -1144,15 +1138,11 @@ impl<'a> Instantiator<'a, '_> { } (TypeDefKind::Result(t1), InterfaceType::Result(t2)) => { let t2 = &self.types[*t2]; - if let Some(t1) = &t1.ok { - if let Type::Id(id) = t1 { - self.connect_resource_types(*id, &t2.ok.unwrap(), is_exports); - } + if let Some(Type::Id(id)) = &t1.ok { + self.connect_resource_types(*id, &t2.ok.unwrap(), is_exports); } - if let Some(t1) = &t1.err { - if let Type::Id(id) = t1 { - self.connect_resource_types(*id, &t2.err.unwrap(), is_exports); - } + if let Some(Type::Id(id)) = &t1.err { + self.connect_resource_types(*id, &t2.err.unwrap(), is_exports); } } (TypeDefKind::List(t1), InterfaceType::List(t2)) => { @@ -1181,18 +1171,11 @@ impl<'a> Instantiator<'a, '_> { abi: AbiVariant, is_exports: bool, ) { - let memory = match opts.memory { - Some(idx) => Some(format!("memory{}", idx.as_u32())), - None => None, - }; - let realloc = match opts.realloc { - Some(idx) => Some(format!("realloc{}", idx.as_u32())), - None => None, - }; - let post_return = match opts.post_return { - Some(idx) => Some(format!("postReturn{}", idx.as_u32())), - None => None, - }; + let memory = opts.memory.map(|idx| format!("memory{}", idx.as_u32())); + let realloc = opts.realloc.map(|idx| format!("realloc{}", idx.as_u32())); + let post_return = opts + .post_return + .map(|idx| format!("postReturn{}", idx.as_u32())); self.src.js("("); let mut params = Vec::new(); @@ -1630,7 +1613,7 @@ impl Source { fn map_import(map: &Option>, impt: &str) -> (String, Option) { let impt_sans_version = match impt.find('@') { Some(version_idx) => &impt[0..version_idx], - None => impt.as_ref(), + None => impt, }; if let Some(map) = map.as_ref() { if let Some(mapping) = map.get(impt) { @@ -1667,7 +1650,7 @@ fn map_import(map: &Option>, impt: &str) -> (String, Opt Some(mapping[hash_idx + 1..].into()), ) } else { - (mapping.into(), None) + (mapping, None) }; } if impt.starts_with(lhs) && impt.ends_with(rhs) { @@ -1680,7 +1663,7 @@ fn map_import(map: &Option>, impt: &str) -> (String, Opt Some(mapping[hash_idx + 1..].into()), ) } else { - (mapping.into(), None) + (mapping, None) }; } } @@ -1689,7 +1672,7 @@ fn map_import(map: &Option>, impt: &str) -> (String, Opt (impt_sans_version.to_string(), None) } -pub fn parse_world_key<'a>(name: &'a str) -> Option<(&'a str, &'a str, &'a str)> { +pub fn parse_world_key(name: &str) -> Option<(&str, &str, &str)> { let registry_idx = match name.find(':') { Some(idx) => idx, None => return None, @@ -1709,7 +1692,7 @@ pub fn parse_world_key<'a>(name: &'a str) -> Option<(&'a str, &'a str, &'a str)> )) } // interface is a namespace, function is a default export - None => Some((ns, &name[registry_idx + 1..], "".as_ref())), + None => Some((ns, &name[registry_idx + 1..], "")), } } diff --git a/crates/js-component-bindgen/src/ts_bindgen.rs b/crates/js-component-bindgen/src/ts_bindgen.rs index caeb9c558..ffeccfb00 100644 --- a/crates/js-component-bindgen/src/ts_bindgen.rs +++ b/crates/js-component-bindgen/src/ts_bindgen.rs @@ -120,7 +120,7 @@ pub fn ts_bindgen( } // kebab import funcs (always default imports) for (name, func) in funcs { - bindgen.import_funcs(resolve, &name, &func, files); + bindgen.import_funcs(resolve, &name, func, files); } // namespace imports are grouped by namespace / kebab name // kebab name imports are direct @@ -154,7 +154,7 @@ pub fn ts_bindgen( }; seen_names.insert(export_name.to_string()); let local_name = - bindgen.export_interface(resolve, &export_name, *id, files, opts.instantiation); + bindgen.export_interface(resolve, export_name, *id, files, opts.instantiation); export_aliases.push((iface_name.to_lower_camel_case(), local_name)); } WorldItem::Type(_) => unimplemented!("type exports"), @@ -277,11 +277,11 @@ impl TsBindgen { files: &mut Files, ) { if ifaces.len() == 1 { - let (iface_name, &id) = ifaces.iter().next().unwrap(); + 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); + let local_name = self.generate_interface(name, resolve, id, files); uwriteln!(self.import_object, "typeof {local_name},",); return; } @@ -289,7 +289,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); + let local_name = self.generate_interface(name, resolve, id, files); uwriteln!( self.import_object, "{}: typeof {local_name},", @@ -329,18 +329,16 @@ impl TsBindgen { "{}: typeof {local_name},", maybe_quote_id(export_name) ); + } else if export_name != maybe_quote_id(export_name) { + // TODO: TypeScript doesn't currently support + // non-identifier exports + // tracking in https://github.com/microsoft/TypeScript/issues/40594 } else { - if export_name != maybe_quote_id(export_name) { - // TODO: TypeScript doesn't currently support - // non-identifier exports - // tracking in https://github.com/microsoft/TypeScript/issues/40594 - } else { - uwriteln!( - self.export_object, - "export const {}: typeof {local_name};", - maybe_quote_id(export_name) - ); - } + uwriteln!( + self.export_object, + "export const {}: typeof {local_name};", + maybe_quote_id(export_name) + ); } local_name } @@ -372,11 +370,11 @@ impl TsBindgen { let goal_name = interface_goal_name(&id_name); let goal_name_kebab = goal_name.to_kebab_case(); let file_name = &format!("interfaces/{}.d.ts", goal_name_kebab); - let (name, iface_exists) = self.interface_names.get_or_create(&file_name, &goal_name); + let (name, iface_exists) = self.interface_names.get_or_create(file_name, &goal_name); let camel = name.to_upper_camel_case(); - let (local_name, local_exists) = self.local_names.get_or_create(&file_name, &goal_name); + let (local_name, local_exists) = self.local_names.get_or_create(file_name, &goal_name); let local_name = local_name.to_upper_camel_case(); if !local_exists { @@ -404,16 +402,13 @@ impl TsBindgen { } for (_, ty) in resolve.interfaces[id].types.iter() { let ty = &resolve.types[*ty]; - match ty.kind { - TypeDefKind::Resource => { - let resource = ty.name.as_ref().unwrap(); - if !gen.resources.contains_key(resource) { - uwriteln!(gen.src, "export {{ {} }};", resource.to_upper_camel_case()); - gen.resources - .insert(resource.to_string(), TsInterface::new(resolve)); - } + if let TypeDefKind::Resource = ty.kind { + let resource = ty.name.as_ref().unwrap(); + if !gen.resources.contains_key(resource) { + uwriteln!(gen.src, "export {{ {} }};", resource.to_upper_camel_case()); + gen.resources + .insert(resource.to_string(), TsInterface::new(resolve)); } - _ => {} } } uwriteln!(gen.src, "}}"); @@ -472,9 +467,8 @@ impl<'a> TsInterface<'a> { } fn docs(&mut self, docs: &Docs) { - match &docs.contents { - Some(docs) => self.docs_raw(docs), - None => return, + if let Some(docs) = &docs.contents { + self.docs_raw(docs); } } @@ -616,9 +610,6 @@ impl<'a> TsInterface<'a> { self }; - let is_resource = !matches!(func.kind, FunctionKind::Freestanding); - if is_resource {} - let end_character = if declaration { iface.src.push_str(match func.kind { FunctionKind::Freestanding => "export function ", @@ -902,10 +893,9 @@ impl<'a> TsInterface<'a> { fn interface_goal_name(iface_name: &str) -> String { let iface_name_sans_version = match iface_name.find('@') { Some(version_idx) => &iface_name[0..version_idx], - None => iface_name.as_ref(), + None => iface_name, }; iface_name_sans_version - .replace('/', "-") - .replace(':', "-") + .replace(['/', ':'], "-") .to_kebab_case() } diff --git a/crates/wasm-tools-component/src/lib.rs b/crates/wasm-tools-component/src/lib.rs index fc5bc6f6f..d2c81e00c 100644 --- a/crates/wasm-tools-component/src/lib.rs +++ b/crates/wasm-tools-component/src/lib.rs @@ -76,7 +76,7 @@ impl Guest for WasmToolsJs { let pkg = if let Some(wit_source) = &embed_opts.wit_source { let path = PathBuf::from("component.wit"); - UnresolvedPackage::parse(&path, &wit_source).map_err(|e| e.to_string())? + UnresolvedPackage::parse(&path, wit_source).map_err(|e| e.to_string())? } else { let wit_path = &embed_opts.wit_path.as_ref().unwrap(); UnresolvedPackage::parse_file(&PathBuf::from(wit_path)).map_err(|e| e.to_string())? @@ -84,10 +84,7 @@ impl Guest for WasmToolsJs { let id = resolve.push(pkg).map_err(|e| e.to_string())?; - let world_string = match &embed_opts.world { - Some(world) => Some(world.to_string()), - None => None, - }; + let world_string = embed_opts.world.as_ref().map(|world| world.to_string()); let world = resolve .select_world(id, world_string.as_deref()) .map_err(|e| e.to_string())?; @@ -127,7 +124,7 @@ impl Guest for WasmToolsJs { return Err(format!("'{field_name}' is not a valid field to embed in the metadata. Must be one of 'language', 'processed-by' or 'sdk'.")); } for (name, version) in items { - producers.add(&field_name, &name, &version); + producers.add(field_name, name, version); } } Some(producers) diff --git a/xtask/src/build/jco.rs b/xtask/src/build/jco.rs index e0d977f65..6a26cca20 100644 --- a/xtask/src/build/jco.rs +++ b/xtask/src/build/jco.rs @@ -2,7 +2,6 @@ use anyhow::Context; use std::{collections::HashMap, fs, io::Write, path::PathBuf}; use anyhow::Result; -use js_component_bindgen; use wit_component::ComponentEncoder; pub(crate) fn run() -> Result<()> { @@ -19,10 +18,10 @@ pub(crate) fn run() -> Result<()> { } fn transpile(component_path: &str, name: String) -> Result<()> { - let component = fs::read(&component_path).context("wasm bindgen component missing")?; + let component = fs::read(component_path).context("wasm bindgen component missing")?; let adapter_path = "lib/wasi_snapshot_preview1.reactor.wasm"; - let adapter = fs::read(&adapter_path).context("preview1 adapter file missing")?; + let adapter = fs::read(adapter_path).context("preview1 adapter file missing")?; let mut encoder = ComponentEncoder::default() .validate(true) diff --git a/xtask/src/generate/tests.rs b/xtask/src/generate/tests.rs index 26acd390f..84f0f2675 100644 --- a/xtask/src/generate/tests.rs +++ b/xtask/src/generate/tests.rs @@ -19,7 +19,7 @@ pub fn run() -> anyhow::Result<()> { let entry = entry?; // skip all files which don't end with `.wasm` - if entry.path().extension().map(|p| p.to_str()).flatten() != Some("wasm") { + if entry.path().extension().and_then(|p| p.to_str()) != Some("wasm") { continue; } @@ -33,12 +33,12 @@ pub fn run() -> anyhow::Result<()> { test_names.push(test_name.to_owned()); let content = generate_test(test_name); let file_name = format!("tests/generated/{test_name}.rs"); - fs::write(&file_name, content)?; + fs::write(file_name, content)?; } let content = generate_mod(test_names.as_slice()); - let file_name = format!("tests/generated/mod.rs"); - fs::write(&file_name, content)?; + let file_name = "tests/generated/mod.rs"; + fs::write(file_name, content)?; println!("generated {} tests", test_names.len()); Ok(()) } @@ -67,8 +67,12 @@ fn {test_name}() -> anyhow::Result<()> {{ /// Generate the mod.rs file containing all tests fn generate_mod(test_names: &[String]) -> String { + use std::fmt::Write; + test_names - .into_iter() - .map(|t| format!("mod {t};\n")) - .collect() + .iter() + .fold(String::new(), |mut output, test_name| { + let _ = write!(output, "mod {test_name};\n"); + output + }) } diff --git a/xtask/src/update/preview2.rs b/xtask/src/update/preview2.rs index 80bf50513..8d42645b2 100644 --- a/xtask/src/update/preview2.rs +++ b/xtask/src/update/preview2.rs @@ -4,7 +4,7 @@ use xshell::{cmd, Shell}; pub(crate) fn run() -> anyhow::Result<()> { let sh = Shell::new()?; update_submodules(&sh)?; - let _guard = sh.change_dir("submodules/wasmtime"); + sh.change_dir("submodules/wasmtime"); // Build the artifacts cmd!(sh, "./ci/build-wasi-preview1-component-adapter.sh").run()?;