Skip to content

fix(prefer-optional-chain): validate restarted OR chain guards - #1299

Open
farnoodma wants to merge 1 commit into
oxc-project:mainfrom
farnoodma:fix/prefer-optional-chain-reset-guards
Open

farnoodma wants to merge 1 commit into
oxc-project:mainfrom
farnoodma:fix/prefer-optional-chain-reset-guards

Conversation

@farnoodma

@farnoodma farnoodma commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Problem

prefer-optional-chain accepts a value comparison as a new OR-chain guard when an unrelated operand precedes it:

declare const input: { text: string; ctrl: boolean };
if (input.ctrl || input.text === ' ' || input.text.length !== 1) {}

The comparison against ' ' is meaningful and cannot be replaced by an optional chain. The same two trailing operands are correctly left alone without the leading input.ctrl.

Change

Apply isInvalidOrChainStartingOperand when restarting an OR chain, matching the validation already used when starting a fresh chain. This prevents the restarted chain from treating a non-nullish value comparison as a nullish guard.

Add regressions for a leading property, a separate boolean, and a reversed literal comparison. This is distinct from the trailing-comparison behavior in #1127: the invalid operand here starts a new chain after an unrelated operand.

Validation

  • All three new valid cases fail on current main (8897346) before the change and pass afterward.
  • go test ./internal/rules/prefer_optional_chain passes, including the existing valid/invalid and fix snapshots.
  • Full just ready passes: formatting, golangci-lint, build, E2E, and all Go tests.
  • The synthetic examples are clean under @typescript-eslint/prefer-optional-chain 8.63.0.

AI assistance: This contribution was prepared with OpenAI Codex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant