noParameterAssign
Summary
Section titled “Summary”- Rule available since:
v1.0.0
- Diagnostic Category:
lint/style/noParameterAssign
- This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
no-param-reassign
- Same as
Description
Section titled “Description”Disallow reassigning function
parameters.
Assignment to function
parameters can be misleading and confusing,
as modifying parameters will also mutate the arguments
object.
It is often unintended and indicative of a programmer error.
In contrast to the ESLint rule, this rule cannot be configured to report assignments to a property of a parameter.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”function f(param) { param = 13;}
code-block.js:2:5 lint/style/noParameterAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Assigning a function parameter is confusing.
1 │ function f(param) {
> 2 │ param = 13;
│ ^^^^^
3 │ }
4 │
ℹ The parameter is declared here:
> 1 │ function f(param) {
│ ^^^^^
2 │ param = 13;
3 │ }
ℹ Developers usually expect function parameters to be readonly. To align with this expectation, use a local variable instead.
function f(param) { param++;}
code-block.js:2:5 lint/style/noParameterAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Assigning a function parameter is confusing.
1 │ function f(param) {
> 2 │ param++;
│ ^^^^^
3 │ }
4 │
ℹ The parameter is declared here:
> 1 │ function f(param) {
│ ^^^^^
2 │ param++;
3 │ }
ℹ Developers usually expect function parameters to be readonly. To align with this expectation, use a local variable instead.
function f(param) { for (param of arr) {}}
code-block.js:2:10 lint/style/noParameterAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Assigning a function parameter is confusing.
1 │ function f(param) {
> 2 │ for (param of arr) {}
│ ^^^^^
3 │ }
4 │
ℹ The parameter is declared here:
> 1 │ function f(param) {
│ ^^^^^
2 │ for (param of arr) {}
3 │ }
ℹ Developers usually expect function parameters to be readonly. To align with this expectation, use a local variable instead.
class C { constructor(readonly prop: number) { prop++; }}
code-block.ts:3:9 lint/style/noParameterAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Assigning a function parameter is confusing.
1 │ class C {
2 │ constructor(readonly prop: number) {
> 3 │ prop++;
│ ^^^^
4 │ }
5 │ }
ℹ The parameter is declared here:
1 │ class C {
> 2 │ constructor(readonly prop: number) {
│ ^^^^
3 │ prop++;
4 │ }
ℹ Developers usually expect function parameters to be readonly. To align with this expectation, use a local variable instead.
function f(param) { let local = param;}
Options
Section titled “Options”propertyAssignment
Section titled “propertyAssignment”The noParameterAssign
rule can be configured using the propertyAssignment
option, which determines whether property assignments on function parameters are allowed or denied. By default, propertyAssignment
is set to allow
.
{ "options": { "propertyAssignment": "allow" }}
- allow: Allows property assignments on function parameters. This is the default behavior.
- Example:
{ "options": { "propertyAssignment": "allow" }}
function update(obj) { obj.key = "value"; // No diagnostic}
- deny: Disallows property assignments on function parameters, enforcing stricter immutability.
- Example:
{ "options": { "propertyAssignment": "deny" }}
function update(obj) { obj.key = "value"; // Diagnostic: Assignment to a property of function parameter is not allowed.}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "style": { "noParameterAssign": "error" } } }}