Index

id-denylist

不允许指定的标识符

❄️ Frozen

此规则目前已 冻结,不接受功能请求。

“计算机科学中只有两件难事:缓存失效和命名东西。” — 菲尔·卡尔顿

通用名称可能导致难以理解的代码。此规则允许你指定不允许的标识符名称的拒绝列表,以避免这种做法。

🌐 Generic names can lead to hard-to-decipher code. This rule allows you to specify a deny list of disallowed identifier names to avoid this practice.

规则详情

🌐 Rule Details

此规则禁止在赋值和 function 定义中使用指定的标识符。

🌐 This rule disallows specified identifiers in assignments and function definitions.

此规则将捕获不允许的标识符,这些标识符是:

🌐 This rule will catch disallowed identifiers that are:

  • 变量声明
  • 函数声明
  • 在对象创建期间分配给的对象属性
  • 类字段
  • 类方法

它不会捕获不允许的标识符:

🌐 It will not catch disallowed identifiers that are:

  • 函数调用(所以你仍然可以使用你无法控制的函数)
  • 对象属性(因此你仍然可以使用你无法控制的对象)

选项

🌐 Options

该规则将一个或多个字符串作为选项:受限制标识符的名称。

🌐 The rule takes one or more strings as options: the names of restricted identifiers.

例如,要限制使用常见的泛型恒等符:

🌐 For example, to restrict the use of common generic identifiers:

{
    "id-denylist": ["error", "data", "err", "e", "cb", "callback"]
}

注意: 数组的第一个元素用于规则的严重性(参见 配置规则)。数组中的其他元素是你想要禁止的标识符。

此规则的错误代码示例,包含示例 "data", "callback" 受限标识符:

🌐 Examples of incorrect code for this rule with sample "data", "callback" restricted identifiers:

在线运行
/*eslint id-denylist: ["error", "data", "callback"] */

const data = { ...values };

function callback() {
    // ...
}

element.callback = function() {
    // ...
};

const itemSet = {
    data: [...values]
};

class Foo {
    data = [];
}

class Bar {
    #data = [];
}

class Baz {
    callback() {}
}

class Qux {
    #callback() {}
}

此规则的正确代码示例,包含示例 "data", "callback" 受限标识符:

🌐 Examples of correct code for this rule with sample "data", "callback" restricted identifiers:

在线运行
/*eslint id-denylist: ["error", "data", "callback"] */

const encodingOptions = {...values};

function processFileResult() {
    // ...
}

element.successHandler = function() {
    // ...
};

const itemSet = {
    entities: [...values]
};

callback(); // all function calls are ignored

foo.callback(); // all function calls are ignored

foo.data; // all property names that are not assignments are ignored

class Foo {
    items = [];
}

class Bar {
    #items = [];
}

class Baz {
    method() {}
}

class Qux {
    #method() {}
}

何时不使用

🌐 When Not To Use It

如果你不想限制某些标识符的使用,你可以关闭此规则。

🌐 You can turn this rule off if you do not want to restrict the use of certain identifiers.

版本

此规则是在 ESLint v7.4.0 中引入。

资源