no-restricted-properties.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @fileoverview Rule to disallow certain object properties
  3. * @author Will Klein & Eli White
  4. */
  5. "use strict";
  6. const astUtils = require("./utils/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. /** @type {import('../shared/types').Rule} */
  11. module.exports = {
  12. meta: {
  13. type: "suggestion",
  14. docs: {
  15. description: "Disallow certain properties on certain objects",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/no-restricted-properties"
  18. },
  19. schema: {
  20. type: "array",
  21. items: {
  22. anyOf: [ // `object` and `property` are both optional, but at least one of them must be provided.
  23. {
  24. type: "object",
  25. properties: {
  26. object: {
  27. type: "string"
  28. },
  29. property: {
  30. type: "string"
  31. },
  32. message: {
  33. type: "string"
  34. }
  35. },
  36. additionalProperties: false,
  37. required: ["object"]
  38. },
  39. {
  40. type: "object",
  41. properties: {
  42. object: {
  43. type: "string"
  44. },
  45. property: {
  46. type: "string"
  47. },
  48. message: {
  49. type: "string"
  50. }
  51. },
  52. additionalProperties: false,
  53. required: ["property"]
  54. }
  55. ]
  56. },
  57. uniqueItems: true
  58. },
  59. messages: {
  60. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  61. restrictedObjectProperty: "'{{objectName}}.{{propertyName}}' is restricted from being used.{{message}}",
  62. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  63. restrictedProperty: "'{{propertyName}}' is restricted from being used.{{message}}"
  64. }
  65. },
  66. create(context) {
  67. const restrictedCalls = context.options;
  68. if (restrictedCalls.length === 0) {
  69. return {};
  70. }
  71. const restrictedProperties = new Map();
  72. const globallyRestrictedObjects = new Map();
  73. const globallyRestrictedProperties = new Map();
  74. restrictedCalls.forEach(option => {
  75. const objectName = option.object;
  76. const propertyName = option.property;
  77. if (typeof objectName === "undefined") {
  78. globallyRestrictedProperties.set(propertyName, { message: option.message });
  79. } else if (typeof propertyName === "undefined") {
  80. globallyRestrictedObjects.set(objectName, { message: option.message });
  81. } else {
  82. if (!restrictedProperties.has(objectName)) {
  83. restrictedProperties.set(objectName, new Map());
  84. }
  85. restrictedProperties.get(objectName).set(propertyName, {
  86. message: option.message
  87. });
  88. }
  89. });
  90. /**
  91. * Checks to see whether a property access is restricted, and reports it if so.
  92. * @param {ASTNode} node The node to report
  93. * @param {string} objectName The name of the object
  94. * @param {string} propertyName The name of the property
  95. * @returns {undefined}
  96. */
  97. function checkPropertyAccess(node, objectName, propertyName) {
  98. if (propertyName === null) {
  99. return;
  100. }
  101. const matchedObject = restrictedProperties.get(objectName);
  102. const matchedObjectProperty = matchedObject ? matchedObject.get(propertyName) : globallyRestrictedObjects.get(objectName);
  103. const globalMatchedProperty = globallyRestrictedProperties.get(propertyName);
  104. if (matchedObjectProperty) {
  105. const message = matchedObjectProperty.message ? ` ${matchedObjectProperty.message}` : "";
  106. context.report({
  107. node,
  108. messageId: "restrictedObjectProperty",
  109. data: {
  110. objectName,
  111. propertyName,
  112. message
  113. }
  114. });
  115. } else if (globalMatchedProperty) {
  116. const message = globalMatchedProperty.message ? ` ${globalMatchedProperty.message}` : "";
  117. context.report({
  118. node,
  119. messageId: "restrictedProperty",
  120. data: {
  121. propertyName,
  122. message
  123. }
  124. });
  125. }
  126. }
  127. /**
  128. * Checks property accesses in a destructuring assignment expression, e.g. `var foo; ({foo} = bar);`
  129. * @param {ASTNode} node An AssignmentExpression or AssignmentPattern node
  130. * @returns {undefined}
  131. */
  132. function checkDestructuringAssignment(node) {
  133. if (node.right.type === "Identifier") {
  134. const objectName = node.right.name;
  135. if (node.left.type === "ObjectPattern") {
  136. node.left.properties.forEach(property => {
  137. checkPropertyAccess(node.left, objectName, astUtils.getStaticPropertyName(property));
  138. });
  139. }
  140. }
  141. }
  142. return {
  143. MemberExpression(node) {
  144. checkPropertyAccess(node, node.object && node.object.name, astUtils.getStaticPropertyName(node));
  145. },
  146. VariableDeclarator(node) {
  147. if (node.init && node.init.type === "Identifier") {
  148. const objectName = node.init.name;
  149. if (node.id.type === "ObjectPattern") {
  150. node.id.properties.forEach(property => {
  151. checkPropertyAccess(node.id, objectName, astUtils.getStaticPropertyName(property));
  152. });
  153. }
  154. }
  155. },
  156. AssignmentExpression: checkDestructuringAssignment,
  157. AssignmentPattern: checkDestructuringAssignment
  158. };
  159. }
  160. };