Skip to content

Commit 97195ec

Browse files
committedOct 21, 2024·
refactor(linter): add run_on_jest_node to run rules on only jest nodes (#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.
1 parent 155fe7e commit 97195ec

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed
 

‎crates/oxc_linter/src/context/host.rs

+6
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ impl<'a> ContextHost<'a> {
209209

210210
self
211211
}
212+
213+
/// Returns the framework hints for the target file.
214+
#[inline]
215+
pub fn frameworks(&self) -> FrameworkFlags {
216+
self.frameworks
217+
}
212218
}
213219

214220
impl<'a> From<ContextHost<'a>> for Vec<Message<'a>> {

‎crates/oxc_linter/src/frameworks.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ bitflags! {
3535
const Jest = 1 << 9;
3636
const Vitest = 1 << 10;
3737
const OtherTest = 1 << 11;
38+
/// Flag for if any test frameworks are used, such as Jest or Vitest.
3839
const Test = Self::Jest.bits() | Self::Vitest.bits() | Self::OtherTest.bits();
3940
}
4041
}

‎crates/oxc_linter/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use config::LintConfig;
2727
use context::ContextHost;
2828
use options::LintOptions;
2929
use oxc_semantic::{AstNode, Semantic};
30+
use utils::iter_possible_jest_call_node;
3031

3132
pub use crate::{
3233
builder::LinterBuilder,
@@ -140,6 +141,14 @@ impl Linter {
140141
}
141142
}
142143

144+
if ctx_host.frameworks().is_test() && self.options.plugins.has_test() {
145+
for jest_node in iter_possible_jest_call_node(semantic) {
146+
for (rule, ctx) in &rules {
147+
rule.run_on_jest_node(&jest_node, ctx);
148+
}
149+
}
150+
}
151+
143152
ctx_host.take_diagnostics()
144153
}
145154

‎crates/oxc_linter/src/rule.rs

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
1111

1212
use crate::{
1313
context::{ContextHost, LintContext},
14+
utils::PossibleJestNode,
1415
AllowWarnDeny, AstNode, FixKind, RuleEnum,
1516
};
1617

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

39+
/// Run on each Jest node (e.g. `it`, `describe`, `test`, `expect`, etc.).
40+
/// This is only called if the Jest plugin is enabled and the file is a test file.
41+
/// It should be used to run rules that are specific to Jest or Vitest.
42+
#[expect(unused_variables)]
43+
#[inline]
44+
fn run_on_jest_node<'a, 'c>(
45+
&self,
46+
jest_node: &PossibleJestNode<'a, 'c>,
47+
ctx: &'c LintContext<'a>,
48+
) {
49+
}
50+
3851
/// Check if a rule should be run at all.
3952
///
4053
/// You usually do not need to implement this function. If you do, use it to

‎crates/oxc_macros/src/declare_all_lint_rules.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
6161
let expanded = quote! {
6262
#(pub use self::#use_stmts::#struct_names;)*
6363

64-
use crate::{context::{ContextHost, LintContext}, rule::{Rule, RuleCategory, RuleFixMeta, RuleMeta}, AstNode};
64+
use crate::{
65+
context::{ContextHost, LintContext},
66+
rule::{Rule, RuleCategory, RuleFixMeta, RuleMeta},
67+
utils::PossibleJestNode,
68+
AstNode
69+
};
6570
use oxc_semantic::SymbolId;
6671

6772
#[derive(Debug, Clone)]
@@ -134,6 +139,16 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
134139
}
135140
}
136141

142+
pub(super) fn run_on_jest_node<'a, 'c>(
143+
&self,
144+
jest_node: &PossibleJestNode<'a, 'c>,
145+
ctx: &'c LintContext<'a>,
146+
) {
147+
match self {
148+
#(Self::#struct_names(rule) => rule.run_on_jest_node(jest_node, ctx)),*
149+
}
150+
}
151+
137152
pub(super) fn should_run(&self, ctx: &ContextHost) -> bool {
138153
match self {
139154
#(Self::#struct_names(rule) => rule.should_run(ctx)),*

0 commit comments

Comments
 (0)
Please sign in to comment.