no-ex-assign

禁止在 catch 条款中重新分配例外情况

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

如果 try 语句中的 catch 子句意外(或故意)为异常参数分配了另一个值,则从那时起就不可能引用该错误。由于没有 arguments 对象可以提供对该数据的替代访问,因此参数的分配绝对是破坏性的。

¥If a catch clause in a try statement accidentally (or purposely) assigns another value to the exception parameter, it is impossible to refer to the error from that point on. Since there is no arguments object to offer alternative access to this data, assignment of the parameter is absolutely destructive.

规则详情

¥Rule Details

此规则不允许在 catch 子句中重新分配例外。

¥This rule disallows reassigning exceptions in catch clauses.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-ex-assign: "error"*/

try {
    // code
} catch (e) {
    e = 10;
}

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-ex-assign: "error"*/

try {
    // code
} catch (e) {
    var foo = 10;
}

版本

此规则是在 ESLint v0.0.9 中引入。

进阶读物

资源

ESLint 中文网
粤ICP备13048890号