Rules Reference
Rules in ESLint are grouped by type to help you understand their purpose. Each rule has emojis denoting:
Using the recommended config from @eslint/js in a configuration file enables this rule
Some problems reported by this rule are automatically fixable by the --fix command line option
Some problems reported by this rule are manually fixable by editor suggestions
Possible Problems
Enforce return statements in callbacks of array methods
đź’ˇ Suggestions
Require super() calls in constructors
Enforce “for” loop update clause moving the counter in the right direction
Enforce return statements in getters
Disallow using an async function as a Promise executor
Disallow await inside of loops
Disallow reassigning class members
Disallow comparing against -0
Disallow assignment operators in conditional expressions
Disallow reassigning const variables
Disallow expressions where the operation doesn’t affect the value
Disallow constant expressions in conditions
Disallow returning value from constructor
Disallow control characters in regular expressions
Disallow the use of debugger
Disallow duplicate arguments in function definitions
Disallow duplicate class members
Disallow duplicate conditions in if-else-if chains
Disallow duplicate keys in object literals
Disallow duplicate case labels
Disallow duplicate module imports
Disallow empty character classes in regular expressions
Disallow empty destructuring patterns
Disallow reassigning exceptions in catch clauses
Disallow fallthrough of case statements
Disallow reassigning function declarations
Disallow assigning to imported bindings
Disallow variable or function declarations in nested blocks
Disallow invalid regular expression strings in RegExp constructors
Disallow irregular whitespace
Disallow literal numbers that lose precision
Disallow characters which are made with multiple code points in character class syntax
Disallow new operators with global non-constructor functions
Disallow calling global object properties as functions
Disallow returning values from Promise executor functions
Disallow calling some Object.prototype methods directly on objects
Disallow assignments where both sides are exactly the same
Disallow comparisons where both sides are exactly the same
Disallow returning values from setters
Disallow sparse arrays
Disallow template literal placeholder syntax in regular strings
Disallow this / super before calling super() in constructors
Disallow the use of undeclared variables unless mentioned in /*global */ comments
Disallow confusing multiline expressions
Disallow unmodified loop conditions
Disallow unreachable code after return , throw , continue , and break statements
Disallow loops with a body that allows only one iteration
Disallow control flow statements in finally blocks
Disallow negating the left operand of relational operators
Disallow use of optional chaining in contexts where the undefined value is not allowed
Disallow unused private class members
Disallow unused variables
Disallow the use of variables before they are defined
Disallow variable assignments when the value is not used
Disallow useless backreferences in regular expressions
Disallow assignments that can lead to race conditions due to usage of await or yield
Require calls to isNaN() when checking for NaN
Enforce comparing typeof expressions against valid strings
Suggestions
Enforce getter and setter pairs in objects and classes
Require braces around arrow function bodies
Enforce the use of variables within the scope they are defined
Enforce camelcase naming convention
Enforce or disallow capitalization of the first letter of a comment
Enforce that class methods utilize this
Enforce a maximum cyclomatic complexity allowed in a program
Require return statements to either always or never specify values
Enforce consistent naming when capturing the current execution context
Enforce consistent brace style for all control statements
Require default cases in switch statements
Enforce default clauses in switch statements to be last
Enforce default parameters to be last
Enforce dot notation whenever possible
Require the use of === and !==
Require function names to match the name of the variable or property to which they are assigned
Require or disallow named function expressions
Enforce the consistent use of either function declarations or expressions assigned to variables
Require grouped accessor pairs in object literals and classes
Require for-in loops to include an if statement
Disallow specified identifiers
Enforce minimum and maximum identifier lengths
Require identifiers to match a specified regular expression
Require or disallow initialization in variable declarations
Require or disallow logical assignment operator shorthand
Enforce a maximum number of classes per file
Enforce a maximum depth that blocks can be nested
Enforce a maximum number of lines per file
Enforce a maximum number of lines of code in a function
Enforce a maximum depth that callbacks can be nested
Enforce a maximum number of parameters in function definitions
Enforce a maximum number of statements allowed in function blocks
Require constructor names to begin with a capital letter
Disallow the use of alert , confirm , and prompt
Disallow Array constructors
Disallow bitwise operators
Disallow the use of arguments.caller or arguments.callee
Disallow lexical declarations in case clauses
Disallow the use of console
Disallow continue statements
Disallow deleting variables
Disallow equal signs explicitly at the beginning of regular expressions
Disallow else blocks after return statements in if statements
Disallow empty block statements
Disallow empty functions
Disallow empty static blocks
Disallow null comparisons without type-checking operators
Disallow the use of eval()
Disallow extending native types
Disallow unnecessary calls to .bind()
Disallow unnecessary boolean casts
Disallow unnecessary labels
Disallow assignments to native objects or read-only global variables
Disallow shorthand type conversions
Disallow declarations in the global scope
Disallow the use of eval() -like methods
Disallow inline comments after code
Disallow use of this in contexts where the value of this is undefined
Disallow the use of the __iterator__ property
Disallow labels that share a name with a variable
Disallow labeled statements
Disallow unnecessary nested blocks
Disallow if statements as the only statement in else blocks
Disallow function declarations that contain unsafe references inside loop statements
Disallow magic numbers
Disallow use of chained assignment expressions
Disallow multiline strings
Disallow negated conditions
Disallow nested ternary expressions
Disallow new operators outside of assignments or comparisons
Disallow new operators with the Function object
Disallow new operators with the String , Number , and Boolean objects
Disallow \8 and \9 escape sequences in string literals
Disallow calls to the Object constructor without an argument
Disallow octal literals
Disallow octal escape sequences in string literals
Disallow reassigning function parameters
Disallow the unary operators ++ and --
Disallow the use of the __proto__ property
Disallow variable redeclaration
Disallow multiple spaces in regular expressions
Disallow specified names in exports
Disallow specified global variables
Disallow specified modules when loaded by import
Disallow certain properties on certain objects
Disallow specified syntax
Disallow assignment operators in return statements
Disallow javascript: urls
Disallow comma operators
Disallow variable declarations from shadowing variables declared in the outer scope
Disallow identifiers from shadowing restricted names
Disallow ternary operators
Disallow throwing literals as exceptions
Disallow initializing variables to undefined
Disallow the use of undefined as an identifier
Disallow dangling underscores in identifiers
Disallow ternary operators when simpler alternatives exist
Disallow unused expressions
Disallow unused labels
Disallow unnecessary calls to .call() and .apply()
Disallow unnecessary catch clauses
Disallow unnecessary computed property keys in objects and classes
Disallow unnecessary concatenation of literals or template literals
Disallow unnecessary constructors
Disallow unnecessary escape characters
Disallow renaming import, export, and destructured assignments to the same name
Disallow redundant return statements
Require let or const instead of var
Disallow void operators
Disallow specified warning terms in comments
Disallow with statements
Require or disallow method and property shorthand syntax for object literals
Enforce variables to be declared either together or separately in functions
Require or disallow assignment operator shorthand where possible
Require using arrow functions for callbacks
Require const declarations for variables that are never reassigned after declared
Require destructuring from arrays and/or objects
Disallow the use of Math.pow in favor of the ** operator
Enforce using named capture group in regular expression
Disallow parseInt() and Number.parseInt() in favor of binary, octal, and hexadecimal literals
Disallow use of Object.prototype.hasOwnProperty.call() and prefer use of Object.hasOwn()
Disallow using Object.assign with an object literal as the first argument and prefer the use of object spread instead
Require using Error objects as Promise rejection reasons
Disallow use of the RegExp constructor in favor of regular expression literals
Require rest parameters instead of arguments
Require spread operators instead of .apply()
Require template literals instead of string concatenation
Enforce the consistent use of the radix argument when using parseInt()
Disallow async functions which have no await expression
Enforce the use of u or v flag on RegExp
Require generator functions to contain yield
Enforce sorted import declarations within modules
Require object keys to be sorted
Require variables within the same declaration block to be sorted
Require or disallow strict mode directives
Require symbol descriptions
Require var declarations be placed at the top of their containing scope
Require or disallow “Yoda” conditions
Layout & Formatting
Require or disallow Unicode byte order mark (BOM)
array-bracket-newline deprecated
array-bracket-spacing deprecated
array-element-newline deprecated
arrow-parens deprecated
arrow-spacing deprecated
block-spacing deprecated
brace-style deprecated
callback-return deprecated
comma-dangle deprecated
comma-spacing deprecated
comma-style deprecated
computed-property-spacing deprecated
dot-location deprecated
eol-last deprecated
func-call-spacing deprecated
function-call-argument-newline deprecated
function-paren-newline deprecated
generator-star-spacing deprecated
global-require deprecated
handle-callback-err deprecated
id-blacklist deprecated
Replaced by id-denylist
implicit-arrow-linebreak deprecated
indent deprecated
indent-legacy deprecated
Replaced by indent
jsx-quotes deprecated
key-spacing deprecated
keyword-spacing deprecated
line-comment-position deprecated
linebreak-style deprecated
lines-around-comment deprecated
lines-around-directive deprecated
Replaced by padding-line-between-statements
lines-between-class-members deprecated
max-len deprecated
max-statements-per-line deprecated
multiline-comment-style deprecated
multiline-ternary deprecated
new-parens deprecated
newline-after-var deprecated
newline-before-return deprecated
newline-per-chained-call deprecated
no-buffer-constructor deprecated
no-catch-shadow deprecated
Replaced by no-shadow
no-confusing-arrow deprecated
no-extra-parens deprecated
no-extra-semi deprecated
no-floating-decimal deprecated
no-mixed-operators deprecated
no-mixed-requires deprecated
no-mixed-spaces-and-tabs deprecated
no-multi-spaces deprecated
no-multiple-empty-lines deprecated
no-native-reassign deprecated
Replaced by no-global-assign
no-negated-in-lhs deprecated
Replaced by no-unsafe-negation
no-new-object deprecated
Replaced by no-object-constructor
no-new-require deprecated
no-new-symbol deprecated
Replaced by no-new-native-nonconstructor
no-path-concat deprecated
no-process-env deprecated
no-process-exit deprecated
no-restricted-modules deprecated
no-return-await deprecated
no-spaced-func deprecated
Replaced by func-call-spacing
no-sync deprecated
no-tabs deprecated
no-trailing-spaces deprecated
no-whitespace-before-property deprecated
nonblock-statement-body-position deprecated
object-curly-newline deprecated
object-curly-spacing deprecated
object-property-newline deprecated
one-var-declaration-per-line deprecated
operator-linebreak deprecated
padded-blocks deprecated
padding-line-between-statements deprecated
prefer-reflect deprecated
quote-props deprecated
quotes deprecated
rest-spread-spacing deprecated
semi deprecated
semi-spacing deprecated
semi-style deprecated
space-before-blocks deprecated
space-before-function-paren deprecated
space-in-parens deprecated
space-infix-ops deprecated
space-unary-ops deprecated
spaced-comment deprecated
switch-colon-spacing deprecated
template-curly-spacing deprecated
template-tag-spacing deprecated
wrap-iife deprecated
wrap-regex deprecated
yield-star-spacing deprecated
generator-star removed
Replaced by generator-star-spacing
global-strict removed
Replaced by strict
no-arrow-condition removed
Replaced by no-confusing-arrow no-constant-condition
no-comma-dangle removed
Replaced by comma-dangle
no-empty-class removed
Replaced by no-empty-character-class
no-empty-label removed
Replaced by no-labels
no-extra-strict removed
no-reserved-keys removed
Replaced by quote-props
no-space-before-semi removed
Replaced by semi-spacing
no-wrap-func removed
Replaced by no-extra-parens
space-after-function-name removed
Replaced by space-before-function-paren
space-after-keywords removed
Replaced by keyword-spacing
space-before-function-parentheses removed
space-before-keywords removed
space-in-brackets removed
Replaced by object-curly-spacing array-bracket-spacing
space-return-throw-case removed
space-unary-word-ops removed
Replaced by space-unary-ops
spaced-line-comment removed
Replaced by spaced-comment
valid-jsdoc removed
Replaced by
require-jsdoc removed
require or disallow assignment operator shorthand where possible (operator-assignment)
The --fix option on the command line can automatically fix some of the problems reported by this rule.
JavaScript provides shorthand operators that combine variable assignment and some simple mathematical operations. For example, x = x + 4 can be shortened to x += 4 . The supported shorthand forms are as follows:
Rule Details
This rule requires or disallows assignment operator shorthand where possible.
The rule applies to the operators listed in the above table. It does not report the logical assignment operators &&= , ||= , and ??= because their short-circuiting behavior is different from the other assignment operators.
This rule has a single string option:
- "always" (default) requires assignment operator shorthand where possible
- "never" disallows assignment operator shorthand
Examples of incorrect code for this rule with the default "always" option:
Examples of correct code for this rule with the default "always" option:
Examples of incorrect code for this rule with the "never" option:
Examples of correct code for this rule with the "never" option:
When Not To Use It
Use of operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis.
This rule was introduced in ESLint 0.10.0.
- Rule source
- Documentation source
© OpenJS Foundation and other contributors Licensed under the MIT License. https://eslint.org/docs/rules/operator-assignment
IMAGES
VIDEO