no-restricted-properties

禁止某些对象的某些属性

代码库中可能不允许对象的某些属性。这对于弃用 API 或限制模块方法的使用很有用。例如,您可能希望在使用 Mocha 时禁止使用 describe.only,或者告诉人们使用 Object.assign 而不是 _.extend

规则详情

此规则查找访问给定对象名称上的给定属性键,无论是在读取属性值还是将其作为函数调用时。您可以指定可选消息来指示替代 API 或限制原因。

选项

此规则采用对象列表,其中指定了对象名称和属性名称:

{
    "rules": {
        "no-restricted-properties": [2, {
            "object": "disallowedObjectName",
            "property": "disallowedPropertyName"
        }]
    }
}

可以禁止多个对象/属性值,您可以指定可选消息:

{
    "rules": {
        "no-restricted-properties": [2, {
            "object": "disallowedObjectName",
            "property": "disallowedPropertyName"
        }, {
            "object": "disallowedObjectName",
            "property": "anotherDisallowedPropertyName",
            "message": "Please use allowedObjectName.allowedPropertyName."
        }]
    }
}

OqHk7iMULYBd+myfugKjUab3Dk5QKe9UgfxZs7XyKqaPpmuLt0UrtjGJaR8u9/spY70Of3Begu0lWS4IFDSXSvcpGlt/ysX0+xU32pkKyME=

{
    "rules": {
        "no-restricted-properties": [2, {
            "property": "__defineGetter__",
            "message": "Please use Object.defineProperty instead."
        }]
    }
}

WfKB5n5fxkwV4rzYOK8tPFShA8qAXnlnRDLZRdqdl+EJU+OpSa3JtQZKNdj9ngRYyHv0xvVMeiUPYT+txjK4s6NhpcX5UpALh/CC5zBpOhQ=

{
    "rules": {
        "no-restricted-properties": [2, {
            "object": "require",
            "message": "Please call require() directly."
        }]
    }
}

mmxLFhqxXZjHj18gill8d7xoj4WPKV1qaR8noXngSr3Gyf1pD8gWIrWYZavPBCih

/* eslint no-restricted-properties: [2, {
    "object": "disallowedObjectName",
    "property": "disallowedPropertyName"
}] */

var example = disallowedObjectName.disallowedPropertyName; /*error Disallowed object property: disallowedObjectName.disallowedPropertyName.*/

disallowedObjectName.disallowedPropertyName(); /*error Disallowed object property: disallowedObjectName.disallowedPropertyName.*/
/* eslint no-restricted-properties: [2, {
    "property": "__defineGetter__"
}] */

foo.__defineGetter__(bar, baz);
/* eslint no-restricted-properties: [2, {
    "object": "require"
}] */

require.resolve('foo');

+2jabC1cuAvyDUwnGxKcmwB1ejOLGUWMWD7qViNHzWwgHar8cUzqr2bYbCOTvm1P

/* eslint no-restricted-properties: [2, {
    "object": "disallowedObjectName",
    "property": "disallowedPropertyName"
}] */

var example = disallowedObjectName.somePropertyName;

allowedObjectName.disallowedPropertyName();
/* eslint no-restricted-properties: [2, {
    "object": "require"
}] */

require('foo');

何时不使用

8UW2DAOe8qOK6kBx8N8gwimgsUotbxD6zPxiGBLfpS7ZhkzXB+EBQPNTe+kHaNojcYEFpDM4iNcuWMTBuR1yJDW1HF8lzB0tB4yU2ZAKhcnf+ay8emSmcbjedb67hNQa