No Parameter Reassign

ID

javascript.no_param_reassign

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

JavaScript

Tags

code-smell, mutable-state

Description

Reports an assignment, compound assignment, or ++/-- update whose target binds back to a function’s formal parameter:

function badAssign(value) {
  value = value || defaultValue();        // overwrites the caller's argument
  return value;
}

function badIncrement(count) {
  count++;                                 // mutates the parameter binding
  return count;
}

Rationale

Reassigning a parameter erases the caller’s original argument from the function — it cannot be inspected for logging, asserted on later, or distinguished from the modified version. It also confuses readers and tools that expect the parameter list to document the inputs the function actually works with.

Remediation

Introduce a local variable that holds the working value. The parameter then keeps pointing at the caller’s argument throughout the body:

function goodAssign(value) {
  const v = value || defaultValue();
  return v;
}

function goodIncrement(count) {
  let n = count;
  n++;
  return n;
}

If the parameter is an object and you need to mutate one of its fields, that is a property assignment (obj.x = …) — that case is not reported by this rule because the parameter binding itself is not changed.

Notes

The rule fires on:

  • plain assignment: param = …

  • compound assignment: param += …, param ??= …, etc.

  • pre/post ++ and -- on the parameter

It does not fire on:

  • param.x = … or param[i] = … (property mutation)

  • The default-value initialiser inside the parameter list (function f(x = 1))