no-param-reassign.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. },
  45. additionalProperties: false
  46. }
  47. ]
  48. }
  49. ]
  50. },
  51. create(context) {
  52. const props = context.options[0] && context.options[0].props;
  53. const ignoredPropertyAssignmentsFor = context.options[0] && context.options[0].ignorePropertyModificationsFor || [];
  54. /**
  55. * Checks whether or not the reference modifies properties of its variable.
  56. * @param {Reference} reference - A reference to check.
  57. * @returns {boolean} Whether or not the reference modifies properties of its variable.
  58. */
  59. function isModifyingProp(reference) {
  60. let node = reference.identifier;
  61. let parent = node.parent;
  62. while (parent && !stopNodePattern.test(parent.type)) {
  63. switch (parent.type) {
  64. // e.g. foo.a = 0;
  65. case "AssignmentExpression":
  66. return parent.left === node;
  67. // e.g. ++foo.a;
  68. case "UpdateExpression":
  69. return true;
  70. // e.g. delete foo.a;
  71. case "UnaryExpression":
  72. if (parent.operator === "delete") {
  73. return true;
  74. }
  75. break;
  76. // EXCLUDES: e.g. cache.get(foo.a).b = 0;
  77. case "CallExpression":
  78. if (parent.callee !== node) {
  79. return false;
  80. }
  81. break;
  82. // EXCLUDES: e.g. cache[foo.a] = 0;
  83. case "MemberExpression":
  84. if (parent.property === node) {
  85. return false;
  86. }
  87. break;
  88. // EXCLUDES: e.g. ({ [foo]: a }) = bar;
  89. case "Property":
  90. if (parent.key === node) {
  91. return false;
  92. }
  93. break;
  94. // EXCLUDES: e.g. (foo ? a : b).c = bar;
  95. case "ConditionalExpression":
  96. if (parent.test === node) {
  97. return false;
  98. }
  99. break;
  100. // no default
  101. }
  102. node = parent;
  103. parent = node.parent;
  104. }
  105. return false;
  106. }
  107. /**
  108. * Reports a reference if is non initializer and writable.
  109. * @param {Reference} reference - A reference to check.
  110. * @param {int} index - The index of the reference in the references.
  111. * @param {Reference[]} references - The array that the reference belongs to.
  112. * @returns {void}
  113. */
  114. function checkReference(reference, index, references) {
  115. const identifier = reference.identifier;
  116. if (identifier &&
  117. !reference.init &&
  118. /*
  119. * Destructuring assignments can have multiple default value,
  120. * so possibly there are multiple writeable references for the same identifier.
  121. */
  122. (index === 0 || references[index - 1].identifier !== identifier)
  123. ) {
  124. if (reference.isWrite()) {
  125. context.report({ node: identifier, message: "Assignment to function parameter '{{name}}'.", data: { name: identifier.name } });
  126. } else if (props && isModifyingProp(reference) && ignoredPropertyAssignmentsFor.indexOf(identifier.name) === -1) {
  127. context.report({ node: identifier, message: "Assignment to property of function parameter '{{name}}'.", data: { name: identifier.name } });
  128. }
  129. }
  130. }
  131. /**
  132. * Finds and reports references that are non initializer and writable.
  133. * @param {Variable} variable - A variable to check.
  134. * @returns {void}
  135. */
  136. function checkVariable(variable) {
  137. if (variable.defs[0].type === "Parameter") {
  138. variable.references.forEach(checkReference);
  139. }
  140. }
  141. /**
  142. * Checks parameters of a given function node.
  143. * @param {ASTNode} node - A function node to check.
  144. * @returns {void}
  145. */
  146. function checkForFunction(node) {
  147. context.getDeclaredVariables(node).forEach(checkVariable);
  148. }
  149. return {
  150. // `:exit` is needed for the `node.parent` property of identifier nodes.
  151. "FunctionDeclaration:exit": checkForFunction,
  152. "FunctionExpression:exit": checkForFunction,
  153. "ArrowFunctionExpression:exit": checkForFunction
  154. };
  155. }
  156. };