Skip to content

Commit

Permalink
refactor(lint): split files for no_side_effects rule (#2684)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven authored Mar 12, 2024
1 parent 3ae9479 commit 220eba1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 60 deletions.
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(_) => {}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use oxc_ast::{
ast::{ArrayExpressionElement, Expression, Program, Statement},
AstKind,
};
use oxc_ast::AstKind;
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
Expand All @@ -11,6 +8,10 @@ use oxc_span::Span;

use crate::{context::LintContext, rule::Rule};

use self::listener_map::ListenerMap;

mod listener_map;

#[derive(Debug, Error, Diagnostic)]
#[error(
"eslint-plugin-tree-shaking(no-side-effects-in-initialization): cannot determine side-effects"
Expand Down Expand Up @@ -52,61 +53,6 @@ impl Rule for NoSideEffectsInInitialization {
}
}

// TODO: add more type
#[allow(dead_code)]
enum Value {
Boolean(bool),
Number(f64),
}

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(_) => {}
}
}
}

#[ignore]
#[test]
fn test() {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod nextjs;
mod node;
mod react;
mod react_perf;
mod tree_shaking;
mod unicorn;

pub use self::{jest::*, nextjs::*, node::*, react::*, react_perf::*, unicorn::*};
pub use self::{jest::*, nextjs::*, node::*, react::*, react_perf::*, tree_shaking::*, unicorn::*};
5 changes: 5 additions & 0 deletions crates/oxc_linter/src/utils/tree_shaking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[allow(dead_code)]
pub enum Value {
Boolean(bool),
Number(f64),
}

0 comments on commit 220eba1

Please sign in to comment.