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 index 3ab4f3b5e..d0ae38150 100644 --- 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 @@ -88,6 +88,15 @@ impl<'a> ListenerMap for Statement<'a> { no_effects(); } } + Self::TryStatement(stmt) => { + stmt.block.body.iter().for_each(|stmt| stmt.report_effects(options)); + stmt.handler.iter().for_each(|handler| { + handler.body.body.iter().for_each(|stmt| stmt.report_effects(options)); + }); + stmt.finalizer.iter().for_each(|finalizer| { + finalizer.body.iter().for_each(|stmt| stmt.report_effects(options)); + }); + } _ => {} } } @@ -212,6 +221,9 @@ impl<'a> ListenerMap for Expression<'a> { Self::NewExpression(expr) => { expr.report_effects(options); } + Self::AwaitExpression(expr) => { + expr.argument.report_effects(options); + } Self::ArrowFunctionExpression(_) | Self::FunctionExpression(_) | Self::Identifier(_) diff --git a/crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/mod.rs b/crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/mod.rs index 652343bb7..bd9cdfc4d 100644 --- a/crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/mod.rs +++ b/crates/oxc_linter/src/rules/tree_shaking/no_side_effects_in_initialization/mod.rs @@ -119,11 +119,11 @@ fn test() { "function x(){this.y = 1}; const z = new x()", "let x = 1; x = 2 + 3", "let x; x = 2 + 3", - // // AssignmentPattern - // "const {x = ext} = {}", - // "const {x: y = ext} = {}", - // "const {[ext]: x = ext} = {}", - // "const x = ()=>{}, {y = x()} = {}", + // AssignmentPattern + "const {x = ext} = {}", + "const {x: y = ext} = {}", + "const {[ext]: x = ext} = {}", + "const x = ()=>{}, {y = x()} = {}", // // BinaryExpression // "const x = 1 + 2", // "if (1-1) ext()", @@ -137,9 +137,9 @@ fn test() { "(a=>{const y = a})(ext, ext)", "const x = ()=>{}, y = ()=>{}; x(y())", // // CatchClause - // "try {} catch (error) {}", - // "const x = ()=>{}; try {} catch (error) {const x = ext}; x()", - // "const x = ext; try {} catch (error) {const x = ()=>{}; x()}", + "try {} catch (error) {}", + "const x = ()=>{}; try {} catch (error) {const x = ext}; x()", + "const x = ext; try {} catch (error) {const x = ()=>{}; x()}", // // ClassBody // "class x {a(){ext()}}", // // ClassBody when called @@ -387,11 +387,11 @@ fn test() { "ext.x = 1", "const x = {};x[ext()] = 1", "this.x = 1", - // // AssignmentPattern - // "const {x = ext()} = {}", - // "const {y: {x = ext()} = {}} = {}", - // // AwaitExpression - // "const x = async ()=>{await ext()}; x()", + // AssignmentPattern + "const {x = ext()} = {}", + "const {y: {x = ext()} = {}} = {}", + // AwaitExpression + "const x = async ()=>{await ext()}; x()", // // BinaryExpression // "const x = 1 + ext()", // "const x = ext() + 1", @@ -406,8 +406,9 @@ fn test() { "const x = ()=>ext; const y = x(); y()", // CallExpression when mutated "const x = ()=>ext; const y = x(); y.z = 1", - // // CatchClause - // "try {} catch (error) {ext()}", + // CatchClause + "try {} catch (error) {ext()}", + // TODO: check global function `ext` call when called `x()` in no strict mode // "var x=()=>{}; try {} catch (error) {var x=ext}; x()", // // ClassBody // "class x {[ext()](){}}", diff --git a/crates/oxc_linter/src/snapshots/no_side_effects_in_initialization.snap b/crates/oxc_linter/src/snapshots/no_side_effects_in_initialization.snap index e9226b9bf..cce528590 100644 --- a/crates/oxc_linter/src/snapshots/no_side_effects_in_initialization.snap +++ b/crates/oxc_linter/src/snapshots/no_side_effects_in_initialization.snap @@ -110,6 +110,24 @@ expression: no_side_effects_in_initialization · ──── ╰──── + ⚠ eslint-plugin-tree-shaking(no-side-effects-in-initialization): Cannot determine side-effects of calling global function `ext` + ╭─[no_side_effects_in_initialization.tsx:1:12] + 1 │ const {x = ext()} = {} + · ─── + ╰──── + + ⚠ eslint-plugin-tree-shaking(no-side-effects-in-initialization): Cannot determine side-effects of calling global function `ext` + ╭─[no_side_effects_in_initialization.tsx:1:16] + 1 │ const {y: {x = ext()} = {}} = {} + · ─── + ╰──── + + ⚠ eslint-plugin-tree-shaking(no-side-effects-in-initialization): Cannot determine side-effects of calling global function `ext` + ╭─[no_side_effects_in_initialization.tsx:1:28] + 1 │ const x = async ()=>{await ext()}; x() + · ─── + ╰──── + ⚠ eslint-plugin-tree-shaking(no-side-effects-in-initialization): Cannot determine side-effects of calling global function `ext` ╭─[no_side_effects_in_initialization.tsx:1:10] 1 │ (()=>{})(ext(), 1) @@ -134,6 +152,12 @@ expression: no_side_effects_in_initialization · ─── ╰──── + ⚠ eslint-plugin-tree-shaking(no-side-effects-in-initialization): Cannot determine side-effects of calling global function `ext` + ╭─[no_side_effects_in_initialization.tsx:1:23] + 1 │ try {} catch (error) {ext()} + · ─── + ╰──── + ⚠ eslint-plugin-tree-shaking(no-side-effects-in-initialization): Cannot determine side-effects of calling global function `ext` ╭─[no_side_effects_in_initialization.tsx:1:15] 1 │ const x = new ext()