Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-param-reassign.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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)$/;
  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] && Boolean(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. // no default
  95. }
  96. node = parent;
  97. parent = node.parent;
  98. }
  99. return false;
  100. }
  101. /**
  102. * Reports a reference if is non initializer and writable.
  103. * @param {Reference} reference - A reference to check.
  104. * @param {int} index - The index of the reference in the references.
  105. * @param {Reference[]} references - The array that the reference belongs to.
  106. * @returns {void}
  107. */
  108. function checkReference(reference, index, references) {
  109. const identifier = reference.identifier;
  110. if (identifier &&
  111. !reference.init &&
  112. /*
  113. * Destructuring assignments can have multiple default value,
  114. * so possibly there are multiple writeable references for the same identifier.
  115. */
  116. (index === 0 || references[index - 1].identifier !== identifier)
  117. ) {
  118. if (reference.isWrite()) {
  119. context.report({ node: identifier, message: "Assignment to function parameter '{{name}}'.", data: { name: identifier.name } });
  120. } else if (props && isModifyingProp(reference) && ignoredPropertyAssignmentsFor.indexOf(identifier.name) === -1) {
  121. context.report({ node: identifier, message: "Assignment to property of function parameter '{{name}}'.", data: { name: identifier.name } });
  122. }
  123. }
  124. }
  125. /**
  126. * Finds and reports references that are non initializer and writable.
  127. * @param {Variable} variable - A variable to check.
  128. * @returns {void}
  129. */
  130. function checkVariable(variable) {
  131. if (variable.defs[0].type === "Parameter") {
  132. variable.references.forEach(checkReference);
  133. }
  134. }
  135. /**
  136. * Checks parameters of a given function node.
  137. * @param {ASTNode} node - A function node to check.
  138. * @returns {void}
  139. */
  140. function checkForFunction(node) {
  141. context.getDeclaredVariables(node).forEach(checkVariable);
  142. }
  143. return {
  144. // `:exit` is needed for the `node.parent` property of identifier nodes.
  145. "FunctionDeclaration:exit": checkForFunction,
  146. "FunctionExpression:exit": checkForFunction,
  147. "ArrowFunctionExpression:exit": checkForFunction
  148. };
  149. }
  150. };