logical-assignment-operators
要求或禁止逻辑赋值逻辑运算符简写
一些该规则报告的问题可以通过 --fix 命令行选项 自动修复
ES2021 引入了逻辑运算符 ||
、&&
和 ??
的赋值运算符简写。以前,这仅允许用于数学运算,例如 +
或 *
(参见规则 operator-assignment)。如果赋值目标和逻辑表达式的左表达式相同,则可以使用简写。例如 a = a || b
可以缩短为 a ||= b
。
规则详情
此规则要求或禁止逻辑赋值运算符简写。
选项
该规则有一个字符串和一个对象选项。字符串选项:
- 4v8/Bf/WrWOvo2VtHRYHX4sMI8vukEaenFapfeXZ5Fdu6PEexKfag+TkOURoCnm0Fv3XoeIWIY6ZS4X3fpLd+ZVzCBmNqnIZ0wnx7KI5CMQ=
P+AJLL7DXIcjSWYxZB2Zq8nhHE8UGaKgqOAPZqaKHqoiwYYkbg8QXfPFd1kUkw+v7Zq8Xz/XY7GOhPKn/WAzpfNwPtApm48KyoIsYUHtH8kADcBzO37srZwqP7QWx7UF
- OJZw80TxYwQOJ07dMmAfmqiv+++S8FT/3hGXczAQTh26trCLWVMgtmhr2NAmMACdhJOiqQaPL5EgmUlsz9RP6FWx6Vi2+zjnnhAp1D2QVn71yvaqmbHXxjQwNMJvVw0eni3xFgmtT7mkA4F788n+eSSzaJVywxBWNNxzyoaChm7RE/yn38ybu4aEVD1mCFMIdWProVshflc8QWCR4Vc/2VaZQOh/cbhiVXLQuYuOFo66JHSoMMNThmrzxjxx1rVqZHB2LhoS5OOdw42V/rMiUw==
always
EKtVsMjWQsWIsgwyRUkFI4hsuaWJOTP4v0wiiBvlW5Rd9YEiqeOnO03HGF2/LvAE7R829YyRt8VQR0O9ruFIDVgMkb7681ekPuQyNZrJh+Y=
/*eslint logical-assignment-operators: ["error", "always"]*/
a = a || b
a = a && b
a = a ?? b
a || (a = b)
a && (a = b)
a ?? (a = b)
EKtVsMjWQsWIsgwyRUkFI4hsuaWJOTP4v0wiiBvlW5Rd9YEiqeOnO03HGF2/LvAECnEcTFJ49I4h/7bfCKKeGuagYojYKEDx1CF7MEMDu7w=
/*eslint logical-assignment-operators: ["error", "always"]*/
a = b
a += b
a ||= b
a = b || c
a || (b = c)
if (a) a = b
never
CDruO8STtfiPEOKFpJL7OP3s/zODsVkmFoDRyeNPerKWJzOC/XsffPjrRBPva+sDxpJklNHBLSvxIoom0j0rjg3iA0oNz3SKYnbzHhs2c28=
/*eslint logical-assignment-operators: ["error", "never"]*/
a ||= b
a &&= b
a ??= b
CDruO8STtfiPEOKFpJL7OP3s/zODsVkmFoDRyeNPerIHIKjEgS1AfcbwibqAjOVeRQ0yd86ZeCQFHHLlcCo1q/0esnt+UhtmbaktHFTc6+M=
/*eslint logical-assignment-operators: ["error", "never"]*/
a = a || b
a = a && b
a = a ?? b
enforceForIfStatements
19doLLPv8boX+3FBj6U03Rri1cdaCNJ0MKB0IcMcelwPmQHMMs3L9SEJYtS6Og3bLeJm4okxa5yVUGuJ7uswGT9V7Vx02Du/rjnXsAn29LjV0ge9R7jHRvXruSmjNlbT
CDruO8STtfiPEOKFpJL7OPg2wdCkZ6LtZ33aj9r5kT+Uro9CZ0r1Wugzp3WyYIHpg8N5zXGXO6OMhJhXFa/ozOFgSjvaYwxHWkEgcuqYnPgbXhwUpuhpGCBOwWzXbugwq95PTHG8eJahnFtMCB4/pQ==
/*eslint logical-assignment-operators: ["error", "always", { enforceForIfStatements: true }]*/
if (a) a = b // <=> a &&= b
if (!a) a = b // <=> a ||= b
if (a == null) a = b // <=> a ??= b
if (a === null || a === undefined) a = b // <=> a ??= b
CDruO8STtfiPEOKFpJL7OPg2wdCkZ6LtZ33aj9r5kT+Uro9CZ0r1Wugzp3WyYIHpg8N5zXGXO6OMhJhXFa/ozOFgSjvaYwxHWkEgcuqYnPjnDg8modp7qKHsYSnYrov1+ZEdyKwDBSufDF3s9KYDZA==
/*eslint logical-assignment-operators: ["error", "always", { enforceForIfStatements: true }]*/
if (a) b = c
if (a === 0) a = b
何时不使用
ZbtXHzDjt/SjT6iy1VbtK7I81nEVGlxU27m8ly3R3l9MRTIIMlEdeFw2bm1AhEqipUcQBvXEuWbMGUhpMzvZQkEgGI4qoJG9y9tAuhJlukiVDuxqu1XanV46uhoEVQjHiYm7MAXgPOakJrb/Rs4bhShqhYF497XpPRawbXr6E01ha7shDyL4uPFd8kYx8MApwv2zspN+69SUebQ3kXecpA==