no-param-reassign
Disallow assigning to function parameters
After price = price * 1.2, the name price no longer holds what the caller passed, and anyone reading further down the function has to notice that. In a function that isn't in strict mode, the assignment also changes arguments[0].
Use a new variable for the new value.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-param-reassign" = "error" # or "warn"Every option, set to its default. The ones that are commented out are unset by default.
bunfig.toml
[lint.rules]
"no-param-reassign" = ["error", {
props = false,
# ignorePropertyModificationsFor = [], # list of strings
# ignorePropertyModificationsForRegex = [], # list of strings
}]Examples#
example.js2 problems
function total(price, count) {
priceAssignment to function parameter price. = price * 1.2;
countAssignment to function parameter count.++;
return price * count;
}example.jsNo problems
function total(price, count) {
const withTax = price * 1.2;
return withTax * (count + 1);
}Options#
| Option | Default | What it does |
|---|---|---|
props | false | Also report a change to a parameter's property: an assignment, ++, --, delete, or use as the variable of a for...in or for...of loop. |
ignorePropertyModificationsFor | [] | With props: names of parameters whose properties can be changed. |
ignorePropertyModificationsForRegex | [] | With props: the same, as regular expressions. |
By default, changing a property is allowed:
example.jsNo problems
function activate(user) {
user.active = true;
}With { props: true }, it's reported, because the caller's object changes:
example.jswith
{ "props": true }2 problemsfunction activate(user) {
userAssignment to property of function parameter user..active = true;
delete userAssignment to property of function parameter user..pending;
}The two lists make exceptions for parameters that exist to be changed. With { props: true, ignorePropertyModificationsFor: ["res"], ignorePropertyModificationsForRegex: ["^draft"] }:
example.jswith
{ "props": true, "ignorePropertyModificationsFor": ["res"], "ignorePropertyModificationsForRegex": ["^draft"] }No problemsfunction notFound(req, res) {
res.statusCode = 404;
}
function rename(draftUser, name) {
draftUser.name = name;
}