id-denylist

不允许指定的标识符

“计算机科学中只有两件事是困难的:缓存失效和命名。” - Phil Karlton

¥"There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton

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

¥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:

  • 变量声明

    ¥variable declarations

  • 函数声明

    ¥function declarations

  • 在对象创建期间分配给的对象属性

    ¥object properties assigned to during object creation

  • 类字段

    ¥class fields

  • 类方法

    ¥class methods

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

¥It will not catch disallowed identifiers that are:

  • 函数调用(所以你仍然可以使用你无法控制的函数)

    ¥function calls (so you can still use functions you do not have control over)

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

    ¥object properties (so you can still use objects you do not have control over)

选项

¥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"]
}

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

¥Note: The first element of the array is for the rule severity (see Configure Rules. The other elements in the array are the identifiers that you want to disallow.

此规则的错误代码示例(具有示例 "data", "callback" 受限标识符):

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

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

var data = { ...values };

function callback() {
    // ...
}

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

var 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"] */

var encodingOptions = {...values};

function processFileResult() {
    // ...
}

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

var 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 中引入。

资源

ESLint 中文网
粤ICP备13048890号