2 * @fileoverview Rule to disallow certain object properties
3 * @author Will Klein & Eli White
8 const astUtils = require("./utils/ast-utils");
10 //------------------------------------------------------------------------------
12 //------------------------------------------------------------------------------
19 description: "disallow certain properties on certain objects",
20 category: "Best Practices",
22 url: "https://eslint.org/docs/rules/no-restricted-properties"
28 anyOf: [ // `object` and `property` are both optional, but at least one of them must be provided.
42 additionalProperties: false,
58 additionalProperties: false,
59 required: ["property"]
68 const restrictedCalls = context.options;
70 if (restrictedCalls.length === 0) {
74 const restrictedProperties = new Map();
75 const globallyRestrictedObjects = new Map();
76 const globallyRestrictedProperties = new Map();
78 restrictedCalls.forEach(option => {
79 const objectName = option.object;
80 const propertyName = option.property;
82 if (typeof objectName === "undefined") {
83 globallyRestrictedProperties.set(propertyName, { message: option.message });
84 } else if (typeof propertyName === "undefined") {
85 globallyRestrictedObjects.set(objectName, { message: option.message });
87 if (!restrictedProperties.has(objectName)) {
88 restrictedProperties.set(objectName, new Map());
91 restrictedProperties.get(objectName).set(propertyName, {
92 message: option.message
98 * Checks to see whether a property access is restricted, and reports it if so.
99 * @param {ASTNode} node The node to report
100 * @param {string} objectName The name of the object
101 * @param {string} propertyName The name of the property
102 * @returns {undefined}
104 function checkPropertyAccess(node, objectName, propertyName) {
105 if (propertyName === null) {
108 const matchedObject = restrictedProperties.get(objectName);
109 const matchedObjectProperty = matchedObject ? matchedObject.get(propertyName) : globallyRestrictedObjects.get(objectName);
110 const globalMatchedProperty = globallyRestrictedProperties.get(propertyName);
112 if (matchedObjectProperty) {
113 const message = matchedObjectProperty.message ? ` ${matchedObjectProperty.message}` : "";
117 // eslint-disable-next-line eslint-plugin/report-message-format
118 message: "'{{objectName}}.{{propertyName}}' is restricted from being used.{{message}}",
125 } else if (globalMatchedProperty) {
126 const message = globalMatchedProperty.message ? ` ${globalMatchedProperty.message}` : "";
130 // eslint-disable-next-line eslint-plugin/report-message-format
131 message: "'{{propertyName}}' is restricted from being used.{{message}}",
141 * Checks property accesses in a destructuring assignment expression, e.g. `var foo; ({foo} = bar);`
142 * @param {ASTNode} node An AssignmentExpression or AssignmentPattern node
143 * @returns {undefined}
145 function checkDestructuringAssignment(node) {
146 if (node.right.type === "Identifier") {
147 const objectName = node.right.name;
149 if (node.left.type === "ObjectPattern") {
150 node.left.properties.forEach(property => {
151 checkPropertyAccess(node.left, objectName, astUtils.getStaticPropertyName(property));
158 MemberExpression(node) {
159 checkPropertyAccess(node, node.object && node.object.name, astUtils.getStaticPropertyName(node));
161 VariableDeclarator(node) {
162 if (node.init && node.init.type === "Identifier") {
163 const objectName = node.init.name;
165 if (node.id.type === "ObjectPattern") {
166 node.id.properties.forEach(property => {
167 checkPropertyAccess(node.id, objectName, astUtils.getStaticPropertyName(property));
172 AssignmentExpression: checkDestructuringAssignment,
173 AssignmentPattern: checkDestructuringAssignment