noParameterAssign
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/style/noParameterAssign¥Diagnostic Category:
lint/style/noParameterAssign -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
-
与
no-param-reassign相同¥Same as
no-param-reassign
-
¥How to configure
{ "linter": { "rules": { "style": { "noParameterAssign": "error" } } }}¥Description
禁止重新分配 function 参数。
¥Disallow reassigning function parameters.
对 function 参数的赋值可能会产生误导和混淆,因为修改参数也会改变 arguments 对象。这通常是无意的,并且表明程序员犯了错误。
¥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.
¥Examples
¥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.
¥Valid
function f(param) { let local = param;}¥Options
propertyAssignment
Section titled “propertyAssignment”可以使用 propertyAssignment 选项配置 noParameterAssign 规则,该选项决定是否允许或禁止对函数参数进行属性赋值。默认情况下,propertyAssignment 设置为 allow。
¥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:
-
{ "linter": { "rules": { "style": { "noParameterAssign": { "options": { "propertyAssignment": "allow" } } } } }}function update(obj) { obj.key = "value"; // No diagnostic}-
拒绝:禁止对函数参数进行属性赋值,从而强制执行更严格的不可变性。
¥deny: Disallows property assignments on function parameters, enforcing stricter immutability.
-
示例:
¥Example:
-
{ "linter": { "rules": { "style": { "noParameterAssign": { "options": { "propertyAssignment": "deny" } } } } }}function update(obj) { obj.key = "value"; // Diagnostic: Assignment to a property of function parameter is not allowed.}code-block.js:2:5 lint/style/noParameterAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Assigning to a property of a function parameter is confusing.
1 │ function update(obj) {
> 2 │ obj.key = “value”; // Diagnostic: Assignment to a property of function parameter is not allowed.
│ ^^^
3 │ }
4 │
ℹ Function callers usually don’t expect the parameters they pass in to be modified. To avoid mutation, create a new instance and return it to the caller.
¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号