Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clippy and use napi's Either3 #9047

Merged
merged 24 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/node-bindings/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

#[cfg(target_arch = "wasm32")]
use std::alloc::{alloc, Layout};

Expand Down
48 changes: 18 additions & 30 deletions crates/node-bindings/src/resolver.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +20,7 @@ use parcel_resolver::{
IncludeNodeModules, Invalidations, ModuleType, Resolution, ResolverError, SpecifierType,
};

type NapiSideEffectsVariants = napi::Either<bool, napi::Either<Vec<String>, HashMap<String, bool>>>;
type NapiSideEffectsVariants = Either3<bool, Vec<String>, HashMap<String, bool>>;

#[napi(object)]
pub struct JsFileSystemOptions {
Expand Down Expand Up @@ -197,12 +200,8 @@ pub struct GlobCreateInvalidation {
pub struct ResolveResult {
pub resolution: JsUnknown,
pub invalidate_on_file_change: Vec<String>,
pub invalidate_on_file_create: Vec<
napi::Either<
FilePathCreateInvalidation,
napi::Either<FileNameCreateInvalidation, GlobCreateInvalidation>,
>,
>,
pub invalidate_on_file_create:
Vec<Either3<FilePathCreateInvalidation, FileNameCreateInvalidation, GlobCreateInvalidation>>,
pub query: Option<String>,
pub side_effects: bool,
pub error: JsUnknown,
Expand All @@ -212,12 +211,8 @@ pub struct ResolveResult {
#[napi(object)]
pub struct JsInvalidations {
pub invalidate_on_file_change: Vec<String>,
pub invalidate_on_file_create: Vec<
napi::Either<
FilePathCreateInvalidation,
napi::Either<FileNameCreateInvalidation, GlobCreateInvalidation>,
>,
>,
pub invalidate_on_file_create:
Vec<Either3<FilePathCreateInvalidation, FileNameCreateInvalidation, GlobCreateInvalidation>>,
pub invalidate_on_startup: bool,
}

Expand Down Expand Up @@ -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),
});
}

Expand Down Expand Up @@ -360,7 +355,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,
Expand Down Expand Up @@ -483,12 +478,7 @@ fn convert_invalidations(
invalidations: Invalidations,
) -> (
Vec<String>,
Vec<
napi::Either<
FilePathCreateInvalidation,
napi::Either<FileNameCreateInvalidation, GlobCreateInvalidation>,
>,
>,
Vec<Either3<FilePathCreateInvalidation, FileNameCreateInvalidation, GlobCreateInvalidation>>,
) {
let invalidate_on_file_change = invalidations
.invalidate_on_file_change
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions packages/transformers/js/core/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| &param.pat),
Expr::Arrow(arrow) => arrow.params.get(0),
Expr::Fn(func) => func.function.params.first().map(|param| &param.pat),
Expr::Arrow(arrow) => arrow.params.first(),
_ => None,
};

Expand Down
70 changes: 31 additions & 39 deletions packages/transformers/js/core/src/constant_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -33,14 +29,12 @@ 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 {
Expr::Lit(lit) => {
if !is_safe_literal(&lit) {
if !is_safe_literal(lit) {
return false;
}
}
Expand All @@ -55,14 +49,12 @@ impl ConstantModule {
}
}

return true;
}
_ => {
return false;
true
}
_ => false,
}
} else {
return true;
true
}
}

Expand All @@ -73,7 +65,7 @@ impl ConstantModule {
}

for declarator in &var_decl.decls {
if !self.is_constant_declarator(&declarator) {
if !self.is_constant_declarator(declarator) {
return false;
}

Expand All @@ -84,16 +76,16 @@ impl ConstantModule {
}
}

return true;
true
} else {
return false;
false
}
}
}

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;
Expand All @@ -117,7 +109,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;
Expand Down Expand Up @@ -183,7 +175,7 @@ mod tests {
"#,
);

assert_eq!(result, true);
assert!(result);
}

#[test]
Expand All @@ -194,7 +186,7 @@ mod tests {
"#,
);

assert_eq!(result, true);
assert!(result);
}

#[test]
Expand All @@ -205,7 +197,7 @@ mod tests {
"#,
);

assert_eq!(result, true);
assert!(result);
}

#[test]
Expand All @@ -216,7 +208,7 @@ mod tests {
"#,
);

assert_eq!(result, true);
assert!(result);
}

#[test]
Expand All @@ -227,7 +219,7 @@ mod tests {
"#,
);

assert_eq!(result, true);
assert!(result);
}

#[test]
Expand All @@ -239,7 +231,7 @@ mod tests {
"#,
);

assert_eq!(result, true);
assert!(result);
}

#[test]
Expand All @@ -250,7 +242,7 @@ mod tests {
"#,
);

assert_eq!(result, true);
assert!(result);
}

#[test]
Expand All @@ -262,7 +254,7 @@ mod tests {
"#,
);

assert_eq!(result, true);
assert!(result);
}

#[test]
Expand All @@ -273,7 +265,7 @@ mod tests {
"#,
);

assert_eq!(result, true);
assert!(result);
}

#[test]
Expand All @@ -284,7 +276,7 @@ mod tests {
"#,
);

assert_eq!(result, false);
assert!(!result);
}

#[test]
Expand All @@ -296,7 +288,7 @@ mod tests {
"#,
);

assert_eq!(result, false);
assert!(!result);
}

#[test]
Expand All @@ -308,7 +300,7 @@ mod tests {
"#,
);

assert_eq!(result, false);
assert!(!result);
}

#[test]
Expand All @@ -319,7 +311,7 @@ mod tests {
"#,
);

assert_eq!(result, false);
assert!(!result);
}

#[test]
Expand All @@ -330,7 +322,7 @@ mod tests {
"#,
);

assert_eq!(result, false);
assert!(!result);
}

#[test]
Expand All @@ -341,7 +333,7 @@ mod tests {
"#,
);

assert_eq!(result, false);
assert!(!result);
}

#[test]
Expand All @@ -352,13 +344,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);
}
}