noUnsafeFinally
诊断类别:lint/correctness/noUnsafeFinally
¥Diagnostic Category: lint/correctness/noUnsafeFinally
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
-
与以下相同:
no-unsafe-finally
¥Same as:
no-unsafe-finally
禁止在 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
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’.
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’.
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’.
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’.
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
¥Related links