operator-assignment

尽可能要求或禁止赋值运算符简写

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

JavaScript 提供了结合变量赋值和一些简单数学运算的速记运算符。例如,x = x + 4 可以缩短为 x += 4。支持的速记形式如下:

 Shorthand | Separate
-----------|------------
 x += y    | x = x + y
 x -= y    | x = x - y
 x *= y    | x = x * y
 x /= y    | x = x / y
 x %= y    | x = x % y
 x **= y   | x = x ** y
 x <<= y   | x = x << y
 x >>= y   | x = x >> y
 x >>>= y  | x = x >>> y
 x &= y    | x = x & y
 x ^= y    | x = x ^ y
 x |= y    | x = x | y

规则详情

此规则在可能的情况下要求或禁止赋值运算符简写。

该规则适用于上表中列出的运算符。它不报告逻辑赋值运算符 &&=||=??=,因为它们的短路行为与其他赋值运算符不同。

选项

p1sIsUzWj7eKM+EUv2HNlsFedZ/4YmKX2ZN/hZtNxApRTjq8PfL8BO0nDGeLq6Wk

    FLrUhq5Ano8/LrwG0esbkNaKvTTYqaiCDX8KSE/7gEA9agMcvex1kLJjbpCqjKoyp+wFT+se7qYJMipNWEI2amXIQtMVav6A/Jeo7LWbageihmbPXx506vV9bgydP6tpICW+bKKXogg9ST0Qmn1bxD0Klj1h3B2nt8mxGBkMZ494JplVc/5tVvwfRqveVuIa0q5/zKUaaYt0orVHbuqdxA==

always

f0EVrwcDZny0gxqj5YucMIJrRAM+klylTDr73gbbrC8qKQuCaTxKiIBYW/oK07krJmx0N1SxCwzoK3/F8oScNYuS4qMtldN/YpQoot77MQ4=

/*eslint operator-assignment: ["error", "always"]*/

x = x + y;
x = y * x;
x[0] = x[0] / y;
x.y = x.y << z;

f0EVrwcDZny0gxqj5YucMIJrRAM+klylTDr73gbbrC8qKQuCaTxKiIBYW/oK07krPdZLqYSrN3KknB8fSqt1kpOICRWrTMuS5HEUA4rdJxA=

/*eslint operator-assignment: ["error", "always"]*/

x = y;
x += y;
x = y * z;
x = (x * y) * z;
x[0] /= y;
x[foo()] = x[foo()] % 2;
x = y + x; // `+` is not always commutative (e.g. x = "abc")

never

cHf/0vFMNJSQMO1JxuxkQGLWYNnrcIwAItSZv5wfIODh2r9cmbK35Awqz9FmTcLYb/eerR3vE56VY6dM/ne8DtJhlVmDzh7T7rOXZEu5ecE=

/*eslint operator-assignment: ["error", "never"]*/

x *= y;
x ^= (y + z) / foo();

cHf/0vFMNJSQMO1JxuxkQGLWYNnrcIwAItSZv5wfIODhI+dsTIuxuv33xeY2QEPi+mCtJqbo/xI28GjjwbDxEbfey+sFq92Z8lBMYA8Of5Y=

/*eslint operator-assignment: ["error", "never"]*/

x = x + y;
x.y = x.y / a.b;

何时不使用

8dqDJLeOWMyNXG8SgpPUEvi2mFDbxeYK674jhHQ3M2HgHKGwx9qG9zRdKQKiLp3dpovj/eiurZwegWJsR0HUAePxS/eOigywWtewZ3bNVGLsvxNcQnrn2xqqmce8Ztd1cIoNtj9bOO5JEEUrJ8jz62vH3KY7AWtjAqURWCm66EEB28uI0lConoW53lTfWwFl