Skip to content

Commit

Permalink
feat(linter): support vitest/no-disabled-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven committed Jun 18, 2024
1 parent 41b3c24 commit bc379a1
Show file tree
Hide file tree
Showing 15 changed files with 323 additions and 21 deletions.
5 changes: 5 additions & 0 deletions apps/oxlint/fixtures/eslintrc_vitest_replace/eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"vitest/no-disabled-tests": "error"
}
}
3 changes: 3 additions & 0 deletions apps/oxlint/fixtures/eslintrc_vitest_replace/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test.skip('foo', () => {
// ...
})
4 changes: 4 additions & 0 deletions apps/oxlint/src/command/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ pub struct EnablePlugins {
#[bpaf(switch, hide_usage)]
pub jest_plugin: bool,

/// Enable the Vitest plugin and detect test problems
#[bpaf(switch, hide_usage)]
pub vitest_plugin: bool,

/// Enable the JSX-a11y plugin and detect accessibility problems
#[bpaf(switch, hide_usage)]
pub jsx_a11y_plugin: bool,
Expand Down
23 changes: 23 additions & 0 deletions apps/oxlint/src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl Runner for LintRunner {
.with_import_plugin(enable_plugins.import_plugin)
.with_jsdoc_plugin(enable_plugins.jsdoc_plugin)
.with_jest_plugin(enable_plugins.jest_plugin)
.with_vitest_plugin(enable_plugins.vitest_plugin)
.with_jsx_a11y_plugin(enable_plugins.jsx_a11y_plugin)
.with_nextjs_plugin(enable_plugins.nextjs_plugin)
.with_react_perf_plugin(enable_plugins.react_perf_plugin);
Expand Down Expand Up @@ -486,4 +487,26 @@ mod test {
assert!(test_invalid_options(&["--tsconfig", "oxc/tsconfig.json"])
.contains("oxc/tsconfig.json\" does not exist, Please provide a valid tsconfig file."));
}

#[test]
fn test_enable_vitest_plugin() {
let args = &[
"-c",
"fixtures/eslintrc_vitest_replace/eslintrc.json",
"fixtures/eslintrc_vitest_replace/foo.js",
];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_errors, 0);

let args = &[
"--vitest-plugin",
"-c",
"fixtures/eslintrc_vitest_replace/eslintrc.json",
"fixtures/eslintrc_vitest_replace/foo.js",
];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_errors, 1);
}
}
5 changes: 5 additions & 0 deletions crates/oxc_linter/fixtures/eslint_config_vitest_replace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"vitest/no-disabled-tests": "error"
}
}
37 changes: 34 additions & 3 deletions crates/oxc_linter/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use rustc_hash::FxHashSet;
use schemars::JsonSchema;
use serde::Deserialize;

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

pub use self::{
env::OxlintEnv, globals::OxlintGlobals, rules::OxlintRules,
Expand Down Expand Up @@ -112,8 +114,10 @@ impl OxlintConfig {
0 => unreachable!(),
1 => {
let rule_config = &rule_configs[0];
let rule_name = &rule_config.rule_name;
let plugin_name = &rule_config.plugin_name;
let (rule_name, plugin_name) = transform_rule_and_plugin_name(
&rule_config.rule_name,
&rule_config.plugin_name,
);
let severity = rule_config.severity;
match severity {
AllowWarnDeny::Warn | AllowWarnDeny::Deny => {
Expand Down Expand Up @@ -167,9 +171,23 @@ impl OxlintConfig {
}
}

fn transform_rule_and_plugin_name<'a>(
rule_name: &'a str,
plugin_name: &'a str,
) -> (&'a str, &'a str) {
if plugin_name == "vitest" && is_jest_rule_adapted_to_vitest(rule_name) {
return (rule_name, "jest");
}

(rule_name, plugin_name)
}

#[cfg(test)]
mod test {
use crate::rules::RULES;

use super::OxlintConfig;
use rustc_hash::FxHashSet;
use serde::Deserialize;
use std::env;

Expand Down Expand Up @@ -218,4 +236,17 @@ mod test {
assert_eq!(env.iter().count(), 1);
assert!(globals.is_enabled("foo"));
}

#[test]
fn test_vitest_rule_replace() {
let fixture_path: std::path::PathBuf =
env::current_dir().unwrap().join("fixtures/eslint_config_vitest_replace.json");
let config = OxlintConfig::from_file(&fixture_path).unwrap();
let mut set = FxHashSet::default();
config.override_rules(&mut set, &RULES);

let rule = set.into_iter().next().unwrap();
assert_eq!(rule.name(), "no-disabled-tests");
assert_eq!(rule.plugin_name(), "jest");
}
}
5 changes: 5 additions & 0 deletions crates/oxc_linter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use oxc_span::{SourceType, Span};
use oxc_syntax::module_record::ModuleRecord;

use crate::{
config::OxlintRules,
disable_directives::{DisableDirectives, DisableDirectivesBuilder},
fixer::{Fix, Message, RuleFixer},
javascript_globals::GLOBALS,
Expand Down Expand Up @@ -113,6 +114,10 @@ impl<'a> LintContext<'a> {
&self.eslint_config.env
}

pub fn rules(&self) -> &OxlintRules {
&self.eslint_config.rules
}

pub fn env_contains_var(&self, var: &str) -> bool {
for env in self.env().iter() {
let env = GLOBALS.get(env).unwrap_or(&GLOBALS["builtin"]);
Expand Down
23 changes: 21 additions & 2 deletions crates/oxc_linter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use serde_json::{Number, Value};

use oxc_diagnostics::{Error, OxcDiagnostic, Severity};

use crate::{config::OxlintConfig, rules::RULES, RuleCategory, RuleEnum, RuleWithSeverity};
use crate::{
config::OxlintConfig, rules::RULES, utils::is_jest_rule_adapted_to_vitest, RuleCategory,
RuleEnum, RuleWithSeverity,
};

#[derive(Debug)]
pub struct LintOptions {
Expand All @@ -22,6 +25,7 @@ pub struct LintOptions {
pub import_plugin: bool,
pub jsdoc_plugin: bool,
pub jest_plugin: bool,
pub vitest_plugin: bool,
pub jsx_a11y_plugin: bool,
pub nextjs_plugin: bool,
pub react_perf_plugin: bool,
Expand All @@ -40,6 +44,7 @@ impl Default for LintOptions {
import_plugin: false,
jsdoc_plugin: false,
jest_plugin: false,
vitest_plugin: false,
jsx_a11y_plugin: false,
nextjs_plugin: false,
react_perf_plugin: false,
Expand Down Expand Up @@ -110,6 +115,12 @@ impl LintOptions {
self
}

#[must_use]
pub fn with_vitest_plugin(mut self, yes: bool) -> Self {
self.vitest_plugin = yes;
self
}

#[must_use]
pub fn with_jsx_a11y_plugin(mut self, yes: bool) -> Self {
self.jsx_a11y_plugin = yes;
Expand Down Expand Up @@ -278,7 +289,15 @@ impl LintOptions {
"typescript" => self.typescript_plugin,
"import" => self.import_plugin,
"jsdoc" => self.jsdoc_plugin,
"jest" => self.jest_plugin,
"jest" => {
if self.jest_plugin {
return true;
}
if self.vitest_plugin && is_jest_rule_adapted_to_vitest(rule.name()) {
return true;
}
false
}
"jsx_a11y" => self.jsx_a11y_plugin,
"nextjs" => self.nextjs_plugin,
"react_perf" => self.react_perf_plugin,
Expand Down
Loading

0 comments on commit bc379a1

Please sign in to comment.