Skip to content

Commit

Permalink
feat(tree-shaking): support try-catch and AwaitExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven committed Apr 6, 2024
1 parent 33de904 commit 5d2f1f7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
}
_ => {}
}
}
Expand Down Expand Up @@ -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(_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()",
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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()](){}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 5d2f1f7

Please sign in to comment.