src/content/docs/linter/rules/no-unsafe-finally.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/correctness/noUnsafeFinally`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`no-unsafe-finally`](https://eslint.org/docs/latest/rules/no-unsafe-finally){
"linter": {
"rules": {
"correctness": {
"noUnsafeFinally": "error"
}
}
}
}
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.
(() => {
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
}
})();
(() => {
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
}
})();
(() => {
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
}
})();
(() => {
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;
})();
function a() {
switch (condition) {
case 'a': {
try {
console.log('a');
return;
} finally {
break;
}
}
case 'b': {
console.log('b');
}
}
}
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;
}
}
}
};