fix(linter/unicorn/prefer-math-trunc): don't suggest a fix for a side-effecting assignment LHS#23531
Closed
hyf0 wants to merge 1 commit into
Closed
fix(linter/unicorn/prefer-math-trunc): don't suggest a fix for a side-effecting assignment LHS#23531hyf0 wants to merge 1 commit into
hyf0 wants to merge 1 commit into
Conversation
…-effecting assignment LHS The `x |= 0` → `x = Math.trunc(x)` suggestion duplicates the assignment LHS source text. For a side-effecting LHS this re-runs the side effect: e.g. `foo[i++] |= 0` was suggested as `foo[i++] = Math.trunc(foo[i++])`, evaluating `i++` twice; likewise `obj[bar()] ^= 0`, `getFoo().baz >>= 0`, etc. Detect a side effect in the LHS (Call / New / Update / Await / Yield / Assignment / dynamic `import` / `delete`, matching eslint-plugin-unicorn's `hasSideEffect`) and report without a suggestion in that case. Side-effect-free targets (`x`, `foo.bar`, `[foo][0]`) still get the fix.
Merging this PR will not alter performance
Comparing Footnotes
|
camc314
requested changes
Jun 17, 2026
camc314
left a comment
Contributor
There was a problem hiding this comment.
Thanks for working on this.
The approach you've taken doesn't work correctly in certain scenarios.
For example, given the following:
obj.foo.bar |= 0;The current suggestion would become:
obj.foo.bar = Math.trunc(obj.foo.bar);which evaluates obj.foo twice.
If that property read is a getter/proxy, this suggestion can change behaviour.
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
The
x |= 0→x = Math.trunc(x)suggestion is built by duplicating the assignment LHS source text. For a side-effecting LHS this re-runs the side effect:Expected
Side-effecting LHS should still be reported but offered NO fix/suggestion — eslint-plugin-unicorn guards exactly this (
if (!isAssignment || !hasSideEffect(left, sourceCode))).Fix
Detect a side effect in the assignment LHS (Call / New / Update / Await / Yield / Assignment / dynamic
import/delete— matching eslint-utilshasSideEffect) via a smallVisitwalker, and report without a suggestion in that case. Side-effect-free targets (x,foo.bar,[foo][0]) still get the fix. Regression tests added for the side-effecting cases.Prepared with AI assistance.