From 220eba14cd1226430176fa7eaab4e146a3c031db Mon Sep 17 00:00:00 2001 From: Wang Wenzhe Date: Tue, 12 Mar 2024 11:09:33 +0800 Subject: [PATCH] refactor(lint): split files for no_side_effects rule (#2684) --- .../listener_map.rs | 51 +++++++++++++++ .../mod.rs} | 64 ++----------------- crates/oxc_linter/src/utils/mod.rs | 3 +- crates/oxc_linter/src/utils/tree_shaking.rs | 5 ++ 4 files changed, 63 insertions(+), 60 deletions(-) create mode 100644 crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/listener_map.rs rename crates/oxc_linter/src/rules/tree_shaking/{no_side_effects_in_initialization.rs => no_side_effects_in_initialization/mod.rs} (93%) create mode 100644 crates/oxc_linter/src/utils/tree_shaking.rs diff --git a/crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/listener_map.rs b/crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/listener_map.rs new file mode 100644 index 000000000..0cbd30b4e --- /dev/null +++ b/crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/listener_map.rs @@ -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 { + 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(_) => {} + } + } +} diff --git a/crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization.rs b/crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/mod.rs similarity index 93% rename from crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization.rs rename to crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/mod.rs index 40fdcded7..1ea6646b7 100644 --- a/crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization.rs +++ b/crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/mod.rs @@ -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, @@ -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" @@ -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 { - 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() { diff --git a/crates/oxc_linter/src/utils/mod.rs b/crates/oxc_linter/src/utils/mod.rs index bfbd1e9ec..03b28acc4 100644 --- a/crates/oxc_linter/src/utils/mod.rs +++ b/crates/oxc_linter/src/utils/mod.rs @@ -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::*}; diff --git a/crates/oxc_linter/src/utils/tree_shaking.rs b/crates/oxc_linter/src/utils/tree_shaking.rs new file mode 100644 index 000000000..3cba28a14 --- /dev/null +++ b/crates/oxc_linter/src/utils/tree_shaking.rs @@ -0,0 +1,5 @@ +#[allow(dead_code)] +pub enum Value { + Boolean(bool), + Number(f64), +}