Skip to content

noParameterAssign

诊断类别:lint/style/noParameterAssign

¥Diagnostic Category: lint/style/noParameterAssign

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止重新分配 function 参数。

¥Disallow reassigning function parameters.

分配给 function 参数可能会产生误导和混淆,因为修改参数也会改变 arguments 对象。这通常是无意的,并且表明程序员犯了错误。

¥Assignment to a 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.

与 ESLint 规则相比,此规则无法配置为报告对参数属性的分配。

¥In contrast to the ESLint rule, this rule cannot be configured to report assignments to a property of a parameter.

¥Examples

¥Invalid

function f(param) {
param = 13;
}
code-block.js:2:5 lint/style/noParameterAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Reassigning 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 │ }

Use a local variable instead.

function f(param) {
param++;
}
code-block.js:2:5 lint/style/noParameterAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Reassigning 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 │ }

Use a local variable instead.

function f(param) {
for (param of arr) {}
}
code-block.js:2:10 lint/style/noParameterAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Reassigning 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 │ }

Use a local variable instead.

class C {
constructor(readonly prop: number) {
prop++
}
}
code-block.ts:3:9 lint/style/noParameterAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Reassigning 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 │ }

Use a local variable instead.

¥Valid

function f(param) {
let local = param;
}

¥Related links