no-cond-assign
禁止条件表达式中的赋值运算符
配置文件 中的
"extends": "eslint:recommended"
属性启用了该规则
在条件语句中,很容易将比较运算符(例如 ==
)误键入为赋值运算符(例如 =
)。例如:
// Check the user's job title
if (user.jobTitle = "manager") {
// user.jobTitle is now incorrect
}
在条件语句中使用赋值运算符是有充分理由的。然而,很难判断一个特定的任务是否是故意的。
规则详情
此规则不允许在 if
、for
、while
和 do...while
语句的测试条件中使用模棱两可的赋值运算符。
选项
WhtdTe+SyJ0WUNUgKbqftHKsXY5Xm15X5kJLFmjbJU11+P/8ka3u9R1NdHmiWzzC
- oqg4MCPTWwlAEj+Wc/4+MJ/3C3NogSTgQTqGUx6iu0+jPBgbu4gJPd7Yb2LtTw/OtOb6grdSK8cffKEN+ueJy9+Wg+rBKJKRjQgDRkvrxS5RlsYVGapc8Yk+vbKP43GDfty/uFrxaJRx//rDlH+HP7BwnF521gwBBdw1KQVs8eNH/yBbMDzILFp7hTWeaEwBgThp4CM2SX9UVA+w7kp4FZPVEH6jRkKBlgJwEOrFzfQwYKs6aKCAC9QnSNT2jJ3kD1zpZzKUYVmlxmz8D3e/qrSXmjVzffGqpQ5Qp6otP7gqRWcm+saPtHDX5Iwhx8aIaXF12kgNONytZQxkdx1/4a572ueRk7WDHdB4yD05U/ZzRQOq9mKXRXBk2RB1DeKLvSgwXF+i0CY2PQCaCkOSeg==
except-parens
IkNtK1AnBCO6NSjlzxSStf5iVpMs1HDgZdtYG1DAvezN42FPqvNavN7MY6FVJKyt/QGqpMR//SnUovnL4r0/sM5VOw76dobG5gmXF3LhlaipYk4XOkASu4Zz61Dou/er
/*eslint no-cond-assign: "error"*/
// Unintentional assignment
var x;
if (x = 0) {
var b = 1;
}
// Practical example that is similar to an error
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while (someNode = someNode.parentNode);
}
IkNtK1AnBCO6NSjlzxSStf5iVpMs1HDgZdtYG1DAvezN42FPqvNavN7MY6FVJKytzEOE1zGwbAxrglUrdqMES2D6yuhLZF+e0hP6dckWOe2jDs1LqpQY2IRpOJEiR3hu
/*eslint no-cond-assign: "error"*/
// Assignment replaced by comparison
var x;
if (x === 0) {
var b = 1;
}
// Practical example that wraps the assignment in parentheses
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode));
}
// Practical example that wraps the assignment and tests for 'null'
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode) !== null);
}
always
dg8a89Fcmdiuqkj/gGSI6n35puJvbFhv9IINqugqNY5a8t7H2Amg3Rp9cav/D8AHEqJke80GIPIUkIFMte4VQqKsD+iB9eSo+NsyAoG5J40=
/*eslint no-cond-assign: ["error", "always"]*/
// Unintentional assignment
var x;
if (x = 0) {
var b = 1;
}
// Practical example that is similar to an error
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while (someNode = someNode.parentNode);
}
// Practical example that wraps the assignment in parentheses
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode));
}
// Practical example that wraps the assignment and tests for 'null'
function setHeight(someNode) {
"use strict";
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode) !== null);
}
dg8a89Fcmdiuqkj/gGSI6n35puJvbFhv9IINqugqNY4aJ3YzCb5PIUZ8Wx07NQxGmH/h2EjHtDeTPA05gjiHeEH84uHpDh58slEKpK28o0M=
/*eslint no-cond-assign: ["error", "always"]*/
// Assignment replaced by comparison
var x;
if (x === 0) {
var b = 1;
}