Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-restricted-properties.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @fileoverview Rule to disallow certain object properties
  3. * @author Will Klein & Eli White
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "disallow certain properties on certain objects",
  15. category: "Best Practices",
  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. },
  60. create(context) {
  61. const restrictedCalls = context.options;
  62. if (restrictedCalls.length === 0) {
  63. return {};
  64. }
  65. const restrictedProperties = new Map();
  66. const globallyRestrictedObjects = new Map();
  67. const globallyRestrictedProperties = new Map();
  68. restrictedCalls.forEach(option => {
  69. const objectName = option.object;
  70. const propertyName = option.property;
  71. if (typeof objectName === "undefined") {
  72. globallyRestrictedProperties.set(propertyName, { message: option.message });
  73. } else if (typeof propertyName === "undefined") {
  74. globallyRestrictedObjects.set(objectName, { message: option.message });
  75. } else {
  76. if (!restrictedProperties.has(objectName)) {
  77. restrictedProperties.set(objectName, new Map());
  78. }
  79. restrictedProperties.get(objectName).set(propertyName, {
  80. message: option.message
  81. });
  82. }
  83. });
  84. /**
  85. * Checks to see whether a property access is restricted, and reports it if so.
  86. * @param {ASTNode} node The node to report
  87. * @param {string} objectName The name of the object
  88. * @param {string} propertyName The name of the property
  89. * @returns {undefined}
  90. */
  91. function checkPropertyAccess(node, objectName, propertyName) {
  92. if (propertyName === null) {
  93. return;
  94. }
  95. const matchedObject = restrictedProperties.get(objectName);
  96. const matchedObjectProperty = matchedObject ? matchedObject.get(propertyName) : globallyRestrictedObjects.get(objectName);
  97. const globalMatchedProperty = globallyRestrictedProperties.get(propertyName);
  98. if (matchedObjectProperty) {
  99. const message = matchedObjectProperty.message ? ` ${matchedObjectProperty.message}` : "";
  100. context.report({
  101. node,
  102. // eslint-disable-next-line eslint-plugin/report-message-format
  103. message: "'{{objectName}}.{{propertyName}}' is restricted from being used.{{message}}",
  104. data: {
  105. objectName,
  106. propertyName,
  107. message
  108. }
  109. });
  110. } else if (globalMatchedProperty) {
  111. const message = globalMatchedProperty.message ? ` ${globalMatchedProperty.message}` : "";
  112. context.report({
  113. node,
  114. // eslint-disable-next-line eslint-plugin/report-message-format
  115. message: "'{{propertyName}}' is restricted from being used.{{message}}",
  116. data: {
  117. propertyName,
  118. message
  119. }
  120. });
  121. }
  122. }
  123. /**
  124. * Checks property accesses in a destructuring assignment expression, e.g. `var foo; ({foo} = bar);`
  125. * @param {ASTNode} node An AssignmentExpression or AssignmentPattern node
  126. * @returns {undefined}
  127. */
  128. function checkDestructuringAssignment(node) {
  129. if (node.right.type === "Identifier") {
  130. const objectName = node.right.name;
  131. if (node.left.type === "ObjectPattern") {
  132. node.left.properties.forEach(property => {
  133. checkPropertyAccess(node.left, objectName, astUtils.getStaticPropertyName(property));
  134. });
  135. }
  136. }
  137. }
  138. return {
  139. MemberExpression(node) {
  140. checkPropertyAccess(node, node.object && node.object.name, astUtils.getStaticPropertyName(node));
  141. },
  142. VariableDeclarator(node) {
  143. if (node.init && node.init.type === "Identifier") {
  144. const objectName = node.init.name;
  145. if (node.id.type === "ObjectPattern") {
  146. node.id.properties.forEach(property => {
  147. checkPropertyAccess(node.id, objectName, astUtils.getStaticPropertyName(property));
  148. });
  149. }
  150. }
  151. },
  152. AssignmentExpression: checkDestructuringAssignment,
  153. AssignmentPattern: checkDestructuringAssignment
  154. };
  155. }
  156. };