From 9c28fe6c0002bf1eccc9ddfbe2676246c7b7e2fc Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 26 May 2023 22:52:25 +0200 Subject: [PATCH 01/23] silence large_enum_variant --- packages/utils/node-resolver-rs/src/cache.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/utils/node-resolver-rs/src/cache.rs b/packages/utils/node-resolver-rs/src/cache.rs index e1a69c17886..101d1ac75be 100644 --- a/packages/utils/node-resolver-rs/src/cache.rs +++ b/packages/utils/node-resolver-rs/src/cache.rs @@ -17,12 +17,12 @@ use typed_arena::Arena; pub struct Cache { pub fs: Fs, - // This stores file content strings, which are borrowed when parsing package.json and tsconfig.json files. + /// This stores file content strings, which are borrowed when parsing package.json and tsconfig.json files. arena: Mutex>>, - // These map paths to parsed config files. They aren't really 'static, but Rust doens't have a good - // way to associate a lifetime with owned data stored in the same struct. We only vend temporary references - // from our public methods so this is ok for now. FrozenMap is an append only map, which doesn't require &mut - // to insert into. Since each value is in a Box, it won't move and therefore references are stable. + /// These map paths to parsed config files. They aren't really 'static, but Rust doens't have a good + /// way to associate a lifetime with owned data stored in the same struct. We only vend temporary references + /// from our public methods so this is ok for now. FrozenMap is an append only map, which doesn't require &mut + /// to insert into. Since each value is in a Box, it won't move and therefore references are stable. packages: FrozenMap, ResolverError>>>, tsconfigs: FrozenMap, ResolverError>>>, is_file_cache: DashMap, @@ -30,7 +30,8 @@ pub struct Cache { realpath_cache: DashMap>, } -// Special Cow implementation for a Cache that doesn't require Clone. +#[allow(clippy::large_enum_variant)] +/// Special Cow implementation for a Cache that doesn't require Clone. pub enum CacheCow<'a, Fs> { Borrowed(&'a Cache), Owned(Cache), From 7af5268b74fcb4e619e5e05c543c7d9c26e11ff0 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 26 May 2023 22:52:36 +0200 Subject: [PATCH 02/23] never_loop --- packages/utils/node-resolver-rs/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/node-resolver-rs/src/lib.rs b/packages/utils/node-resolver-rs/src/lib.rs index afbef2ef165..8173b4a9f33 100644 --- a/packages/utils/node-resolver-rs/src/lib.rs +++ b/packages/utils/node-resolver-rs/src/lib.rs @@ -762,7 +762,7 @@ impl<'a, Fs: FileSystem> ResolveRequest<'a, Fs> { package: &PackageJson, ) -> Result, ResolverError> { // Try all entry fields. - for (entry, field) in package.entries(self.resolver.entries) { + if let Some((entry, field)) = package.entries(self.resolver.entries).next() { if let Some(res) = self.load_path(&entry, Some(package))? { return Ok(Some(res)); } else { From 6548db098bd8b045179de0e09085e77dff127fbd Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 26 May 2023 22:53:21 +0200 Subject: [PATCH 03/23] needless_borrow --- crates/node-bindings/src/resolver.rs | 2 +- packages/utils/dev-dep-resolver/src/lib.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/node-bindings/src/resolver.rs b/crates/node-bindings/src/resolver.rs index 819ae1d9fec..99089c003ed 100644 --- a/crates/node-bindings/src/resolver.rs +++ b/crates/node-bindings/src/resolver.rs @@ -360,7 +360,7 @@ impl Resolver { if self.mode == 2 { if let Ok((Resolution::Path(p), _)) = &res.result { - module_type = match self.resolver.resolve_module_type(&p, &res.invalidations) { + module_type = match self.resolver.resolve_module_type(p, &res.invalidations) { Ok(t) => match t { ModuleType::CommonJs | ModuleType::Json => 1, ModuleType::Module => 2, diff --git a/packages/utils/dev-dep-resolver/src/lib.rs b/packages/utils/dev-dep-resolver/src/lib.rs index 1df843af614..afe8dc27f08 100644 --- a/packages/utils/dev-dep-resolver/src/lib.rs +++ b/packages/utils/dev-dep-resolver/src/lib.rs @@ -103,7 +103,7 @@ impl<'a, Fs: FileSystem> EsmGraphBuilder<'a, Fs> { ModuleType::CommonJs | ModuleType::Json => &self.cjs_resolver, ModuleType::Module => &self.esm_resolver, }; - let contents = resolver.cache.fs.read_to_string(&file)?; + let contents = resolver.cache.fs.read_to_string(file)?; let module = lex(&contents)?; module .imports() @@ -113,7 +113,7 @@ impl<'a, Fs: FileSystem> EsmGraphBuilder<'a, Fs> { ImportKind::DynamicExpression => { if let Some(glob) = specifier_to_glob(&import.specifier()) { // println!("GLOB {:?} {:?}", import.specifier(), glob); - self.expand_glob(&glob, file, &resolver, &invalidations)?; + self.expand_glob(&glob, file, resolver, &invalidations)?; } else { // println!("DYNAMIC: {} {:?}", import.specifier(), file); invalidations.invalidate_on_startup(); @@ -127,7 +127,7 @@ impl<'a, Fs: FileSystem> EsmGraphBuilder<'a, Fs> { match resolver.resolve_with_invalidations( &import.specifier(), - &file, + file, SpecifierType::Esm, &invalidations, ResolveOptions::default(), @@ -492,12 +492,12 @@ pub fn build_esm_graph<'a, Fs: FileSystem>( visited_globs: DashSet::new(), invalidations: Invalidations::default(), cjs_resolver: Resolver::node( - Cow::Borrowed(&project_root), - CacheCow::Borrowed(&resolver_cache), + Cow::Borrowed(project_root), + CacheCow::Borrowed(resolver_cache), ), esm_resolver: Resolver::node_esm( - Cow::Borrowed(&project_root), - CacheCow::Borrowed(&resolver_cache), + Cow::Borrowed(project_root), + CacheCow::Borrowed(resolver_cache), ), cache, }; From d737c43382657dd632682e31773c8d2583acba99 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 26 May 2023 22:53:56 +0200 Subject: [PATCH 04/23] println_empty_string --- packages/utils/dev-dep-resolver/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/dev-dep-resolver/src/main.rs b/packages/utils/dev-dep-resolver/src/main.rs index 15f6bd3454a..d70eef34952 100644 --- a/packages/utils/dev-dep-resolver/src/main.rs +++ b/packages/utils/dev-dep-resolver/src/main.rs @@ -39,6 +39,6 @@ fn main() { } #[cfg(debug_assertions)] - println!(""); + println!(); }); } From 4542cc8c9ffef7bb71cbfc1745d3024cd61ea808 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 26 May 2023 22:54:18 +0200 Subject: [PATCH 05/23] extra_unused_lifetimes --- packages/utils/dev-dep-resolver/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/dev-dep-resolver/src/lib.rs b/packages/utils/dev-dep-resolver/src/lib.rs index afe8dc27f08..ec88eae1745 100644 --- a/packages/utils/dev-dep-resolver/src/lib.rs +++ b/packages/utils/dev-dep-resolver/src/lib.rs @@ -481,7 +481,7 @@ pub fn resolve_path, B: AsRef>(base: A, subpath: B) -> Path ret } -pub fn build_esm_graph<'a, Fs: FileSystem>( +pub fn build_esm_graph( file: &Path, project_root: &Path, resolver_cache: &parcel_resolver::Cache, From 9f4a56394bf4cd484c92382bfd51637a79efbaad Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 26 May 2023 22:54:49 +0200 Subject: [PATCH 06/23] needless_lifetimes --- packages/utils/dev-dep-resolver/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/dev-dep-resolver/src/lib.rs b/packages/utils/dev-dep-resolver/src/lib.rs index ec88eae1745..627f715eedb 100644 --- a/packages/utils/dev-dep-resolver/src/lib.rs +++ b/packages/utils/dev-dep-resolver/src/lib.rs @@ -360,7 +360,7 @@ fn read_template_string(mut bytes: &[u8]) -> Option<(Cow<'_, str>, &[u8])> { None } -fn escape_glob<'a>(s: &'a str) -> Cow<'a, str> { +fn escape_glob(s: &str) -> Cow<'_, str> { let mut result = Cow::Borrowed(""); let mut start = 0; for (index, matched) in s.match_indices(&['*', '?', '[', ']', '{', '}', '(', ')', '!', '\\']) { From ecd780806c30e65083a40890148583fe681b5fbb Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 26 May 2023 22:56:14 +0200 Subject: [PATCH 07/23] single_match --- packages/utils/dev-dep-resolver/src/lib.rs | 24 ++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/utils/dev-dep-resolver/src/lib.rs b/packages/utils/dev-dep-resolver/src/lib.rs index 627f715eedb..68b8096662b 100644 --- a/packages/utils/dev-dep-resolver/src/lib.rs +++ b/packages/utils/dev-dep-resolver/src/lib.rs @@ -125,27 +125,25 @@ impl<'a, Fs: FileSystem> EsmGraphBuilder<'a, Fs> { return Ok(()); } - match resolver.resolve_with_invalidations( + if let Ok((Resolution::Path(p), _)) = resolver.resolve_with_invalidations( &import.specifier(), file, SpecifierType::Esm, &invalidations, ResolveOptions::default(), ) { - Ok((Resolution::Path(p), _)) => { - // println!( - // "IMPORT {} {:?} {:?} {:?}", - // import.specifier(), - // import.kind(), - // file, - // p - // ); - invalidations.invalidate_on_file_change(&p); - self.build(&p)?; - } + // println!( + // "IMPORT {} {:?} {:?} {:?}", + // import.specifier(), + // import.kind(), + // file, + // p + // ); + invalidations.invalidate_on_file_change(&p); + self.build(&p)?; + } else { // Ignore dependencies that don't resolve to anything. // The resolver calls invalidate_on_file_create already. - _ => {} } } ImportKind::Meta => {} From 90dc1d63525438ebf5589db316a11032f1bd7c51 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 26 May 2023 22:57:53 +0200 Subject: [PATCH 08/23] silence map_collect_result_unit --- packages/utils/dev-dep-resolver/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/utils/dev-dep-resolver/src/lib.rs b/packages/utils/dev-dep-resolver/src/lib.rs index 68b8096662b..13bee264c89 100644 --- a/packages/utils/dev-dep-resolver/src/lib.rs +++ b/packages/utils/dev-dep-resolver/src/lib.rs @@ -105,6 +105,7 @@ impl<'a, Fs: FileSystem> EsmGraphBuilder<'a, Fs> { }; let contents = resolver.cache.fs.read_to_string(file)?; let module = lex(&contents)?; + #[allow(clippy::map_collect_result_unit)] module .imports() // .par_bridge() From 87673b2249ddb4f9232dee1c5e196ab29020f5d9 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 26 May 2023 23:00:43 +0200 Subject: [PATCH 09/23] Use Either3 --- crates/node-bindings/src/resolver.rs | 46 ++++++++++------------------ 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/crates/node-bindings/src/resolver.rs b/crates/node-bindings/src/resolver.rs index 99089c003ed..ec7a4110350 100644 --- a/crates/node-bindings/src/resolver.rs +++ b/crates/node-bindings/src/resolver.rs @@ -1,5 +1,8 @@ use dashmap::DashMap; -use napi::{Env, JsBoolean, JsBuffer, JsFunction, JsObject, JsString, JsUnknown, Ref, Result}; +use napi::{ + bindgen_prelude::Either3, Env, JsBoolean, JsBuffer, JsFunction, JsObject, JsString, JsUnknown, + Ref, Result, +}; use napi_derive::napi; #[cfg(not(target_arch = "wasm32"))] use std::sync::atomic::Ordering; @@ -17,7 +20,7 @@ use parcel_resolver::{ IncludeNodeModules, Invalidations, ModuleType, Resolution, ResolverError, SpecifierType, }; -type NapiSideEffectsVariants = napi::Either, HashMap>>; +type NapiSideEffectsVariants = Either3, HashMap>; #[napi(object)] pub struct JsFileSystemOptions { @@ -197,12 +200,8 @@ pub struct GlobCreateInvalidation { pub struct ResolveResult { pub resolution: JsUnknown, pub invalidate_on_file_change: Vec, - pub invalidate_on_file_create: Vec< - napi::Either< - FilePathCreateInvalidation, - napi::Either, - >, - >, + pub invalidate_on_file_create: + Vec>, pub query: Option, pub side_effects: bool, pub error: JsUnknown, @@ -212,12 +211,8 @@ pub struct ResolveResult { #[napi(object)] pub struct JsInvalidations { pub invalidate_on_file_change: Vec, - pub invalidate_on_file_create: Vec< - napi::Either< - FilePathCreateInvalidation, - napi::Either, - >, - >, + pub invalidate_on_file_create: + Vec>, pub invalidate_on_startup: bool, } @@ -272,9 +267,9 @@ impl Resolver { if let Some(include_node_modules) = options.include_node_modules { resolver.include_node_modules = Cow::Owned(match include_node_modules { - napi::Either::A(b) => IncludeNodeModules::Bool(b), - napi::Either::B(napi::Either::A(v)) => IncludeNodeModules::Array(v), - napi::Either::B(napi::Either::B(v)) => IncludeNodeModules::Map(v), + Either3::A(b) => IncludeNodeModules::Bool(b), + Either3::B(v) => IncludeNodeModules::Array(v), + Either3::C(v) => IncludeNodeModules::Map(v), }); } @@ -483,12 +478,7 @@ fn convert_invalidations( invalidations: Invalidations, ) -> ( Vec, - Vec< - napi::Either< - FilePathCreateInvalidation, - napi::Either, - >, - >, + Vec>, ) { let invalidate_on_file_change = invalidations .invalidate_on_file_change @@ -499,18 +489,16 @@ fn convert_invalidations( .invalidate_on_file_create .into_iter() .map(|i| match i { - FileCreateInvalidation::Path(p) => napi::Either::A(FilePathCreateInvalidation { + FileCreateInvalidation::Path(p) => Either3::A(FilePathCreateInvalidation { file_path: p.to_string_lossy().into_owned(), }), FileCreateInvalidation::FileName { file_name, above } => { - napi::Either::B(napi::Either::A(FileNameCreateInvalidation { + Either3::B(FileNameCreateInvalidation { file_name, above_file_path: above.to_string_lossy().into_owned(), - })) - } - FileCreateInvalidation::Glob(glob) => { - napi::Either::B(napi::Either::B(GlobCreateInvalidation { glob })) + }) } + FileCreateInvalidation::Glob(glob) => Either3::C(GlobCreateInvalidation { glob }), }) .collect(); (invalidate_on_file_change, invalidate_on_file_create) From 686f42bfe41d8288cd338b9562fadcf8736405cf Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Thu, 14 Sep 2023 15:25:07 +0200 Subject: [PATCH 10/23] default_constructed_unit_structs --- packages/utils/dev-dep-resolver/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/dev-dep-resolver/src/main.rs b/packages/utils/dev-dep-resolver/src/main.rs index d70eef34952..c0d655caf9a 100644 --- a/packages/utils/dev-dep-resolver/src/main.rs +++ b/packages/utils/dev-dep-resolver/src/main.rs @@ -9,7 +9,7 @@ fn main() { let deps = pkg.get("dependencies").unwrap().as_object().unwrap(); let cwd = std::env::current_dir().unwrap(); - let cache = Cache::new(OsFileSystem::default()); + let cache = Cache::new(OsFileSystem); let cjs_resolver = Resolver::node(Cow::Borrowed(&cwd), CacheCow::Borrowed(&cache)); let esm_graph_cache = parcel_dev_dep_resolver::Cache::default(); From 05ea2f8267207594e17cdd90daa848894d688afc Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:04:21 +0200 Subject: [PATCH 11/23] `#[napi]` is not dead code --- crates/node-bindings/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/node-bindings/src/lib.rs b/crates/node-bindings/src/lib.rs index 0388f007385..e79c2e768df 100644 --- a/crates/node-bindings/src/lib.rs +++ b/crates/node-bindings/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + #[cfg(target_arch = "wasm32")] use std::alloc::{alloc, Layout}; From 10925f0930b106d2b6173b107c78dd4fb0b9fef4 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:05:19 +0200 Subject: [PATCH 12/23] default_constructed_unit_structs --- packages/utils/node-resolver-rs/src/lib.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/utils/node-resolver-rs/src/lib.rs b/packages/utils/node-resolver-rs/src/lib.rs index 8173b4a9f33..288a7e1c5d3 100644 --- a/packages/utils/node-resolver-rs/src/lib.rs +++ b/packages/utils/node-resolver-rs/src/lib.rs @@ -1203,17 +1203,11 @@ mod tests { } fn test_resolver<'a>() -> Resolver<'a, OsFileSystem> { - Resolver::parcel( - root().into(), - CacheCow::Owned(Cache::new(OsFileSystem::default())), - ) + Resolver::parcel(root().into(), CacheCow::Owned(Cache::new(OsFileSystem))) } fn node_resolver<'a>() -> Resolver<'a, OsFileSystem> { - Resolver::node( - root().into(), - CacheCow::Owned(Cache::new(OsFileSystem::default())), - ) + Resolver::node(root().into(), CacheCow::Owned(Cache::new(OsFileSystem))) } #[test] From ca3e14d8a9fa8d417cc84784f63087105f8bbfc2 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:07:01 +0200 Subject: [PATCH 13/23] Allow single_range_in_vec_init --- packages/utils/node-resolver-rs/src/package_json.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/utils/node-resolver-rs/src/package_json.rs b/packages/utils/node-resolver-rs/src/package_json.rs index 6d9b0d53c71..9eec6f1c45b 100644 --- a/packages/utils/node-resolver-rs/src/package_json.rs +++ b/packages/utils/node-resolver-rs/src/package_json.rs @@ -1527,6 +1527,7 @@ mod tests { ); } + #[allow(clippy::single_range_in_vec_init)] #[test] fn test_replace_captures() { assert_eq!( From 79d8ae436d2e0a129ead957e91c79c5945669ced Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:07:08 +0200 Subject: [PATCH 14/23] .is_err --- packages/utils/node-resolver-rs/src/path.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/utils/node-resolver-rs/src/path.rs b/packages/utils/node-resolver-rs/src/path.rs index 9dc70aa129a..f978151df0b 100644 --- a/packages/utils/node-resolver-rs/src/path.rs +++ b/packages/utils/node-resolver-rs/src/path.rs @@ -196,10 +196,7 @@ mod test { canonicalize(dir.child("recursive").path(), &cache)?, canonicalize(dir.child("root.js").path(), &cache)? ); - assert!(matches!( - canonicalize(dir.child("cycle").path(), &cache), - Err(_) - )); + assert!(canonicalize(dir.child("cycle").path(), &cache).is_err()); assert_eq!( canonicalize(dir.child("a/b/e/d/a/b/e/d/a").path(), &cache)?, canonicalize(dir.child("a").path(), &cache)? From 0fb92b666eb851d8a39a926be5c10b2b7c1597c8 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:08:09 +0200 Subject: [PATCH 15/23] assert_eq boolean --- .../js/core/src/constant_module.rs | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/transformers/js/core/src/constant_module.rs b/packages/transformers/js/core/src/constant_module.rs index 57439567464..e0220b3fa9b 100644 --- a/packages/transformers/js/core/src/constant_module.rs +++ b/packages/transformers/js/core/src/constant_module.rs @@ -183,7 +183,7 @@ mod tests { "#, ); - assert_eq!(result, true); + assert!(result); } #[test] @@ -194,7 +194,7 @@ mod tests { "#, ); - assert_eq!(result, true); + assert!(result); } #[test] @@ -205,7 +205,7 @@ mod tests { "#, ); - assert_eq!(result, true); + assert!(result); } #[test] @@ -216,7 +216,7 @@ mod tests { "#, ); - assert_eq!(result, true); + assert!(result); } #[test] @@ -227,7 +227,7 @@ mod tests { "#, ); - assert_eq!(result, true); + assert!(result); } #[test] @@ -239,7 +239,7 @@ mod tests { "#, ); - assert_eq!(result, true); + assert!(result); } #[test] @@ -250,7 +250,7 @@ mod tests { "#, ); - assert_eq!(result, true); + assert!(result); } #[test] @@ -262,7 +262,7 @@ mod tests { "#, ); - assert_eq!(result, true); + assert!(result); } #[test] @@ -273,7 +273,7 @@ mod tests { "#, ); - assert_eq!(result, true); + assert!(result); } #[test] @@ -284,7 +284,7 @@ mod tests { "#, ); - assert_eq!(result, false); + assert!(!result); } #[test] @@ -296,7 +296,7 @@ mod tests { "#, ); - assert_eq!(result, false); + assert!(!result); } #[test] @@ -308,7 +308,7 @@ mod tests { "#, ); - assert_eq!(result, false); + assert!(!result); } #[test] @@ -319,7 +319,7 @@ mod tests { "#, ); - assert_eq!(result, false); + assert!(!result); } #[test] @@ -330,7 +330,7 @@ mod tests { "#, ); - assert_eq!(result, false); + assert!(!result); } #[test] @@ -341,7 +341,7 @@ mod tests { "#, ); - assert_eq!(result, false); + assert!(!result); } #[test] @@ -352,13 +352,13 @@ mod tests { "#, ); - assert_eq!(result, false); + assert!(!result); } #[test] fn empty_file() { let result = is_constant_module(r#""#); - assert_eq!(result, false); + assert!(!result); } } From dd268413e164688b264430cdbebcb5767ca80c3e Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:08:26 +0200 Subject: [PATCH 16/23] is_empty --- packages/transformers/js/core/src/constant_module.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/transformers/js/core/src/constant_module.rs b/packages/transformers/js/core/src/constant_module.rs index e0220b3fa9b..25c26c88138 100644 --- a/packages/transformers/js/core/src/constant_module.rs +++ b/packages/transformers/js/core/src/constant_module.rs @@ -93,7 +93,7 @@ impl ConstantModule { impl Visit for ConstantModule { fn visit_module(&mut self, module: &Module) { - if module.body.len() == 0 { + if module.body.is_empty() { // Empty modules should not be marked as constant modules self.is_constant_module = false; return; From 7de056b464fec6620eae204de0f6e7500cf3412e Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:09:06 +0200 Subject: [PATCH 17/23] needless_borrow --- packages/transformers/js/core/src/constant_module.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/transformers/js/core/src/constant_module.rs b/packages/transformers/js/core/src/constant_module.rs index 25c26c88138..e96d897fa7f 100644 --- a/packages/transformers/js/core/src/constant_module.rs +++ b/packages/transformers/js/core/src/constant_module.rs @@ -34,13 +34,13 @@ impl ConstantModule { if let Some(init) = &decl.init { match &**init { Expr::Lit(lit) => { - return is_safe_literal(&lit); + return is_safe_literal(lit); } Expr::Tpl(tpl) => { for expr in &tpl.exprs { match &**expr { Expr::Lit(lit) => { - if !is_safe_literal(&lit) { + if !is_safe_literal(lit) { return false; } } @@ -73,7 +73,7 @@ impl ConstantModule { } for declarator in &var_decl.decls { - if !self.is_constant_declarator(&declarator) { + if !self.is_constant_declarator(declarator) { return false; } @@ -117,7 +117,7 @@ impl Visit for ConstantModule { }, ModuleItem::Stmt(stmt) => match stmt { Stmt::Decl(decl) => { - let result = self.is_constant_declaration(&decl); + let result = self.is_constant_declaration(decl); if !result { self.is_constant_module = false; From d9390cc82e3b8c63eabe572b9772fdf27d5d889d Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:09:49 +0200 Subject: [PATCH 18/23] needless_return --- .../transformers/js/core/src/constant_module.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/transformers/js/core/src/constant_module.rs b/packages/transformers/js/core/src/constant_module.rs index e96d897fa7f..933d7214fbd 100644 --- a/packages/transformers/js/core/src/constant_module.rs +++ b/packages/transformers/js/core/src/constant_module.rs @@ -33,9 +33,7 @@ impl ConstantModule { fn is_constant_declarator(&mut self, decl: &VarDeclarator) -> bool { if let Some(init) = &decl.init { match &**init { - Expr::Lit(lit) => { - return is_safe_literal(lit); - } + Expr::Lit(lit) => is_safe_literal(lit), Expr::Tpl(tpl) => { for expr in &tpl.exprs { match &**expr { @@ -55,14 +53,12 @@ impl ConstantModule { } } - return true; - } - _ => { - return false; + true } + _ => false, } } else { - return true; + true } } @@ -84,9 +80,9 @@ impl ConstantModule { } } - return true; + true } else { - return false; + false } } } From efab937a644b5f829e6a64f196cdf3e2e36f30f1 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:10:23 +0200 Subject: [PATCH 19/23] simplify --- packages/transformers/js/core/src/constant_module.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/transformers/js/core/src/constant_module.rs b/packages/transformers/js/core/src/constant_module.rs index 933d7214fbd..759e1130c4d 100644 --- a/packages/transformers/js/core/src/constant_module.rs +++ b/packages/transformers/js/core/src/constant_module.rs @@ -7,14 +7,10 @@ use swc_core::ecma::atoms::JsWord; use swc_core::ecma::visit::Visit; fn is_safe_literal(lit: &Lit) -> bool { - match lit { - Lit::Str(..) | Lit::Bool(..) | Lit::BigInt(..) | Lit::Null(..) | Lit::Num(..) => { - return true; - } - _ => { - return false; - } - } + matches!( + lit, + Lit::Str(..) | Lit::Bool(..) | Lit::BigInt(..) | Lit::Null(..) | Lit::Num(..) + ) } pub struct ConstantModule { From 444a178ff6bbecdf76d6ff3c038effa34ddb44fc Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Mon, 16 Oct 2023 11:22:42 +0200 Subject: [PATCH 20/23] needlessly taken reference of left operand --- packages/transformers/js/core/src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/transformers/js/core/src/utils.rs b/packages/transformers/js/core/src/utils.rs index c234aaedefb..e96863adf6d 100644 --- a/packages/transformers/js/core/src/utils.rs +++ b/packages/transformers/js/core/src/utils.rs @@ -33,7 +33,7 @@ pub fn match_member_expr(expr: &ast::MemberExpr, idents: Vec<&str>, decls: &Hash match &*member.obj { Expr::Member(m) => member = m, Expr::Ident(id) => { - return idents.len() == 1 && &id.sym == idents.pop().unwrap() && !decls.contains(&id!(id)); + return idents.len() == 1 && id.sym == idents.pop().unwrap() && !decls.contains(&id!(id)); } _ => return false, } From 6996fa2664751eaf803fedf67e3d8c7119efd8cd Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Sat, 6 Jan 2024 19:31:13 +0100 Subject: [PATCH 21/23] get_first --- packages/transformers/js/core/src/collect.rs | 6 ++--- .../js/core/src/dependency_collector.rs | 22 +++++++++---------- packages/transformers/js/core/src/fs.rs | 2 +- packages/transformers/js/core/src/utils.rs | 6 ++--- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/transformers/js/core/src/collect.rs b/packages/transformers/js/core/src/collect.rs index 804d0a5c427..bf5f823ac1b 100644 --- a/packages/transformers/js/core/src/collect.rs +++ b/packages/transformers/js/core/src/collect.rs @@ -919,10 +919,10 @@ impl Visit for Collect { // import('foo').then(foo => ...); if let Some(source) = match_import(&member.obj, self.ignore_mark) { if match_property_name(member).map_or(false, |f| &*f.0 == "then") { - if let Some(ExprOrSpread { expr, .. }) = node.args.get(0) { + if let Some(ExprOrSpread { expr, .. }) = node.args.first() { let param = match &**expr { - Expr::Fn(func) => func.function.params.get(0).map(|param| ¶m.pat), - Expr::Arrow(arrow) => arrow.params.get(0), + Expr::Fn(func) => func.function.params.first().map(|param| ¶m.pat), + Expr::Arrow(arrow) => arrow.params.first(), _ => None, }; diff --git a/packages/transformers/js/core/src/dependency_collector.rs b/packages/transformers/js/core/src/dependency_collector.rs index 62bb544afb0..cfe43890145 100644 --- a/packages/transformers/js/core/src/dependency_collector.rs +++ b/packages/transformers/js/core/src/dependency_collector.rs @@ -238,7 +238,7 @@ impl<'a> DependencyCollector<'a> { } fn rewrite_require_specifier(node: ast::CallExpr) -> ast::CallExpr { - if let Some(arg) = node.args.get(0) { + if let Some(arg) = node.args.first() { if let Some((value, _)) = match_str(&arg.expr) { if value.starts_with("node:") { // create_require will take care of replacing the node: prefix... @@ -384,7 +384,7 @@ impl<'a> Fold for DependencyCollector<'a> { if self.config.is_worker { let (msg, span) = if self.config.source_type == SourceType::Script { // Ignore if argument is not a string literal. - let span = if let Some(ast::ExprOrSpread { expr, .. }) = node.args.get(0) { + let span = if let Some(ast::ExprOrSpread { expr, .. }) = node.args.first() { match &**expr { Lit(ast::Lit::Str(ast::Str { value, span, .. })) => { // Ignore absolute URLs. @@ -481,7 +481,7 @@ impl<'a> Fold for DependencyCollector<'a> { // Match compiled dynamic imports (Parcel) // Promise.resolve(require('foo')) if match_member_expr(member, vec!["Promise", "resolve"], self.decls) { - if let Some(expr) = node.args.get(0) { + if let Some(expr) = node.args.first() { if match_require(&expr.expr, self.decls, Mark::fresh(Mark::root())).is_some() { self.in_promise = true; let node = node.fold_children_with(self); @@ -508,7 +508,7 @@ impl<'a> Fold for DependencyCollector<'a> { { if let MemberProp::Ident(id) = &member.prop { if id.sym.to_string().as_str() == "then" { - if let Some(arg) = node.args.get(0) { + if let Some(arg) = node.args.first() { match &*arg.expr { Fn(_) | Arrow(_) => { self.in_promise = true; @@ -578,7 +578,7 @@ impl<'a> Fold for DependencyCollector<'a> { } } - let node = if let Some(arg) = node.args.get(0) { + let node = if let Some(arg) = node.args.first() { if kind == DependencyKind::ServiceWorker || kind == DependencyKind::Worklet { let (source_type, opts) = if kind == DependencyKind::ServiceWorker { match_worker_type(node.args.get(1)) @@ -888,10 +888,10 @@ impl<'a> DependencyCollector<'a> { // new Promise(function (resolve) { resolve(require('foo')) }) // new Promise(function (resolve) { return resolve(require('foo')) }) if let Some(args) = &node.args { - if let Some(arg) = args.get(0) { + if let Some(arg) = args.first() { let (resolve, expr) = match &*arg.expr { Fn(f) => { - let param = f.function.params.get(0).map(|param| ¶m.pat); + let param = f.function.params.first().map(|param| ¶m.pat); let body = if let Some(body) = &f.function.body { self.match_block_stmt_expr(body) } else { @@ -900,7 +900,7 @@ impl<'a> DependencyCollector<'a> { (param, body) } Arrow(f) => { - let param = f.params.get(0); + let param = f.params.first(); let body = match &*f.body { ast::BlockStmtOrExpr::Expr(expr) => Some(&**expr), ast::BlockStmtOrExpr::BlockStmt(block) => self.match_block_stmt_expr(block), @@ -919,7 +919,7 @@ impl<'a> DependencyCollector<'a> { if let ast::Callee::Expr(callee) = &call.callee { if let ast::Expr::Ident(id) = &**callee { if id.to_id() == resolve_id { - if let Some(arg) = call.args.get(0) { + if let Some(arg) = call.args.first() { if match_require(&arg.expr, self.decls, Mark::fresh(Mark::root())).is_some() { let was_in_promise = self.in_promise; self.in_promise = true; @@ -968,7 +968,7 @@ fn build_promise_chain(node: ast::CallExpr, require_node: ast::CallExpr) -> ast: let node = node.fold_with(&mut transformer); if let Some(require_node) = &transformer.require_node { - if let Some(f) = node.args.get(0) { + if let Some(f) = node.args.first() { // Add `res` as an argument to the original function let f = match &*f.expr { ast::Expr::Fn(f) => { @@ -1154,7 +1154,7 @@ impl<'a> DependencyCollector<'a> { } if let Some(args) = &new.args { - let (specifier, span) = if let Some(arg) = args.get(0) { + let (specifier, span) = if let Some(arg) = args.first() { match_str(&arg.expr)? } else { return None; diff --git a/packages/transformers/js/core/src/fs.rs b/packages/transformers/js/core/src/fs.rs index 83cc9b030c4..9814fc72fc3 100644 --- a/packages/transformers/js/core/src/fs.rs +++ b/packages/transformers/js/core/src/fs.rs @@ -54,7 +54,7 @@ impl<'a> Fold for InlineFS<'a> { if let Callee::Expr(expr) = &call.callee { if let Some((source, specifier)) = self.match_module_reference(expr) { if &source == "fs" && &specifier == "readFileSync" { - if let Some(arg) = call.args.get(0) { + if let Some(arg) = call.args.first() { if let Some(res) = self.evaluate_fs_arg(&arg.expr, call.args.get(1), call.span) { return res; } diff --git a/packages/transformers/js/core/src/utils.rs b/packages/transformers/js/core/src/utils.rs index e96863adf6d..f9b504caab0 100644 --- a/packages/transformers/js/core/src/utils.rs +++ b/packages/transformers/js/core/src/utils.rs @@ -125,7 +125,7 @@ pub fn match_require(node: &ast::Expr, decls: &HashSet, ignore_mark: Mark) - && !decls.contains(&(ident.sym.clone(), ident.span.ctxt)) && !is_marked(ident.span, ignore_mark) { - if let Some(arg) = call.args.get(0) { + if let Some(arg) = call.args.first() { return match_str(&arg.expr).map(|(name, _)| name); } } @@ -134,7 +134,7 @@ pub fn match_require(node: &ast::Expr, decls: &HashSet, ignore_mark: Mark) - } Expr::Member(member) => { if match_member_expr(member, vec!["module", "require"], decls) { - if let Some(arg) = call.args.get(0) { + if let Some(arg) = call.args.first() { return match_str(&arg.expr).map(|(name, _)| name); } } @@ -155,7 +155,7 @@ pub fn match_import(node: &ast::Expr, ignore_mark: Mark) -> Option { match node { Expr::Call(call) => match &call.callee { Callee::Import(ident) if !is_marked(ident.span, ignore_mark) => { - if let Some(arg) = call.args.get(0) { + if let Some(arg) = call.args.first() { return match_str(&arg.expr).map(|(name, _)| name); } None From ed873322d86b103b5cf1a51188123d387dccbf18 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Sat, 6 Jan 2024 19:33:03 +0100 Subject: [PATCH 22/23] Allow items_after_test_module --- packages/utils/node-resolver-rs/src/url_to_path.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/utils/node-resolver-rs/src/url_to_path.rs b/packages/utils/node-resolver-rs/src/url_to_path.rs index 5f01c13a44d..bb4a2df7a3f 100644 --- a/packages/utils/node-resolver-rs/src/url_to_path.rs +++ b/packages/utils/node-resolver-rs/src/url_to_path.rs @@ -1,4 +1,5 @@ //! An implementation url.to_file_path that behaves like Unix on Wasm +#![allow(clippy::items_after_test_module)] use crate::specifier::SpecifierError; #[cfg(any(target_arch = "wasm32", test))] From 5c20dd5fb8c6524b17824971123eb44083dd6339 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Sat, 6 Jan 2024 19:33:35 +0100 Subject: [PATCH 23/23] assert_eq boolean --- packages/transformers/js/core/src/hoist.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/transformers/js/core/src/hoist.rs b/packages/transformers/js/core/src/hoist.rs index b248815a2de..daceb698f0d 100644 --- a/packages/transformers/js/core/src/hoist.rs +++ b/packages/transformers/js/core/src/hoist.rs @@ -3473,7 +3473,7 @@ mod tests { .collect::>(), vec![&BailoutReason::ThisInExport] ); - assert_eq!(collect.should_wrap, true); + assert!(collect.should_wrap); // module is not wrapped when `this` inside a class collides with an export let (collect, _code, _hoist) = parse( @@ -3505,6 +3505,6 @@ mod tests { .collect::>(), Vec::<&BailoutReason>::new() ); - assert_eq!(collect.should_wrap, false); + assert!(!collect.should_wrap); } }