feat(linter/tree-shaking): support try-catch and AwaitExpression (#2902)

This commit is contained in:
Wang Wenzhe 2024-04-06 14:02:42 +08:00 committed by GitHub
parent 59869d0a96
commit b053d54b6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 52 additions and 15 deletions

View file

@ -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(_)

View file

@ -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()](){}}",

View file

@ -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()