-
-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lint): split files for no_side_effects rule (#2684)
- Loading branch information
1 parent
3ae9479
commit 220eba1
Showing
4 changed files
with
63 additions
and
60 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/listener_map.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use oxc_ast::ast::{ArrayExpressionElement, Expression, Program, Statement}; | ||
|
||
use crate::{utils::Value, LintContext}; | ||
|
||
pub trait ListenerMap { | ||
fn report_effects(&self, _ctx: &LintContext) {} | ||
fn report_effects_when_assigned(&self, _ctx: &LintContext) {} | ||
fn report_effects_when_called(&self, _ctx: &LintContext) {} | ||
fn report_effects_when_mutated(&self, _ctx: &LintContext) {} | ||
fn get_value_and_report_effects(&self, _ctx: &LintContext) -> Option<Value> { | ||
None | ||
} | ||
} | ||
|
||
impl<'a> ListenerMap for Program<'a> { | ||
fn report_effects(&self, ctx: &LintContext) { | ||
self.body.iter().for_each(|stmt| stmt.report_effects(ctx)); | ||
} | ||
} | ||
|
||
impl<'a> ListenerMap for Statement<'a> { | ||
fn report_effects(&self, ctx: &LintContext) { | ||
if let Self::ExpressionStatement(expr_stmt) = self { | ||
expr_stmt.expression.report_effects(ctx); | ||
} | ||
} | ||
} | ||
|
||
impl<'a> ListenerMap for Expression<'a> { | ||
fn report_effects(&self, ctx: &LintContext) { | ||
#[allow(clippy::single_match)] | ||
match self { | ||
Self::ArrayExpression(array_expr) => { | ||
array_expr.elements.iter().for_each(|el| el.report_effects(ctx)); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
impl<'a> ListenerMap for ArrayExpressionElement<'a> { | ||
fn report_effects(&self, ctx: &LintContext) { | ||
match self { | ||
Self::Expression(expr) => expr.report_effects(ctx), | ||
Self::SpreadElement(spreed) => { | ||
spreed.argument.report_effects(ctx); | ||
} | ||
Self::Elision(_) => {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[allow(dead_code)] | ||
pub enum Value { | ||
Boolean(bool), | ||
Number(f64), | ||
} |