Skip to content

Commit

Permalink
0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jul 4, 2024
1 parent b5c7c86 commit 9a6ff86
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
1 change: 1 addition & 0 deletions __test__/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ test('getUsageOfDangerStrings', (t) => {
let response = getUsageOfDangerStrings(["bootcss.com","bootcdn.com","polyfill.com","polyfill.io"],{
cwd: dirname(__filename),
});
console.log(response);
t.is(response.length,7)
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shined/source-code-diagnosis",
"version": "0.0.1",
"version": "0.0.2",
"main": "index.js",
"types": "index.d.ts",
"napi": {
Expand Down
22 changes: 18 additions & 4 deletions src/danger_string/danger_string_visitor.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
use std::{marker::PhantomData, path::PathBuf};

use oxc_ast::Visit;

use super::location::Location;

pub struct DangerStringVisitor<'a> {
pub used: &'a mut Vec<Location>,
pub file_path: &'a String,
pub danger_strings: &'a Vec<String>,
pub used: Vec<Location>,
pub file_path: PathBuf,
pub danger_strings: Vec<String>,
_phantom: PhantomData<&'a ()>,
}

impl<'a> DangerStringVisitor<'a> {
pub fn new(file_path: PathBuf, danger_strings: Vec<String>) -> Self {
Self {
used: Vec::new(),
file_path: file_path,
danger_strings: danger_strings,
_phantom: PhantomData {},
}
}
}

impl<'a> Visit<'a> for DangerStringVisitor<'a> {
Expand All @@ -22,7 +36,7 @@ impl<'a> Visit<'a> for DangerStringVisitor<'a> {
match_danger_string: item.to_string(),
start: lit.span.start,
end: lit.span.end,
file_path: self.file_path.clone(),
file_path: self.file_path.display().to_string(),
})
});
}
Expand Down
39 changes: 16 additions & 23 deletions src/danger_string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ pub fn get_usage_of_danger_strings(

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

let dir = match current_dir() {
Ok(dir) => dir.display().to_string(),
Err(e) => {
return Err(Error::new(napi::Status::GenericFailure, e.to_string()));
}
};
let dir = current_dir()?.display().to_string();

let cwd = options
.as_ref()
Expand All @@ -76,18 +71,19 @@ pub fn get_usage_of_danger_strings(
.map_err(|e| Error::new(napi::Status::GenericFailure, e.to_string()))?;

let used = Arc::new(Mutex::new(Vec::new()));

let danger_strings = Arc::new(danger_strings);

// TODO use rayon 🤔 ?
let pool = ThreadPool::new(concurrency as usize);

for entry in entries {
let entry = entry.map_err(|e| Error::new(napi::Status::GenericFailure, e.to_string()))?;
let path = entry.path().to_path_buf();
let used = Arc::clone(&used);
let danger_strings = Arc::clone(&danger_strings);

pool.execute(move || {
if path.is_file() {
if path.is_file() {
pool.execute(move || {
let source_text = read(&path)
.map_err(|err| {
Error::new(
Expand All @@ -98,29 +94,26 @@ pub fn get_usage_of_danger_strings(
.unwrap();

let source_text = String::from_utf8_lossy(&source_text);

let allocator = Allocator::default();
let source_type = SourceType::from_path(&path)
.map_err(|e| Error::new(napi::Status::GenericFailure, e.0.to_string()))
.unwrap();
let ret = Parser::new(&allocator, &source_text, source_type).parse();
let mut local_used = vec![];
DangerStringVisitor {
used: &mut local_used,
file_path: &path.to_str().unwrap().to_string(),
danger_strings: &danger_strings,
}
.visit_program(&ret.program);

let mut visitor = DangerStringVisitor::new(path.to_path_buf(), danger_strings.to_vec());
visitor.visit_program(&ret.program);
let mut used = used.lock().unwrap();
used.extend(local_used);
}
});
used.extend(visitor.used);
});
}
}

pool.join();

let used = Arc::try_unwrap(used).unwrap().into_inner().unwrap();
let vec = Arc::try_unwrap(used)
.ok()
.expect("Arc has more than one strong reference")
.into_inner()
.expect("Mutex cannot be locked");

Ok(used)
Ok(vec)
}

0 comments on commit 9a6ff86

Please sign in to comment.