Skip to content
← Back to rules

eslint/no-unsafe-finally Correctness

This rule is turned on by default.

它的作用

禁止在 finally 块中使用控制流语句。

为什么这不好?

JavaScript 会暂停 trycatch 块中的控制流语句,直到 finally 块执行完毕。

因此,当在 finally 中使用 returnthrowbreakcontinue 时, trycatch 中的控制流语句会被覆盖。 这可能是开发者意料之外的行为。

示例

以下是此规则的错误代码示例:

javascript
// 我们期望这个函数返回 1;
(() => {
  try {
    return 1; // 返回 1,但会被挂起,直到 finally 块结束
  } catch (err) {
    return 2;
  } finally {
    return 3; // 3 会先于 1 返回,这不是我们预期的
  }
})();

// > 3

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "no-unsafe-finally": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-unsafe-finally": "error",
  },
});
bash
oxlint --deny no-unsafe-finally

版本

此规则于 v0.0.5 中添加。

参考资料