Skip to content

Commit

Permalink
fix(linter): detect typescript eslint alias rules (#7622)
Browse files Browse the repository at this point in the history
closes #7233
  • Loading branch information
Sysix authored Dec 4, 2024
1 parent 771c698 commit 29db060
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"categories": {
"correctness": "off"
},
"rules": {
"no-magic-numbers": "error"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": ["typescript"],
"categories": {
"correctness": "off"
},
"rules": {
"typescript/no-magic-numbers": "error"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


const price = 200;
const price_with_tax = price * 0.19; // taxes are expensive

console.debug(price_with_tax);
23 changes: 23 additions & 0 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,4 +819,27 @@ mod test {
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 1);
}

#[test]
fn test_eslint_and_typescript_alias_rules() {
let args = &[
"-c",
"fixtures/eslint_and_typescript_alias_rules/oxlint-eslint.json",
"fixtures/eslint_and_typescript_alias_rules/test.js",
];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 1);

let args = &[
"-c",
"fixtures/eslint_and_typescript_alias_rules/oxlint-typescript.json",
"fixtures/eslint_and_typescript_alias_rules/test.js",
];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 1);
}
}
6 changes: 5 additions & 1 deletion crates/oxc_linter/src/config/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{

use crate::{
rules::{RuleEnum, RULES},
utils::is_jest_rule_adapted_to_vitest,
utils::{is_eslint_rule_adapted_to_typescript, is_jest_rule_adapted_to_vitest},
AllowWarnDeny, RuleWithSeverity,
};

Expand Down Expand Up @@ -155,6 +155,10 @@ fn transform_rule_and_plugin_name<'a>(
return (rule_name, "jest");
}

if plugin_name == "typescript" && is_eslint_rule_adapted_to_typescript(rule_name) {
return (rule_name, "eslint");
}

(rule_name, plugin_name)
}

Expand Down
50 changes: 50 additions & 0 deletions crates/oxc_linter/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,63 @@ const VITEST_COMPATIBLE_JEST_RULES: phf::Set<&'static str> = phf::phf_set! {
"valid-expect",
};

// List of Eslint rules that have Typescript equivalents.
const TYPESCRIPT_COMPATIBLE_ESLINT_RULES: phf::Set<&'static str> = phf::phf_set! {
"class-methods-use-this",
"default-param-last",
"init-declarations",
"max-params",
"no-array-constructor",
"no-dupe-class-members",
"no-empty-function",
"no-invalid-this",
"no-loop-func",
"no-loss-of-precision",
"no-magic-numbers",
"no-redeclare",
"no-restricted-imports",
"no-shadow",
"no-unused-expressions",
"no-unused-vars",
"no-use-before-define",
"no-useless-constructor",

// these rules are equivalents, but not supported
// "block-spacing",
// "brace-style",
// "comma-dangle",
// "comma-spacing",
// "func-call-spacing",
// "indent",
// "key-spacing",
// "keyword-spacing",
// "lines-around-comment",
// "lines-between-class-members",
// "no-extra-parens",
// "no-extra-semi",
// "object-curly-spacing",
// "padding-line-between-statements",
// "quotes",
// "semi",
// "space-before-blocks",
// "space-before-function-paren",
// "space-infix-ops",
};

/// Check if the Jest rule is adapted to Vitest.
/// Many Vitest rule are essentially ports of Jest plugin rules with minor modifications.
/// For these rules, we use the corresponding jest rules with some adjustments for compatibility.
pub fn is_jest_rule_adapted_to_vitest(rule_name: &str) -> bool {
VITEST_COMPATIBLE_JEST_RULES.contains(rule_name)
}

/// Check if the Eslint rule is adapted to Typescript.
/// Many Typescript rule are essentially ports of Eslint plugin rules with minor modifications.
/// For these rules, we use the corresponding eslint rules with some adjustments for compatibility.
pub fn is_eslint_rule_adapted_to_typescript(rule_name: &str) -> bool {
TYPESCRIPT_COMPATIBLE_ESLINT_RULES.contains(rule_name)
}

pub fn read_to_string(path: &Path) -> io::Result<String> {
// `simdutf8` is faster than `std::str::from_utf8` which `fs::read_to_string` uses internally
let bytes = std::fs::read(path)?;
Expand Down

0 comments on commit 29db060

Please sign in to comment.