New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support logical assignments #13569
Comments
These rules/helpers should be probably updated:
It looks like these rules should be also updated, but it might be a breaking change for babel-eslint users:
This rule probably needs a documentation update:
These rules probably shouldn't be updated:
Some other rules and helpers not listed here don't check |
* update operator-assignment rule * update astUtils.couldBeError * update constructor-super rule * add tests for operator-linebreak rule * add tests for space-infix-ops rule * add tests for no-invalid-this rule * add tests for no-param-reassign rule * add tests for no-bitwise rule * add tests for func-name-matching rule * add tests for prefer-destructuring rule * add tests for no-extend-native rule * Update docs/rules/operator-assignment.md Co-authored-by: Brandon Mills <btmills@users.noreply.github.com> * Fix &&= in astUtils.couldBeError * Fix &&= in constructor-super * Fix comment Co-authored-by: Brandon Mills <btmills@users.noreply.github.com>
Marked as "breaking" to update the |
Node.js v15 now supports logitcal assignment operators: https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V15.md Chrome 85, Firefox 79 and Safari 14 all support it too. With Node now supporting it, more and more developers are going to be adopting it I'm sure. |
ESLint also supports logical assignment operators, as of v7.8.0. If you're using the default parser, just update the "parserOptions": {
"ecmaVersion": 2021
} The core and most of the rules have been updated in v7.8.0. We left this issue open for changes that were considered to be breaking for |
This comment has been minimized.
This comment has been minimized.
@aduh95 this isn't valid VariableStatement, |
I just stumbled upon a complaint from ESLint that might be related to these changes. The rule is const resultMap = {};
const accessGroup = (group) => resultMap[group] ??= []; ESlint complains here, because the return of the arrow function conatins an assignment. That is technically correct, but I do not think that it is a problem. Let's leave aside, whether or not this is the most beautiful solution. The
This justification does not work as well for logical assignment operators. Since: The complaint even persists when the intent is shifted and thus explicated further by adding additional code (as long as the const resultMap = {};
const addElementToGroup = ({ element, group }) => (resultMap[group] ??= []).push(element); My current intuition would be to exempt logical assignments from this rule. Otherwise the rule's justification might have to be extended to cover logical assignments as different cases. What do you think? |
@TimMoser92 the rule helps prevent conflating assignments with expressions, and it absolutely should apply to the logical assignment operators. If the docs imply different, I think they should be updated. |
If that is the correct justification, the docs do not seem to imply it. They primarily mention other specific problems that can arise from mixing non-logical assignments into return statements. The justification you give is much more general and would of course also cover logical assignment operators. But if the goal is to never mix assignments into expressions, why is there no general rule about it? Statements like So I am not sure, what the solution is here and where this should be decided, nor do I have a strong opinion in any direction, but it seems documented justification and behaviour do not currently match. |
All planned tasks are now completed, so I'm closing this issue. @TimMoser92 the behavior for /* eslint no-return-assign: ["error", "always"] */
const accessGroup1 = (group) => resultMap[group] ??= []; // reports error
const accessGroup2 = (group) => resultMap[group] += []; // also reports error If there seems to be something in the rule and/or the documentation for this rule that should be corrected in this regard, feel free to open a separate issue. |
Logical assignment proposal is now Stage 4.
https://github.com/tc39/proposal-logical-assignment
ESLint's default parser will support this syntax under
ecmaVersion: 2021
operator-assignment
Update: support logical assignments in core rules (refs #13569) #13618constructor-super
Update: support logical assignments in core rules (refs #13569) #13618astUtils.couldBeError
Update: support logical assignments in core rules (refs #13569) #13618no-constant-condition
Update: check logical assignment in no-constant-condition #13946complexity
Update: check logical assignment operators in the complexity rule #13979func-names
Docs: add class fields in func-names documentation (refs #14857) #14908no-self-assign
? feat: handle logical assignment in no-self-assign #14152The text was updated successfully, but these errors were encountered: