noUnsafeFinally
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/correctness/noUnsafeFinally¥Diagnostic Category:
lint/correctness/noUnsafeFinally -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
-
来源:
¥Sources:
-
与
no-unsafe-finally相同¥Same as
no-unsafe-finally
-
¥How to configure
{ "linter": { "rules": { "correctness": { "noUnsafeFinally": "error" } } }}¥Description
禁止在 finally 块中控制流语句。
¥Disallow control flow statements in finally blocks.
JavaScript 暂停 try 和 catch 块的控制流语句,直到 finally 块执行完成。所以,当在 finally 中使用 return、throw、break 或 continue 时,try 和 catch 内的控制流语句将被覆盖,这被视为意外行为。
¥JavaScript suspends the control flow statements of try and catch blocks until
the execution of finally block finishes. So, when return, throw, break or continue
is used in finally, control flow statements inside try and catch are overwritten,
which is considered as unexpected behavior.
¥Examples
¥Invalid
(() => { try { return 1; // 1 is returned but suspended until finally block ends } catch(err) { return 2; } finally { return 3; // 3 is returned before 1, which we did not expect }})();code-block.js:7:9 lint/correctness/noUnsafeFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of ‘return’.
5 │ return 2;
6 │ } finally {
> 7 │ return 3; // 3 is returned before 1, which we did not expect
│ ^^^^^^^^^
8 │ }
9 │ })();
ℹ ‘return’ in ‘finally’ overwrites the control flow statements inside ‘try’ and ‘catch’.
(() => { try { throw new Error("Try"); // error is thrown but suspended until finally block ends } finally { return 3; // 3 is returned before the error is thrown, which we did not expect }})();code-block.js:5:9 lint/correctness/noUnsafeFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of ‘return’.
3 │ throw new Error(“Try”); // error is thrown but suspended until finally block ends
4 │ } finally {
> 5 │ return 3; // 3 is returned before the error is thrown, which we did not expect
│ ^^^^^^^^^
6 │ }
7 │ })();
ℹ ‘return’ in ‘finally’ overwrites the control flow statements inside ‘try’ and ‘catch’.
(() => { try { throw new Error("Try") } catch(err) { throw err; // The error thrown from try block is caught and re-thrown } finally { throw new Error("Finally"); // Finally(...) is thrown, which we did not expect }})();code-block.js:7:9 lint/correctness/noUnsafeFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of ‘throw’.
5 │ throw err; // The error thrown from try block is caught and re-thrown
6 │ } finally {
> 7 │ throw new Error(“Finally”); // Finally(…) is thrown, which we did not expect
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
8 │ }
9 │ })();
ℹ ‘throw’ in ‘finally’ overwrites the control flow statements inside ‘try’ and ‘catch’.
(() => { label: try { return 0; // 0 is returned but suspended until finally block ends } finally { break label; // It breaks out the try-finally block, before 0 is returned. } return 1;})();code-block.js:5:7 lint/correctness/noUnsafeFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of ‘break’.
3 │ return 0; // 0 is returned but suspended until finally block ends
4 │ } finally {
> 5 │ break label; // It breaks out the try-finally block, before 0 is returned.
│ ^^^^^^^^^^^^
6 │ }
7 │ return 1;
ℹ ‘break’ in ‘finally’ overwrites the control flow statements inside ‘try’ and ‘catch’.
function a() { switch (condition) { case 'a': { try { console.log('a'); return; } finally { break; } } case 'b': { console.log('b'); } }}code-block.js:8:9 lint/correctness/noUnsafeFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of ‘break’.
6 │ return;
7 │ } finally {
> 8 │ break;
│ ^^^^^^
9 │ }
10 │ }
ℹ ‘break’ in ‘finally’ overwrites the control flow statements inside ‘try’ and ‘catch’.
¥Valid
let foo = function() { try { return 1; } catch(err) { return 2; } finally { console.log("hola!"); }};let foo = function() { try { return 1; } catch(err) { return 2; } finally { let a = function() { return "hola!"; } }};let foo = function(a) { try { return 1; } catch(err) { return 2; } finally { switch(a) { case 1: { console.log("hola!") break; } } }};¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号