no-extend-native.js 5.3 KB

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