Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(linter): add run_on_jest_node to run rules on only jest nodes #6721

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/context/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ impl<'a> ContextHost<'a> {

self
}

/// Returns the framework hints for the target file.
#[inline]
pub fn frameworks(&self) -> FrameworkFlags {
self.frameworks
}
}

impl<'a> From<ContextHost<'a>> for Vec<Message<'a>> {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/frameworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ bitflags! {
const Jest = 1 << 9;
const Vitest = 1 << 10;
const OtherTest = 1 << 11;
/// Flag for if any test frameworks are used, such as Jest or Vitest.
const Test = Self::Jest.bits() | Self::Vitest.bits() | Self::OtherTest.bits();
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use config::LintConfig;
use context::ContextHost;
use options::LintOptions;
use oxc_semantic::{AstNode, Semantic};
use utils::iter_possible_jest_call_node;

pub use crate::{
builder::LinterBuilder,
Expand Down Expand Up @@ -140,6 +141,14 @@ impl Linter {
}
}

if ctx_host.frameworks().is_test() && self.options.plugins.has_test() {
for jest_node in iter_possible_jest_call_node(semantic) {
for (rule, ctx) in &rules {
rule.run_on_jest_node(&jest_node, ctx);
}
}
}

ctx_host.take_diagnostics()
}

Expand Down
13 changes: 13 additions & 0 deletions crates/oxc_linter/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};

use crate::{
context::{ContextHost, LintContext},
utils::PossibleJestNode,
AllowWarnDeny, AstNode, FixKind, RuleEnum,
};

Expand All @@ -35,6 +36,18 @@ pub trait Rule: Sized + Default + fmt::Debug {
#[inline]
fn run_once(&self, ctx: &LintContext) {}

/// Run on each Jest node (e.g. `it`, `describe`, `test`, `expect`, etc.).
/// This is only called if the Jest plugin is enabled and the file is a test file.
/// It should be used to run rules that are specific to Jest or Vitest.
#[expect(unused_variables)]
#[inline]
fn run_on_jest_node<'a, 'c>(
&self,
jest_node: &PossibleJestNode<'a, 'c>,
ctx: &'c LintContext<'a>,
) {
}

/// Check if a rule should be run at all.
///
/// You usually do not need to implement this function. If you do, use it to
Expand Down
17 changes: 16 additions & 1 deletion crates/oxc_macros/src/declare_all_lint_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
let expanded = quote! {
#(pub use self::#use_stmts::#struct_names;)*

use crate::{context::{ContextHost, LintContext}, rule::{Rule, RuleCategory, RuleFixMeta, RuleMeta}, AstNode};
use crate::{
context::{ContextHost, LintContext},
rule::{Rule, RuleCategory, RuleFixMeta, RuleMeta},
utils::PossibleJestNode,
AstNode
};
use oxc_semantic::SymbolId;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -134,6 +139,16 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
}
}

pub(super) fn run_on_jest_node<'a, 'c>(
&self,
jest_node: &PossibleJestNode<'a, 'c>,
ctx: &'c LintContext<'a>,
) {
match self {
#(Self::#struct_names(rule) => rule.run_on_jest_node(jest_node, ctx)),*
}
}

pub(super) fn should_run(&self, ctx: &ContextHost) -> bool {
match self {
#(Self::#struct_names(rule) => rule.should_run(ctx)),*
Expand Down
Loading