noUnsafeFinally
Ce contenu n’est pas encore disponible dans votre langue.
Diagnostic Category: lint/correctness/noUnsafeFinally
Since: v1.0.0
Sources:
- Same as:
no-unsafe-finally
Disallow control flow statements in finally blocks.
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
Section titled ExamplesInvalid
Section titled Invalidcode-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’.