fix(rome_js_analyze): only emit control flow side-effects for variable declarators with an initializer
MODIFY
crates/rome_js_analyze/src/control_flow/nodes.rs
crates/rome_js_analyze/src/control_flow/nodes.rs
@@ -11,6 +11,7 @@ mod statement;
mod switch_stmt;
mod throw_stmt;
mod try_catch;
+mod variable;
mod while_stmt;
pub(super) use block::*;
@@ -26,4 +27,5 @@ pub(super) use statement::*;
pub(super) use switch_stmt::*;
pub(super) use throw_stmt::*;
pub(super) use try_catch::*;
+pub(super) use variable::*;
pub(super) use while_stmt::*;

MODIFY
crates/rome_js_analyze/src/control_flow/nodes/statement.rs
crates/rome_js_analyze/src/control_flow/nodes/statement.rs
-use rome_js_syntax::{
- JsDebuggerStatement, JsEmptyStatement, JsExpressionStatement, JsVariableStatement,
-};
+use rome_js_syntax::{JsDebuggerStatement, JsEmptyStatement, JsExpressionStatement};
use rome_rowan::{declare_node_union, AstNode, SyntaxResult};
use crate::control_flow::{
@@ -9,7 +7,7 @@ use crate::control_flow::{
};
declare_node_union! {
- pub(in crate::control_flow) JsSimpleStatement = JsDebuggerStatement | JsEmptyStatement | JsExpressionStatement | JsVariableStatement
+ pub(in crate::control_flow) JsSimpleStatement = JsDebuggerStatement | JsEmptyStatement | JsExpressionStatement
}
pub(in crate::control_flow) struct StatementVisitor;

ADD
crates/rome_js_analyze/src/control_flow/nodes/variable.rs
+use rome_js_syntax::JsVariableStatement;
+use rome_rowan::{AstNode, SyntaxResult};
+
+use crate::control_flow::{
+ visitor::{NodeVisitor, StatementStack},
+ FunctionBuilder,
+};
+
+pub(in crate::control_flow) struct VariableVisitor;
+
+impl<B> NodeVisitor<B> for VariableVisitor {
+ type Node = JsVariableStatement;
+
+ fn enter(
+ node: Self::Node,
+ builder: &mut FunctionBuilder,
+ _: StatementStack,
+ ) -> SyntaxResult<Self> {
+ let declaration = node.declaration()?;
+ for declarator in declaration.declarators() {
+ if let Some(initializer) = declarator?.initializer() {
+ let expr = initializer.expression()?;
+ builder.append_statement().with_node(expr.into_syntax());
+ }
+ }
+
+ Ok(Self)
+ }
+}

MODIFY
crates/rome_js_analyze/src/control_flow/visitor.rs
crates/rome_js_analyze/src/control_flow/visitor.rs
@@ -102,6 +102,7 @@ declare_visitor! {
continue_stmt: ContinueVisitor,
return_stmt: ReturnVisitor,
throw: ThrowVisitor,
+ variable: VariableVisitor,
}
}

ADD
crates/rome_js_analyze/tests/specs/noDeadCode/JsVariableStatement.js
+function JsVariableStatement1() {
+ return;
+ var variable;
+}
+
+function JsVariableStatement2() {
+ return;
+ var variable = initializer();
+}
+
+function JsVariableStatement3() {
+ return;
+ let variable;
+}
+
+function JsVariableStatement4() {
+ return;
+ let variable = initializer();
+}
+
+function JsVariableStatement5() {
+ return;
+ const variable = initializer();
+}
+
+function JsVariableStatement6() {
+ return;
+ var variable1 = initializer(),
+ variable2 = initializer();
+}

fix(rome_js_analyze): only emit control flow side-effects for variable declarators with an initializer (#2870)