mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(linter): report side effect for array element in node_side_effects rule (#2683)
This commit is contained in:
parent
366a87975d
commit
3ae94795a1
1 changed files with 66 additions and 2 deletions
|
|
@ -1,3 +1,7 @@
|
|||
use oxc_ast::{
|
||||
ast::{ArrayExpressionElement, Expression, Program, Statement},
|
||||
AstKind,
|
||||
};
|
||||
use oxc_diagnostics::{
|
||||
miette::{self, Diagnostic},
|
||||
thiserror::Error,
|
||||
|
|
@ -5,7 +9,7 @@ use oxc_diagnostics::{
|
|||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::Span;
|
||||
|
||||
use crate::{context::LintContext, rule::Rule, AstNode};
|
||||
use crate::{context::LintContext, rule::Rule};
|
||||
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
#[error(
|
||||
|
|
@ -40,7 +44,67 @@ declare_oxc_lint!(
|
|||
);
|
||||
|
||||
impl Rule for NoSideEffectsInInitialization {
|
||||
fn run<'a>(&self, _node: &AstNode<'a>, _ctx: &LintContext<'a>) {}
|
||||
fn run_once(&self, ctx: &LintContext) {
|
||||
let Some(root) = ctx.nodes().iter().next() else { return };
|
||||
let AstKind::Program(program) = root.kind() else { return };
|
||||
|
||||
program.report_effects(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// 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]
|
||||
|
|
|
|||
Loading…
Reference in a new issue