fix(linter): detect typescript eslint alias rules (#7622)

closes #7233
This commit is contained in:
Alexander S. 2024-12-04 03:16:00 +01:00 committed by GitHub
parent 771c698a81
commit 29db060c78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 101 additions and 1 deletions

View file

@ -0,0 +1,8 @@
{
"categories": {
"correctness": "off"
},
"rules": {
"no-magic-numbers": "error"
}
}

View file

@ -0,0 +1,9 @@
{
"plugins": ["typescript"],
"categories": {
"correctness": "off"
},
"rules": {
"typescript/no-magic-numbers": "error"
}
}

View file

@ -0,0 +1,6 @@
const price = 200;
const price_with_tax = price * 0.19; // taxes are expensive
console.debug(price_with_tax);

View file

@ -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);
}
}

View file

@ -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,
};
@ -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)
}

View file

@ -34,6 +34,49 @@ 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.
@ -41,6 +84,13 @@ 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)?;