no-restricted-properties

禁止某些对象的某些属性

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

¥Certain properties on objects may be disallowed in a codebase. This is useful for deprecating an API or restricting usage of a module’s methods. For example, you may want to disallow using describe.only when using Mocha or telling people to use Object.assign instead of _.extend.

规则详情

¥Rule Details

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

¥This rule looks for accessing a given property key on a given object name, either when reading the property’s value or invoking it as a function. You may specify an optional message to indicate an alternative API or a reason for the restriction. This rule applies to both properties accessed by dot notation and destructuring.

选项

¥Options

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

¥This rule takes a list of objects, where the object name and property names are specified:

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

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

¥Multiple object/property values can be disallowed, and you can specify an optional message:

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

如果省略对象名称,则所有对象都不允许使用该属性:

¥If the object name is omitted, the property is disallowed for all objects:

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

如果省略属性名称,则不允许访问给定对象的任何属性:

¥If the property name is omitted, accessing any property of the given object is disallowed:

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

如果要全局限制某个属性,但允许特定对象使用它,可以使用 allowObjects 选项:

¥If you want to restrict a property globally but allow specific objects to use it, you can use the allowObjects option:

{
    "rules": {
        "no-restricted-properties": [2, {
            "property": "push",
            "allowObjects": ["router"],
            "message": "Prefer [...array, newValue] because it does not mutate the array in place."
        }]
    }
}

如果你想限制对象上除特定属性之外的所有属性,可以使用 allowProperties 选项:

¥If you want to restrict all properties on an object except for specific ones, you can use the allowProperties option:

{
    "rules": {
        "no-restricted-properties": [2, {
            "object": "config",
            "allowProperties": ["settings", "version"],
            "message": "Accessing other properties is restricted."
        }]
    }
}

请注意,allowObjects 选项不能与 object 选项一起使用,因为它们是互斥的。同样,allowProperties 选项不能与 property 选项一起使用,因为它们也是互斥的。

¥Note that the allowObjects option cannot be used together with the object option since they are mutually exclusive. Similarly, the allowProperties option cannot be used together with the property option since they are also mutually exclusive.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

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

const 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);

const { __defineGetter__ } = qux();

({ __defineGetter__ }) => {};
在线运行
/* eslint no-restricted-properties: [2, {
    "object": "require"
}] */

require.resolve('foo');
在线运行
/* eslint no-restricted-properties: [2, {
    "property": "push",
    "allowObjects": ["router"],
}] */

myArray.push(5);
在线运行
/* eslint no-restricted-properties: [2, {
    "object": "config",
    "allowProperties": ["settings", "version"]
}] */

config.apiKey = "12345";
config.timeout = 5000;

此规则的正确代码示例:

¥Examples of correct code for this rule:

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

const example = disallowedObjectName.somePropertyName;

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

require('foo');
在线运行
/* eslint no-restricted-properties: [2, {
    "property": "push",
    "allowObjects": ["router", "history"],
}] */

router.push('/home');
history.push('/about');
在线运行
/* eslint no-restricted-properties: [2, {
    "object": "config",
    "allowProperties": ["settings", "version"]
}] */

config.settings = { theme: "dark" };
config.version = "1.0.0";  

何时不使用

¥When Not To Use It

如果你没有要限制的任何对象/属性组合,则不应使用此规则。

¥If you don’t have any object/property combinations to restrict, you should not use this rule.

版本

此规则是在 ESLint v3.5.0 中引入。

资源