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."
}]
}
}
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/* 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);
const { __defineGetter__ } = qux();
({ __defineGetter__ }) => {};
/* eslint no-restricted-properties: [2, {
"object": "require"
}] */
require.resolve('foo');
此规则的正确代码示例:
¥Examples of correct code for this rule:
/* eslint no-restricted-properties: [2, {
"object": "disallowedObjectName",
"property": "disallowedPropertyName"
}] */
var example = disallowedObjectName.somePropertyName;
allowedObjectName.disallowedPropertyName();
/* eslint no-restricted-properties: [2, {
"object": "require"
}] */
require('foo');
何时不使用
¥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 中引入。