no-param-reassign.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * @fileoverview Disallow reassignment of function parameters.
  3. * @author Nat Burns
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. const stopNodePattern = /(?:Statement|Declaration|Function(?:Expression)?|Program)$/u;
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "disallow reassigning `function` parameters",
  15. category: "Best Practices",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/no-param-reassign"
  18. },
  19. schema: [
  20. {
  21. oneOf: [
  22. {
  23. type: "object",
  24. properties: {
  25. props: {
  26. enum: [false]
  27. }
  28. },
  29. additionalProperties: false
  30. },
  31. {
  32. type: "object",
  33. properties: {
  34. props: {
  35. enum: [true]
  36. },
  37. ignorePropertyModificationsFor: {
  38. type: "array",
  39. items: {
  40. type: "string"
  41. },
  42. uniqueItems: true
  43. },
  44. ignorePropertyModificationsForRegex: {
  45. type: "array",
  46. items: {
  47. type: "string"
  48. },
  49. uniqueItems: true
  50. }
  51. },
  52. additionalProperties: false
  53. }
  54. ]
  55. }
  56. ]
  57. },
  58. create(context) {
  59. const props = context.options[0] && context.options[0].props;
  60. const ignoredPropertyAssignmentsFor = context.options[0] && context.options[0].ignorePropertyModificationsFor || [];
  61. const ignoredPropertyAssignmentsForRegex = context.options[0] && context.options[0].ignorePropertyModificationsForRegex || [];
  62. /**
  63. * Checks whether or not the reference modifies properties of its variable.
  64. * @param {Reference} reference A reference to check.
  65. * @returns {boolean} Whether or not the reference modifies properties of its variable.
  66. */
  67. function isModifyingProp(reference) {
  68. let node = reference.identifier;
  69. let parent = node.parent;
  70. while (parent && (!stopNodePattern.test(parent.type) ||
  71. parent.type === "ForInStatement" || parent.type === "ForOfStatement")) {
  72. switch (parent.type) {
  73. // e.g. foo.a = 0;
  74. case "AssignmentExpression":
  75. return parent.left === node;
  76. // e.g. ++foo.a;
  77. case "UpdateExpression":
  78. return true;
  79. // e.g. delete foo.a;
  80. case "UnaryExpression":
  81. if (parent.operator === "delete") {
  82. return true;
  83. }
  84. break;
  85. // e.g. for (foo.a in b) {}
  86. case "ForInStatement":
  87. case "ForOfStatement":
  88. if (parent.left === node) {
  89. return true;
  90. }
  91. // this is a stop node for parent.right and parent.body
  92. return false;
  93. // EXCLUDES: e.g. cache.get(foo.a).b = 0;
  94. case "CallExpression":
  95. if (parent.callee !== node) {
  96. return false;
  97. }
  98. break;
  99. // EXCLUDES: e.g. cache[foo.a] = 0;
  100. case "MemberExpression":
  101. if (parent.property === node) {
  102. return false;
  103. }
  104. break;
  105. // EXCLUDES: e.g. ({ [foo]: a }) = bar;
  106. case "Property":
  107. if (parent.key === node) {
  108. return false;
  109. }
  110. break;
  111. // EXCLUDES: e.g. (foo ? a : b).c = bar;
  112. case "ConditionalExpression":
  113. if (parent.test === node) {
  114. return false;
  115. }
  116. break;
  117. // no default
  118. }
  119. node = parent;
  120. parent = node.parent;
  121. }
  122. return false;
  123. }
  124. /**
  125. * Tests that an identifier name matches any of the ignored property assignments.
  126. * First we test strings in ignoredPropertyAssignmentsFor.
  127. * Then we instantiate and test RegExp objects from ignoredPropertyAssignmentsForRegex strings.
  128. * @param {string} identifierName A string that describes the name of an identifier to
  129. * ignore property assignments for.
  130. * @returns {boolean} Whether the string matches an ignored property assignment regular expression or not.
  131. */
  132. function isIgnoredPropertyAssignment(identifierName) {
  133. return ignoredPropertyAssignmentsFor.includes(identifierName) ||
  134. ignoredPropertyAssignmentsForRegex.some(ignored => new RegExp(ignored, "u").test(identifierName));
  135. }
  136. /**
  137. * Reports a reference if is non initializer and writable.
  138. * @param {Reference} reference A reference to check.
  139. * @param {int} index The index of the reference in the references.
  140. * @param {Reference[]} references The array that the reference belongs to.
  141. * @returns {void}
  142. */
  143. function checkReference(reference, index, references) {
  144. const identifier = reference.identifier;
  145. if (identifier &&
  146. !reference.init &&
  147. /*
  148. * Destructuring assignments can have multiple default value,
  149. * so possibly there are multiple writeable references for the same identifier.
  150. */
  151. (index === 0 || references[index - 1].identifier !== identifier)
  152. ) {
  153. if (reference.isWrite()) {
  154. context.report({ node: identifier, message: "Assignment to function parameter '{{name}}'.", data: { name: identifier.name } });
  155. } else if (props && isModifyingProp(reference) && !isIgnoredPropertyAssignment(identifier.name)) {
  156. context.report({ node: identifier, message: "Assignment to property of function parameter '{{name}}'.", data: { name: identifier.name } });
  157. }
  158. }
  159. }
  160. /**
  161. * Finds and reports references that are non initializer and writable.
  162. * @param {Variable} variable A variable to check.
  163. * @returns {void}
  164. */
  165. function checkVariable(variable) {
  166. if (variable.defs[0].type === "Parameter") {
  167. variable.references.forEach(checkReference);
  168. }
  169. }
  170. /**
  171. * Checks parameters of a given function node.
  172. * @param {ASTNode} node A function node to check.
  173. * @returns {void}
  174. */
  175. function checkForFunction(node) {
  176. context.getDeclaredVariables(node).forEach(checkVariable);
  177. }
  178. return {
  179. // `:exit` is needed for the `node.parent` property of identifier nodes.
  180. "FunctionDeclaration:exit": checkForFunction,
  181. "FunctionExpression:exit": checkForFunction,
  182. "ArrowFunctionExpression:exit": checkForFunction
  183. };
  184. }
  185. };