123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- "use strict";
- const stopNodePattern = /(?:Statement|Declaration|Function(?:Expression)?|Program)$/u;
- module.exports = {
- meta: {
- type: "suggestion",
- docs: {
- description: "disallow reassigning `function` parameters",
- category: "Best Practices",
- recommended: false,
- url: "https://eslint.org/docs/rules/no-param-reassign"
- },
- schema: [
- {
- oneOf: [
- {
- type: "object",
- properties: {
- props: {
- enum: [false]
- }
- },
- additionalProperties: false
- },
- {
- type: "object",
- properties: {
- props: {
- enum: [true]
- },
- ignorePropertyModificationsFor: {
- type: "array",
- items: {
- type: "string"
- },
- uniqueItems: true
- },
- ignorePropertyModificationsForRegex: {
- type: "array",
- items: {
- type: "string"
- },
- uniqueItems: true
- }
- },
- additionalProperties: false
- }
- ]
- }
- ],
- messages: {
- assignmentToFunctionParam: "Assignment to function parameter '{{name}}'.",
- assignmentToFunctionParamProp: "Assignment to property of function parameter '{{name}}'."
- }
- },
- create(context) {
- const props = context.options[0] && context.options[0].props;
- const ignoredPropertyAssignmentsFor = context.options[0] && context.options[0].ignorePropertyModificationsFor || [];
- const ignoredPropertyAssignmentsForRegex = context.options[0] && context.options[0].ignorePropertyModificationsForRegex || [];
-
- function isModifyingProp(reference) {
- let node = reference.identifier;
- let parent = node.parent;
- while (parent && (!stopNodePattern.test(parent.type) ||
- parent.type === "ForInStatement" || parent.type === "ForOfStatement")) {
- switch (parent.type) {
-
- case "AssignmentExpression":
- return parent.left === node;
-
- case "UpdateExpression":
- return true;
-
- case "UnaryExpression":
- if (parent.operator === "delete") {
- return true;
- }
- break;
-
- case "ForInStatement":
- case "ForOfStatement":
- if (parent.left === node) {
- return true;
- }
-
- return false;
-
- case "CallExpression":
- if (parent.callee !== node) {
- return false;
- }
- break;
-
- case "MemberExpression":
- if (parent.property === node) {
- return false;
- }
- break;
-
- case "Property":
- if (parent.key === node) {
- return false;
- }
- break;
-
- case "ConditionalExpression":
- if (parent.test === node) {
- return false;
- }
- break;
-
- }
- node = parent;
- parent = node.parent;
- }
- return false;
- }
-
- function isIgnoredPropertyAssignment(identifierName) {
- return ignoredPropertyAssignmentsFor.includes(identifierName) ||
- ignoredPropertyAssignmentsForRegex.some(ignored => new RegExp(ignored, "u").test(identifierName));
- }
-
- function checkReference(reference, index, references) {
- const identifier = reference.identifier;
- if (identifier &&
- !reference.init &&
-
- (index === 0 || references[index - 1].identifier !== identifier)
- ) {
- if (reference.isWrite()) {
- context.report({
- node: identifier,
- messageId: "assignmentToFunctionParam",
- data: { name: identifier.name }
- });
- } else if (props && isModifyingProp(reference) && !isIgnoredPropertyAssignment(identifier.name)) {
- context.report({
- node: identifier,
- messageId: "assignmentToFunctionParamProp",
- data: { name: identifier.name }
- });
- }
- }
- }
-
- function checkVariable(variable) {
- if (variable.defs[0].type === "Parameter") {
- variable.references.forEach(checkReference);
- }
- }
-
- function checkForFunction(node) {
- context.getDeclaredVariables(node).forEach(checkVariable);
- }
- return {
-
- "FunctionDeclaration:exit": checkForFunction,
- "FunctionExpression:exit": checkForFunction,
- "ArrowFunctionExpression:exit": checkForFunction
- };
- }
- };
|