Skip to content

Commit

Permalink
feat(linter): support vitest/no-disabled-tests (#3717)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven committed Jun 24, 2024
1 parent ef82c78 commit 328445b
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 @@ -208,6 +208,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 @@ -488,4 +489,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 @@ -16,7 +16,9 @@ pub use self::{
rules::OxlintRules,
settings::{jsdoc::JSDocPluginSettings, OxlintSettings},
};
use crate::{rules::RuleEnum, AllowWarnDeny, RuleWithSeverity};
use crate::{
rules::RuleEnum, utils::is_jest_rule_adapted_to_vitest, AllowWarnDeny, RuleWithSeverity,
};

/// Oxlint Configuration File
///
Expand Down Expand Up @@ -113,8 +115,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 @@ -168,12 +172,26 @@ 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 std::env;

use rustc_hash::FxHashSet;
use serde::Deserialize;

use crate::rules::RULES;

use super::OxlintConfig;

#[test]
Expand Down Expand Up @@ -221,4 +239,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 @@ -7,6 +7,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 @@ -131,6 +132,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 @@ -4,7 +4,10 @@ use oxc_diagnostics::{Error, OxcDiagnostic, Severity};
use rustc_hash::FxHashSet;
use serde_json::{Number, Value};

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 @@ -21,6 +24,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 @@ -39,6 +43,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 @@ -109,6 +114,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 @@ -277,7 +288,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 328445b

Please sign in to comment.