Skip to content

Commit

Permalink
add new global
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Dec 20, 2024
1 parent d869639 commit 6fcd991
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 231 deletions.
50 changes: 44 additions & 6 deletions crates/check_browser_supported/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use log::debug;
use napi::Error;
use napi_derive::napi;

use utils::{glob_by_path, SemanticBuilder};
use utils::{
glob_by_semantic, GlobErrorHandler, GlobSuccessHandler, SemanticBuilder,
};

fn get_version_list<'a>(
browser_list: &'a Vec<Distrib>,
Expand Down Expand Up @@ -250,11 +252,13 @@ pub fn check_browser_supported(
}
}

let responses = glob_by_path(
|path| {
let responses = glob_by_semantic(
|GlobSuccessHandler {
relative_path,
semantic,
..
}| {
let mut used: Vec<CompatBox> = Vec::new();
let builder = SemanticBuilder::with_file(&path).unwrap();
let semantic = builder.build().unwrap();
for node in semantic.nodes().iter() {
for compat_handler in compat_handlers.iter() {
if compat_handler.handle(
Expand All @@ -270,19 +274,53 @@ pub fn check_browser_supported(
used.push(CompatBox::new(
ast_node,
compat_handler.get_compat().clone(),
path.to_str().unwrap().to_string(),
relative_path.to_string(),
));
}
}
}
Some(used)
},
|GlobErrorHandler { .. }| None,
&args,
)?
.into_iter()
.flatten()
.collect();

// let responses = glob_by_path(
// |path| {
// let mut used: Vec<CompatBox> = Vec::new();
// let builder = SemanticBuilder::with_file(&path).unwrap();
// let semantic = builder.build().unwrap();
// for node in semantic.nodes().iter() {
// for compat_handler in compat_handlers.iter() {
// if compat_handler.handle(
// semantic.source_text(),
// node,
// semantic.nodes(),
// ) {
// let ast_node = beans::AstNode::with_source_and_ast_node(
// semantic.source_text(),
// node,
// );

// used.push(CompatBox::new(
// ast_node,
// compat_handler.get_compat().clone(),
// path.to_str().unwrap().to_string(),
// ));
// }
// }
// }
// Some(used)
// },
// &args,
// )?
// .into_iter()
// .flatten()
// .collect();

Ok(responses)
}

Expand Down
13 changes: 5 additions & 8 deletions crates/check_filename_case/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashSet;

use napi_derive::napi;
use utils::glob_by_path;
use utils::{glob_by_semantic, GlobErrorHandler, GlobSuccessHandler};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[napi(object)]
Expand All @@ -12,14 +12,10 @@ pub struct CheckFilenameCaseResponse {
pub fn check_filename_case(
args: utils::GlobArgs,
) -> anyhow::Result<Vec<CheckFilenameCaseResponse>> {
let res = glob_by_path(
|path| {
let path = pathdiff::diff_paths(path, &args.cwd)?;

let str = utils::win_path_to_unix(path.display().to_string().as_str());

let res = glob_by_semantic(
|GlobSuccessHandler { relative_path, .. }| {
let mut p = vec![];
let path_str = str.split("/").collect::<Vec<&str>>();
let path_str = relative_path.split("/").collect::<Vec<&str>>();
let len = path_str.len();
for (index, part) in path_str.into_iter().enumerate() {
p.push(part);
Expand All @@ -37,6 +33,7 @@ pub fn check_filename_case(

return None;
},
|GlobErrorHandler { .. }| None,
&args,
)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/check_filename_case/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use utils::{GlobArgs, GlobJsArgs};

#[test]
fn test_check_filename_case() -> anyhow::Result<()> {
let cwd = std::env::current_dir()?;
let cwd = std::env::current_dir()?.join("tests").join("fixtures");

let args = GlobArgs::from(GlobJsArgs {
cwd: cwd.to_string_lossy().to_string(),
Expand Down
147 changes: 63 additions & 84 deletions crates/check_oxlint/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::{fmt::Display, fs::read_to_string, rc::Rc, sync::Arc};
use std::{fmt::Display, rc::Rc, sync::Arc};

use napi_derive::napi;
use oxc_allocator::Allocator;
use oxc_diagnostics::Severity;
use oxc_linter::{FixKind, LinterBuilder, Oxlintrc};
use oxc_semantic::SemanticBuilder;
use serde::Serialize;
use utils::source_type_from_path;
use utils::{GlobErrorHandler, GlobSuccessHandler, glob_by_semantic};

#[napi(object)]
#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -49,87 +47,68 @@ pub fn check_oxlint(
.with_fix(FixKind::None)
.build();

let responses = utils::glob_by_path(
|path| {
if let Ok(source_code) = read_to_string(path) {
let allocator = Allocator::default();

let source_type = source_type_from_path(path);

let parse =
oxc_parser::Parser::new(&allocator, &source_code, source_type)
.parse();

let program = allocator.alloc(parse.program);

let semantic = SemanticBuilder::new()
.with_check_syntax_error(false)
.with_cfg(true)
.build(program);

let module_record = Arc::new(oxc_linter::ModuleRecord::new(
&path,
&parse.module_record,
&semantic.semantic,
));

let semantic = Rc::new(semantic.semantic);

let diagnostics = linter.run(path, semantic, module_record);

let responses = diagnostics
.into_iter()
.map(|d| {
let error = d.error;

let file_name = pathdiff::diff_paths(path, &args.cwd)
.map(|p| p.display().to_string())
.unwrap_or_else(|| path.display().to_string());

let file_name = utils::win_path_to_unix(file_name.as_str());

let help = error.help.as_deref().unwrap_or_default().to_string();

let url = error.url.as_deref().unwrap_or_default().to_string();

let severity = match error.severity {
Severity::Advice => "advice".to_string(),
Severity::Warning => "warning".to_string(),
Severity::Error => "error".to_string(),
};

let labels = error
.labels
.as_ref()
.map(|v| {
v.iter()
.map(|l| CheckOxlintLabelsResponse {
span: CheckOxlintLabelsSpan {
offset: l.offset() as u32,
length: l.len() as u32,
},
})
.collect::<Vec<_>>()
})
.unwrap_or_default();

return CheckOxlintResponse {
file_name,
help,
url,
severity,
code: error.code.to_string(),
message: error.message.to_string(),
labels: labels,
};
})
.collect::<Vec<_>>();

Some(responses)
} else {
None
}
let responses = glob_by_semantic(
|GlobSuccessHandler {
path,
parse,
semantic,
relative_path,
..
}| {
let module_record = Arc::new(oxc_linter::ModuleRecord::new(
&path,
&parse.module_record,
&semantic,
));
let semantic = Rc::new(semantic);

let diagnostics = linter.run(&path, semantic, module_record);

let responses = diagnostics
.into_iter()
.map(|d| {
let error = d.error;

let help = error.help.as_deref().unwrap_or_default().to_string();

let url = error.url.as_deref().unwrap_or_default().to_string();

let severity = match error.severity {
Severity::Advice => "advice".to_string(),
Severity::Warning => "warning".to_string(),
Severity::Error => "error".to_string(),
};

let labels = error
.labels
.as_ref()
.map(|v| {
v.iter()
.map(|l| CheckOxlintLabelsResponse {
span: CheckOxlintLabelsSpan {
offset: l.offset() as u32,
length: l.len() as u32,
},
})
.collect::<Vec<_>>()
})
.unwrap_or_default();

return CheckOxlintResponse {
file_name: relative_path.clone(),
help,
url,
severity,
code: error.code.to_string(),
message: error.message.to_string(),
labels: labels,
};
})
.collect::<Vec<_>>();

Some(responses)
},
|GlobErrorHandler { .. }| None,
&args,
)?;

Expand Down
42 changes: 12 additions & 30 deletions crates/check_syntax/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use napi_derive::napi;
use utils::{glob_by_path, GlobArgs};
use utils::{glob_by_semantic, GlobArgs, GlobErrorHandler, GlobSuccessHandler};

#[derive(Debug, Clone)]
#[napi(object, js_name = "CheckSyntaxResponse")]
Expand All @@ -11,35 +11,17 @@ pub struct CheckSyntaxResponse {
pub fn check_syntax(
args: GlobArgs,
) -> anyhow::Result<Vec<CheckSyntaxResponse>> {
let responses = glob_by_path(
|path| {
let builder = utils::SemanticBuilder::with_file(path);

if builder.is_err() {
return Some(CheckSyntaxResponse {
path: path.display().to_string(),
errors: vec![
"File is not a valid JavaScript file, Please check the file syntax"
.to_owned(),
],
});
}

let builder = builder.unwrap();

let semantic = builder.build();

if semantic.is_err() {
return Some(CheckSyntaxResponse {
path: path.display().to_string(),
errors: vec![
"File is not a valid JavaScript file, Please check the file syntax"
.to_owned(),
],
});
}

None
let responses = glob_by_semantic(
|GlobSuccessHandler { .. }| None,
|GlobErrorHandler {
relative_path,
error,
..
}| {
Some(CheckSyntaxResponse {
path: relative_path,
errors: vec![error],
})
},
&args,
)?;
Expand Down
Loading

0 comments on commit 6fcd991

Please sign in to comment.