Skip to content
← Back to rules

promise/param-names Style

它的作用

强制 Promise 构造函数使用标准的参数名。

为什么这不好?

确保 new Promise() 使用参数名 resolve、reject 进行实例化,以避免与 reject、resolve 这类顺序混淆。Promise 构造函数使用揭示构造函数(RevealingConstructor)模式。使用与语言规范相同的参数名可以使代码更加统一,也更容易理解。

示例

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

javascript
new Promise(function (reject, resolve) {
  /* ... */
}); // 顺序错误
new Promise(function (ok, fail) {
  /* ... */
}); // 非标准参数名

以下是此规则下正确代码的示例:

javascript
new Promise(function (resolve, reject) {});

配置

此规则接受一个包含以下属性的配置对象:

rejectPattern

type: string

用于验证 reject 参数名的正则表达式模式。如果提供了该模式,则会使用它替代默认的 ^_?reject$ 检查。

resolvePattern

type: string

用于验证 resolve 参数名的正则表达式模式。如果提供了该模式,则会使用它替代默认的 ^_?resolve$ 检查。

如何使用

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

json
{
  "plugins": ["promise"],
  "rules": {
    "promise/param-names": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["promise"],
  rules: {
    "promise/param-names": "error",
  },
});
bash
oxlint --deny promise/param-names --promise-plugin

版本

此规则在 v0.6.1 中添加。

参考资料