Skip to content

Commit

Permalink
refactor(linter): add run_on_jest_node to run rules on only jest no…
Browse files Browse the repository at this point in the history
…des (#6721)

- part of #6038

Adds a new `run_on_jest_node` method for running lint rules which allows only Jest/Vitest nodes to be linted. This simplifies a number of Jest rules by removing the need to iterate/collect Jest nodes inside of them. Now, they can simply run on the Jest nodes that are passed to them directly. This also saves time by skipping the running of the rule if it is not a test file or the Jest/Vitest plugins are not enabled.
  • Loading branch information
camchenry committed Oct 21, 2024
1 parent 155fe7e commit 97195ec
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
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

0 comments on commit 97195ec

Please sign in to comment.