no-extend-native.js 6.4 KB

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