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-extend-native.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @fileoverview Rule to flag adding properties to native object's prototypes.
  3. * @author David Nelson
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. const globals = require("globals");
  11. //------------------------------------------------------------------------------
  12. // Helpers
  13. //------------------------------------------------------------------------------
  14. const propertyDefinitionMethods = new Set(["defineProperty", "defineProperties"]);
  15. //------------------------------------------------------------------------------
  16. // Rule Definition
  17. //------------------------------------------------------------------------------
  18. module.exports = {
  19. meta: {
  20. type: "suggestion",
  21. docs: {
  22. description: "disallow extending native types",
  23. category: "Best Practices",
  24. recommended: false,
  25. url: "https://eslint.org/docs/rules/no-extend-native"
  26. },
  27. schema: [
  28. {
  29. type: "object",
  30. properties: {
  31. exceptions: {
  32. type: "array",
  33. items: {
  34. type: "string"
  35. },
  36. uniqueItems: true
  37. }
  38. },
  39. additionalProperties: false
  40. }
  41. ],
  42. messages: {
  43. unexpected: "{{builtin}} prototype is read only, properties should not be added."
  44. }
  45. },
  46. create(context) {
  47. const config = context.options[0] || {};
  48. const exceptions = new Set(config.exceptions || []);
  49. const modifiedBuiltins = new Set(
  50. Object.keys(globals.builtin)
  51. .filter(builtin => builtin[0].toUpperCase() === builtin[0])
  52. .filter(builtin => !exceptions.has(builtin))
  53. );
  54. /**
  55. * Reports a lint error for the given node.
  56. * @param {ASTNode} node The node to report.
  57. * @param {string} builtin The name of the native builtin being extended.
  58. * @returns {void}
  59. */
  60. function reportNode(node, builtin) {
  61. context.report({
  62. node,
  63. messageId: "unexpected",
  64. data: {
  65. builtin
  66. }
  67. });
  68. }
  69. /**
  70. * Check to see if the `prototype` property of the given object
  71. * identifier node is being accessed.
  72. * @param {ASTNode} identifierNode The Identifier representing the object
  73. * to check.
  74. * @returns {boolean} True if the identifier is the object of a
  75. * MemberExpression and its `prototype` property is being accessed,
  76. * false otherwise.
  77. */
  78. function isPrototypePropertyAccessed(identifierNode) {
  79. return Boolean(
  80. identifierNode &&
  81. identifierNode.parent &&
  82. identifierNode.parent.type === "MemberExpression" &&
  83. identifierNode.parent.object === identifierNode &&
  84. astUtils.getStaticPropertyName(identifierNode.parent) === "prototype"
  85. );
  86. }
  87. /**
  88. * Checks that an identifier is an object of a prototype whose member
  89. * is being assigned in an AssignmentExpression.
  90. * Example: Object.prototype.foo = "bar"
  91. * @param {ASTNode} identifierNode The identifier to check.
  92. * @returns {boolean} True if the identifier's prototype is modified.
  93. */
  94. function isInPrototypePropertyAssignment(identifierNode) {
  95. return Boolean(
  96. isPrototypePropertyAccessed(identifierNode) &&
  97. identifierNode.parent.parent.type === "MemberExpression" &&
  98. identifierNode.parent.parent.parent.type === "AssignmentExpression" &&
  99. identifierNode.parent.parent.parent.left === identifierNode.parent.parent
  100. );
  101. }
  102. /**
  103. * Checks that an identifier is an object of a prototype whose member
  104. * is being extended via the Object.defineProperty() or
  105. * Object.defineProperties() methods.
  106. * Example: Object.defineProperty(Array.prototype, "foo", ...)
  107. * Example: Object.defineProperties(Array.prototype, ...)
  108. * @param {ASTNode} identifierNode The identifier to check.
  109. * @returns {boolean} True if the identifier's prototype is modified.
  110. */
  111. function isInDefinePropertyCall(identifierNode) {
  112. return Boolean(
  113. isPrototypePropertyAccessed(identifierNode) &&
  114. identifierNode.parent.parent.type === "CallExpression" &&
  115. identifierNode.parent.parent.arguments[0] === identifierNode.parent &&
  116. identifierNode.parent.parent.callee.type === "MemberExpression" &&
  117. identifierNode.parent.parent.callee.object.type === "Identifier" &&
  118. identifierNode.parent.parent.callee.object.name === "Object" &&
  119. identifierNode.parent.parent.callee.property.type === "Identifier" &&
  120. propertyDefinitionMethods.has(identifierNode.parent.parent.callee.property.name)
  121. );
  122. }
  123. /**
  124. * Check to see if object prototype access is part of a prototype
  125. * extension. There are three ways a prototype can be extended:
  126. * 1. Assignment to prototype property (Object.prototype.foo = 1)
  127. * 2. Object.defineProperty()/Object.defineProperties() on a prototype
  128. * If prototype extension is detected, report the AssignmentExpression
  129. * or CallExpression node.
  130. * @param {ASTNode} identifierNode The Identifier representing the object
  131. * which prototype is being accessed and possibly extended.
  132. * @returns {void}
  133. */
  134. function checkAndReportPrototypeExtension(identifierNode) {
  135. if (isInPrototypePropertyAssignment(identifierNode)) {
  136. // Identifier --> MemberExpression --> MemberExpression --> AssignmentExpression
  137. reportNode(identifierNode.parent.parent.parent, identifierNode.name);
  138. } else if (isInDefinePropertyCall(identifierNode)) {
  139. // Identifier --> MemberExpression --> CallExpression
  140. reportNode(identifierNode.parent.parent, identifierNode.name);
  141. }
  142. }
  143. return {
  144. "Program:exit"() {
  145. const globalScope = context.getScope();
  146. modifiedBuiltins.forEach(builtin => {
  147. const builtinVar = globalScope.set.get(builtin);
  148. if (builtinVar && builtinVar.references) {
  149. builtinVar.references
  150. .map(ref => ref.identifier)
  151. .forEach(checkAndReportPrototypeExtension);
  152. }
  153. });
  154. }
  155. };
  156. }
  157. };