Skip to content

Commit

Permalink
Refactor file processing and error handling
Browse files Browse the repository at this point in the history
- Replace custom `oxc_visitor_processor` with new `utils::global::glob` function
- Implement `anyhow::Error` for improved error handling
- Update module imports across multiple files
- Remove `oxc_visitor_processor.rs`
- Add `anyhow` dependency to Cargo.toml
- Minor updates to type definitions in index.d.ts
  • Loading branch information
ityuany committed Sep 13, 2024
1 parent 5a1be93 commit 9b466bd
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 130 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tower-lsp = "0.20.0"
ropey = "1.0.0"
threadpool = "1.8.1"
rayon = "1.10.0"
anyhow = "1.0.88"
serde_json = "1.0.121"
serde_json5 = "0.1.0"
regex = "1.5.4"
Expand Down
15 changes: 6 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ export interface Response {
astNode: AstNode
}
export declare function getModuleMemberUsage(npmNameVec: Array<string>, options?: Options | undefined | null): Array<Response>
export interface Target {
chrome: string
}
export interface Options {
pattern?: string
ignore?: Array<string>
cwd?: string
concurrency?: number
}
export interface AstNode {
span: Span
loc: Location
Expand All @@ -73,3 +64,9 @@ export interface Position {
line: number
col: number
}
export interface Options {
pattern?: string
ignore?: Array<string>
cwd?: string
concurrency?: number
}
9 changes: 6 additions & 3 deletions src/check_browser_supported/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ use std::{

use crate::{
check_browser_supported::compat::CompatHandler,
oxc_visitor_processor::{oxc_visit_process, Options},
utils::semantic_builder::SemanticBuilder,
utils::{
global::{glob, Options},
semantic_builder::SemanticBuilder,
},
};

fn get_version_list<'a>(
Expand Down Expand Up @@ -271,7 +273,8 @@ pub fn check_browser_supported(
}
};

oxc_visit_process(handler, options)?;
glob(handler, options)
.map_err(|err| Error::new(napi::Status::GenericFailure, err.to_string()))?;

let used = Arc::try_unwrap(used)
.ok()
Expand Down
11 changes: 7 additions & 4 deletions src/danger_string_usage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use napi::Result;
use oxc_ast::AstKind;
use serde::Serialize;

use crate::{
oxc_visitor_processor::{oxc_visit_process, Options},
utils::{ast_node::AstNode, semantic_builder::SemanticBuilder},
use crate::utils::{
ast_node::AstNode,
global::{glob, Options},
semantic_builder::SemanticBuilder,
};

#[napi(object)]
Expand Down Expand Up @@ -60,7 +61,9 @@ pub fn get_danger_strings_usage(
}
};

oxc_visit_process(handler, options)?;
glob(handler, options).map_err(|err| {
napi::Error::new(napi::Status::GenericFailure, err.to_string())
})?;

let used = Arc::try_unwrap(used)
.ok()
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ extern crate napi_derive;
mod check_browser_supported;
mod danger_string_usage;
mod module_member_usage;
mod oxc_visitor_processor;
mod utils;

pub use check_browser_supported::check_browser_supported;
Expand Down
10 changes: 6 additions & 4 deletions src/module_member_usage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use handler::ModuleMemberUsageHandler;
use napi::Result;
use response::Response;

use crate::{
oxc_visitor_processor::{oxc_visit_process, Options},
utils::semantic_builder::SemanticBuilder,
use crate::utils::{
global::{glob, Options},
semantic_builder::SemanticBuilder,
};

mod handler;
Expand Down Expand Up @@ -39,7 +39,9 @@ pub fn get_module_member_usage(
used.extend(inline_usages);
}
};
oxc_visit_process(x, options)?;
glob(x, options).map_err(|err| {
napi::Error::new(napi::Status::GenericFailure, err.to_string())
})?;

let used = Arc::try_unwrap(used)
.ok()
Expand Down
109 changes: 0 additions & 109 deletions src/oxc_visitor_processor/mod.rs

This file was deleted.

58 changes: 58 additions & 0 deletions src/utils/global.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use anyhow::Result;
use rayon::prelude::*;
use std::{env::current_dir, path::PathBuf};
use wax::Glob;

#[derive(Debug, Clone)]
#[napi[object]]
pub struct Options {
pub pattern: Option<String>,
pub ignore: Option<Vec<String>>,
pub cwd: Option<String>,
pub concurrency: Option<i32>,
}

pub fn glob<F>(handler_fn: F, options: Option<Options>) -> Result<()>
where
F: Fn(PathBuf) + Send + Sync + 'static,
{
let default_pattern: &str = "**/*.{js,ts,jsx,tsx}";

let default_ignore_patterns: Vec<String> =
vec!["**/node_modules/**".to_string(), "**/*.d.ts".to_string()];

let ignore_patterns_vec: Vec<String> = options
.as_ref()
.and_then(|opts| opts.ignore.clone())
.unwrap_or_else(|| {
default_ignore_patterns
.iter()
.map(|s| s.to_string())
.collect()
});

let ignore_patterns: Vec<&str> =
ignore_patterns_vec.iter().map(String::as_str).collect();

let dir = current_dir()?.display().to_string();

let cwd = options
.as_ref()
.and_then(|opts| opts.cwd.clone())
.unwrap_or(dir);

let glob = Glob::new(default_pattern)?;

let entries: Vec<_> = glob.walk(&cwd).not(ignore_patterns)?.collect();

entries.par_iter().try_for_each(|item| -> Result<()> {
if let Ok(entry) = item {
if entry.path().is_file() {
handler_fn(entry.path().to_path_buf());
}
}
Ok(())
})?;

Ok(())
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod ast_node;
pub mod global;
pub mod semantic_builder;
10 changes: 10 additions & 0 deletions src/utils/position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};

#[napi(object)]
#[derive(
Debug, Default, PartialEq, Eq, Hash, Clone, Serialize, Deserialize,
)]
pub struct Position {
pub line: u32,
pub col: u32,
}

0 comments on commit 9b466bd

Please sign in to comment.