Skip to content
Closed
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
23 changes: 20 additions & 3 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,24 @@ impl Linter {

let rules = rules
.iter()
.filter(|(rule, _)| rule.should_run(&ctx_host) && !rule.is_tsgolint_rule())
.filter(|(rule, _)| {
if !rule.should_run(&ctx_host) {
return false;
}
// Skip tsgolint rules in Rust linter
if rule.is_tsgolint_rule() {
return false;
}
// Skip rules that only run on nodes that this file does not contain
let (ast_types, all_types, only_runs_on_nodes) = rule.types_info();
if !all_types
&& only_runs_on_nodes
&& !ctx_host.semantic().nodes().contains_any(ast_types)
{
return false;
}
true
})
.map(|(rule, severity)| (rule, Rc::clone(&ctx_host).spawn(rule, *severity)));

let semantic = ctx_host.semantic();
Expand Down Expand Up @@ -183,7 +200,7 @@ impl Linter {
// Collect node type information for rules. In large files, benchmarking showed it was worth
// collecting rules into buckets by AST node type to avoid iterating over all rules for each node.
if rule.should_run(&ctx_host) {
let (ast_types, all_types) = rule.types_info();
let (ast_types, all_types, _) = rule.types_info();
if all_types {
rules_any_ast_type.push((rule, ctx));
} else {
Expand Down Expand Up @@ -229,7 +246,7 @@ impl Linter {

// For smaller files, benchmarking showed it was faster to iterate over all rules and just check the
// node types as we go, rather than pre-bucketing rules by AST node type and doing extra allocations.
let (ast_types, all_types) = rule.types_info();
let (ast_types, all_types, _) = rule.types_info();
if all_types {
for node in semantic.nodes() {
rule.run(node, ctx);
Expand Down
11 changes: 8 additions & 3 deletions crates/oxc_linter/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ pub trait RuleRunner: Rule {
const NODE_TYPES: &AstTypesBitset;
/// `true` if codegen can't figure out what node types rule acts on
const ANY_NODE_TYPE: bool;
/// `true` if this rule only has a `run` implementation and does not implement
/// `run_on_symbol`, `run_once`, or `run_on_jest_node`.
const ONLY_RUNS_ON_NODES: bool = false;

fn types_info(&self) -> (&'static AstTypesBitset, bool) {
(Self::NODE_TYPES, Self::ANY_NODE_TYPE)
fn types_info(&self) -> (&'static AstTypesBitset, bool, bool) {
(Self::NODE_TYPES, Self::ANY_NODE_TYPE, Self::ONLY_RUNS_ON_NODES)
}
}
pub trait RuleMeta {
Expand Down Expand Up @@ -410,13 +413,15 @@ mod test {
&unicorn::no_zero_fractions::NoZeroFractions,
&[NumericLiteral],
);

assert!(!&jest::max_expects::MaxExpects::ONLY_RUNS_ON_NODES);
}

fn assert_rule_runs_on_node_types<R: RuleMeta + RuleRunner>(
rule: &R,
node_types: &[oxc_ast::AstType],
) {
let (types, _) = rule.types_info();
let (types, _, _) = rule.types_info();
assert!(!R::ANY_NODE_TYPE, "{} should not have ANY_NODE_TYPE set to true", R::NAME);
for node_type in node_types {
assert!(
Expand Down
Loading
Loading