项目原始demo,不改动
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

no-extend-native.js 6.9 KiB

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