eslint/no-cond-assign Correctness
作用
禁止在条件表达式中使用赋值运算符。
为什么不好?
在条件语句中,很容易将比较运算符(如 ==)误写为赋值运算符(如 =)。
在条件语句中使用赋值运算符是有合理理由的。然而,很难判断特定的赋值是否是故意的。
示例
此规则 不正确 的代码示例:
js
// 检查用户的职位头衔
if ((user.jobTitle = "manager")) {
// user.jobTitle 现在不正确了
}此规则 正确 的代码示例:
js
// 检查用户的职位头衔
if (user.jobTitle === "manager") {
// 正确比较了 `jobTitle`
}配置
此规则接受以下字符串值之一:
"except-parens"
仅允许在条件表达式中使用被括号包围的赋值。
"always"
禁止条件表达式中的所有赋值。
如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"no-cond-assign": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"no-cond-assign": "error",
},
});bash
oxlint --deny no-cond-assign版本
此规则于 v0.0.5 中添加。
