Skip to content

Commit

Permalink
79
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Dec 13, 2024
1 parent e09237c commit c40865b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 138 deletions.
95 changes: 3 additions & 92 deletions crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,102 +12,13 @@ pub fn get_graph(
Ok(graph.get_edges())
}

#[napi(ts_args_type = "
rules: Array<
| 'no-debugger'
| 'no-console'
| 'constructor-super'
| 'for-direction'
| 'getter-return'
| 'no-async-promise-executor'
| 'no-case-declarations'
| 'no-class-assign'
| 'no-compare-neg-zero'
| 'no-cond-assign'
| 'no-const-assign'
| 'no-constant-binary-expression'
| 'no-constant-condition'
| 'no-control-regex'
| 'no-delete-var'
| 'no-dupe-class-members'
| 'no-dupe-else-if'
| 'no-dupe-keys'
| 'no-duplicate-case'
| 'no-empty'
| 'no-empty-character-class'
| 'no-empty-pattern'
| 'no-ex-assign'
| 'no-fallthrough'
| 'no-func-assign'
| 'no-global-assign'
| 'no-import-assign'
| 'no-inner-declarations'
| 'no-invalid-regexp'
| 'no-irregular-whitespace'
| 'no-loss-of-precision'
| 'no-new-native-nonconstructor'
| 'no-nonoctal-decimal-escape'
| 'no-obj-calls'
| 'no-prototype-builtins'
| 'no-redeclare'
| 'no-regex-spaces'
| 'no-self-assign'
| 'no-setter-return'
| 'no-shadow-restricted-names'
| 'no-sparse-arrays'
| 'no-this-before-super'
| 'no-undef'
| 'no-unexpected-multiline'
| 'no-unreachable'
| 'no-unsafe-finally'
| 'no-unsafe-negation'
| 'no-unused-labels'
| 'no-unused-vars'
| 'no-useless-catch'
| 'no-useless-escape'
| 'no-with'
| 'require-atomic-updates'
| 'use-before-define'
| 'valid-typeof'
| 'react/jsx-no-comment-textnodes'
| 'react/jsx-no-duplicate-props'
| 'react/jsx-no-target-blank'
| 'react/jsx-no-undef'
| 'react/no-children-prop'
| 'react/no-danger-with-children'
| 'react/no-direct-mutation-state'
| 'react/no-find-dom-node'
| 'react/no-is-mounted'
| 'react/no-render-return-value'
| 'react/no-string-refs'
| 'react/no-unescaped-entities'
| 'react/react-in-jsx-scope'
| 'react/require-render-return'
| 'import/export'
| 'unicorn/new-for-builtins'
| 'unicorn/no-instanceof-array'
| 'unicorn/no-invalid-remove-event-listener'
| 'unicorn/no-thenable'
| 'unicorn/no-unreadable-array-destructuring'
| 'unicorn/require-array-join-separator'
| 'unicorn/require-number-to-fixed-digits-argument'
| '@ts/no-duplicate-enum-values'
| '@ts/no-extra-non-null-assertion'
| '@ts/no-misused-new'
| '@ts/no-non-null-asserted-optional-chain'
| '@ts/no-unsafe-declaration-merging'
| '@ts/no-unsafe-function-type'
| '@ts/no-wrapper-object-types'
| '@ts/prefer-namespace-keyword'
>,
args: GlobJsArgs
")]
#[napi]
pub fn check_oxlint(
rules: Vec<&'static str>,
oxlint_config: String,
args: utils::GlobJsArgs,
) -> Result<Vec<check_oxlint::CheckOxlintResponse>> {
let args = utils::GlobArgs::from(args);
check_oxlint::check_oxlint(rules, args)
check_oxlint::check_oxlint(oxlint_config, args)
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))
}

Expand Down
53 changes: 9 additions & 44 deletions crates/check_oxlint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use oxc_diagnostics::Severity;
use oxc_linter::{FixKind, LinterBuilder, Oxlintrc};
use oxc_semantic::SemanticBuilder;
use serde::Serialize;
use serde_json::json;

#[napi(object)]
#[derive(Debug, Clone, Serialize)]
Expand All @@ -32,22 +31,22 @@ pub struct CheckOxlintResponse {
pub labels: Vec<CheckOxlintLabelsResponse>,
}

// #[napi(object, js_name = "CheckOxlintArgs")]
// pub struct CheckOxlintArgs {
// pub globals: Option<JsObject>,
// pub rules: Vec<Value>,
// }

impl Display for CheckOxlintResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", serde_json::to_string_pretty(self).unwrap())
}
}

pub fn check_oxlint(
rules: Vec<&'static str>,
oxlint_config: String,
args: utils::GlobArgs,
) -> anyhow::Result<Vec<CheckOxlintResponse>> {
let cx_args: Oxlintrc = serde_json::from_str(&oxlint_config)?;

let linter = LinterBuilder::from_oxlintrc(true, cx_args.clone())
.with_fix(FixKind::None)
.build();

let responses = utils::glob_by(
|path| {
let builder = utils::SemanticBuilder::with_file(path).unwrap();
Expand All @@ -72,40 +71,6 @@ pub fn check_oxlint(
&semantic.semantic,
));

let rules: serde_json::Value = rules
.iter()
.map(|&rule| {
(
rule.to_string(),
serde_json::Value::String("error".to_string()),
)
})
.collect::<serde_json::Map<String, serde_json::Value>>()
.into();

let config: Oxlintrc = serde_json::from_value(json!({
"env":{
"browser": true,
"node": true,
"es6": true,
"jest": true,
"shared-node-browser": true
},
"rules": rules,
"globals":{
"__webpack_public_path__": "readonly",
"ROOT_PATH": "readonly",
"__ROOT_SAGA__":"readonly",
"__ROOT_REDUCER__":"readonly",
"__ROOT_ROUTE__":"readonly",
"__ROOT_REDUX_DEVTOOLS__":"readonly"
}
}))
.unwrap();
let linter = LinterBuilder::from_oxlintrc(true, config)
.with_fix(FixKind::None)
.build();

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

let diagnostics = linter.run(path, semantic, module_record);
Expand All @@ -124,12 +89,12 @@ pub fn check_oxlint(
.help
.as_ref()
.map(|h| h.to_string())
.unwrap_or("".to_string());
.unwrap_or_default();
let url = error
.url
.as_ref()
.map(|u| u.to_string())
.unwrap_or("".to_string());
.unwrap_or_default();

let severity = match error.severity {
Severity::Advice => "advice".to_string(),
Expand Down
12 changes: 11 additions & 1 deletion crates/check_oxlint/tests/oxlint_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ macro_rules! test_oxlint {
};

let args = utils::GlobArgs::from(js_args);
let mut responses = check_oxlint(vec![$rule], args)?;
let mut responses = check_oxlint(
format!(
r#"{{
"rules":{{
"{}":"error"
}}
}}"#,
$rule
),
args,
)?;

responses.sort_by(|a, b| a.file_name.cmp(&b.file_name));

Expand Down
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.78",
"version": "0.0.79",
"main": "index.js",
"types": "index.d.ts",
"napi": {
Expand Down

0 comments on commit c40865b

Please sign in to comment.