Skip to content

fix(linter/no-negated-condition): add autofix for negated if/ternary#23803

Closed
JonasBa wants to merge 2 commits into
oxc-project:mainfrom
JonasBa:fix/no-negated-condition-autofix
Closed

fix(linter/no-negated-condition): add autofix for negated if/ternary#23803
JonasBa wants to merge 2 commits into
oxc-project:mainfrom
JonasBa:fix/no-negated-condition-autofix

Conversation

@JonasBa

@JonasBa JonasBa commented Jun 26, 2026

Copy link
Copy Markdown

Summary

  • Make eslint/no-negated-condition and unicorn/no-negated-condition fixable via shared logic in rules/shared/eslint_unicorn/no_negated_condition.rs.
  • Autofix inverts the test (! removal, or != / !==== / ===) and swaps consequent/alternate branches.
  • Covers unicorn-aligned edge cases: non-block if arms (brace wrap), ASI when removing a leading !, return/throw spacing and multi-line paren wrapping, and fixing only the inner else if in if / else if / else chains.
  • Binary inequalities whose left operand is still ! only rewrite the operator (e.g. !a !== b!a === b), matching eslint-plugin-unicorn.

AI usage: This PR was developed with AI assistance. I reviewed the logic, tests, and unicorn snapshot parity (with one intentional whitespace delta on multi-line return ! // comment fixes: one space after ( vs unicorn’s two).

Test plan

  • cargo test -p oxc_linter --lib no_negated_condition
  • CI on this PR
  • Spot-check with oxlint --fix on a few negated if/? : samples if useful

Jonas Badalic added 2 commits June 25, 2026 20:51
Make eslint and unicorn `no-negated-condition` fixable by inverting the
test (`!` removal or `!=`/`!==` → `==`/`===`) and swapping branches,
including brace wrapping for non-block if arms and ASI / return-throw
edge cases aligned with eslint-plugin-unicorn.
Collapse duplicate swap helpers, inline ternary edge-case handling, and
trim comments so the invert-test + swap-arms flow is easier to follow.
@JonasBa JonasBa force-pushed the fix/no-negated-condition-autofix branch from a141268 to c9e25b1 Compare June 26, 2026 15:25
@JonasBa JonasBa marked this pull request as ready for review June 26, 2026 15:27
@JonasBa JonasBa requested a review from camc314 as a code owner June 26, 2026 15:27
@JonasBa JonasBa marked this pull request as draft June 26, 2026 15:28
@camc314 camc314 added the A-linter Area - Linter label Jun 28, 2026
@camc314

camc314 commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Thanks for working on this!

This got fixed via #23825

@camc314 camc314 closed this Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-linter Area - Linter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants