Skip to content

style(eslint): enable prefer-optional-chain rule - #12331

Merged
pkuczynski merged 2 commits into
masterfrom
style/eslint-prefer-optional-chain
Apr 10, 2026
Merged

pkuczynski merged 2 commits into
masterfrom
style/eslint-prefer-optional-chain

Conversation

@pkuczynski

Copy link
Copy Markdown
Member

Enable @typescript-eslint/prefer-optional-chain as suggested in #12255, replacing verbose && chains with optional chaining (?.).

@pkuczynski pkuczynski self-assigned this Apr 9, 2026
@pkg-pr-new

pkg-pr-new Bot commented Apr 9, 2026

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm/typeorm@12331
    

commit: c1ea54b

@pkuczynski
pkuczynski enabled auto-merge (squash) April 9, 2026 21:54
@sonarqubecloud

sonarqubecloud Bot commented Apr 9, 2026

Copy link
Copy Markdown

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (2)   📘 Rule violations (0)   📎 Requirement gaps (0)   🎨 UX Issues (0)
🐞\ ≡ Correctness (1) ☼ Reliability (1)

Grey Divider


Action required

1. Lint will fail 🐞 ☼
Description
The PR enables @typescript-eslint/prefer-optional-chain as an error for all **/*.ts, but there
are still x && x.y patterns (e.g., this.options && this.options.data/reload/chunk) that the rule
will flag, causing npm run lint to fail.
Code

eslint.config.mjs[40]

+            "@typescript-eslint/prefer-optional-chain": "error",
Evidence
ESLint is run repo-wide via npm run lint, and the new rule applies to all TS files. The current
branch still contains classic null-check && chains that match what prefer-optional-chain targets
(same base repeated with member access), so flipping the rule to error will introduce new lint
failures.

eslint.config.mjs[31-41]
package.json[78-91]
src/persistence/EntityPersistExecutor.ts[46-69]
src/persistence/SubjectExecutor.ts[431-476]
src/persistence/SubjectExecutor.ts[587-600]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`@typescript-eslint/prefer-optional-chain` is now an **error**, but there are still `&&`-based null checks in the repo that will be reported by the rule (and fail `npm run lint`).

## Issue Context
The eslint flat config applies to `files: ["**/*.ts"]`, and `package.json` runs `eslint` without path restrictions.

## Fix Focus Areas
- Replace `this.options && this.options.data` with optional chaining (or an explicit undefined check if falsy values must be preserved).
- Replace `this.options && this.options.reload === false` with `this.options?.reload === false`.
- Refactor the chunk logic to avoid `this.options && this.options.chunk && this.options.chunk > 0` (e.g., store `const chunk = this.options?.chunk` and use `chunk && chunk > 0`).

### Files/lines to update
- eslint.config.mjs[31-41]
- package.json[78-91]
- src/persistence/EntityPersistExecutor.ts[46-69]
- src/persistence/SubjectExecutor.ts[431-476]
- src/persistence/SubjectExecutor.ts[587-600]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Match index check wrong 🐞 ≡
Description
OrmUtils.parseSqlCheckExpression uses if (enumMatch?.index) which skips valid matches at offset
0, preventing enum parsing when the CHECK-expression pattern starts at the beginning of the SQL
string.
Code

src/util/OrmUtils.ts[324]

+        if (enumMatch?.index) {
Evidence
String.match() sets match.index to 0 when the match is at the beginning; checking `if
(enumMatch?.index) treats 0` as falsy and incorrectly bypasses the parsing logic even though a
match exists.

src/util/OrmUtils.ts[314-327]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`parseSqlCheckExpression` incorrectly uses a truthiness check on `enumMatch.index`, which fails when `index === 0`.

## Issue Context
When a regex match is at the start of the string, `match.index` is `0`.

## Fix Focus Areas
- Change the condition to an explicit undefined check, e.g. `if (enumMatch?.index !== undefined)` (or `!= null`).

### Files/lines to update
- src/util/OrmUtils.ts[314-327]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 73.708% (+0.004%) from 73.704% — style/eslint-prefer-optional-chain into master

@smith-xyz

Copy link
Copy Markdown
Collaborator

opened to discuss this one - the rule is definitely a net benefit because it prevents bugs (e.g. what if the var = 0)

Any migration cost if we do this? Strange bugs from rewriting the existing checks? Test look good, and the semantic correctness is a net benefit. Just want to make sure we're not missing something with these sweeping changes.

@pkuczynski
pkuczynski requested a review from OSA413 April 10, 2026 00:37
@pkuczynski
pkuczynski merged commit 7606c21 into master Apr 10, 2026
80 of 82 checks passed
@pkuczynski
pkuczynski deleted the style/eslint-prefer-optional-chain branch April 10, 2026 07:41
@github-actions github-actions Bot added this to the 1.0 milestone Apr 10, 2026
Nosfistis pushed a commit to Nosfistis/typeorm that referenced this pull request Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants