refactor(linters): add prefer-coerce-object rule - #45489
Draft
secustor wants to merge 3 commits into
Draft
Conversation
Closes renovatebot#45217 `x ?? []` and `coerceArray(x)` say the same thing, but only the second one is searchable and consistent. The rule flags a `??` whose default is a bare `[]` literal; a non-empty default or an asserted `([] as Foo[])` still passes. Applying it across `lib/` surfaced three spots the helper cannot cover, which keep `?? []` with a disable comment or a narrower fix: - `package-files.ts` destructures a tuple, which `coerceArray()` widens - `generate.spec.ts` defaults `matchAll()`, an iterator rather than an array - `mise/upgradeable-tooling.ts` declares a `Record` return, so its `?? []` fallback was wrong to begin with and becomes `coerceObject()`
Closes renovatebot#45216 The object counterpart to `prefer-coerce-array`: `x ?? {}` becomes `coerceObject(x)`. The rule flags a `??` whose default is a bare `{}` literal, so a populated default or an asserted `({} as Foo)` still passes; a `x ?? y ?? {}` chain maps onto `coerceObject(x, y)`. Every hit across `lib/` converted cleanly, so no disable comments were needed.
`Object.entries(...).find()` returns a tuple, and defaulting it with `?? []` only to destructure the second element reads as an array default when it is not one. Name the found entry instead, which drops the `?? []` rather than routing a tuple through `coerceArray()`.
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.
Changes
Important
Stacked on #45488 (
prefer-coerce-array). Onlyrefactor(linters): add prefer-coerce-object ruleis new here; the other two commits belong to #45488. The two rewrites touch overlapping lines in several files, so they are best reviewed and merged in order. GitHub cannot base a PR on a branch in a fork, so this one targetsmainand carries #45488's commits until that one lands.The object counterpart to
prefer-coerce-array: adds arenovate/prefer-coerce-objectoxlint rule and applies it acrosslib/.x ?? {}becomescoerceObject(x). The rule flags a??whose default is a bare{}literal, so a populated default (x ?? { a: 1 }) and an asserted one (x ?? ({} as Foo)) still pass — the latter keepslib/util/object.ts's own definition from tripping over itself. Ax ?? y ?? {}chain maps onto the two-argumentcoerceObject(x, y).The rule is enabled for
lib/**(including specs), alongsideprefer-coerce-array. Every one of the 65 hits converted cleanly, so unlike the array rule this one needed no disable comments.Context
Please select one of the following:
coerceObject#45216AI assistance disclosure
Did you use AI tools to create any part of this pull request?
Claude Opus 5 via Claude Code wrote the lint rule and its tests, and ran the codemod that applied the rule across
lib/.Use of AI in replying to PR comments
Who answers review comments:
Documentation (please check one with an [x])
How I've tested my work (please select one)
I have verified these changes via:
tools/lint/rules/prefer-coerce-object.spec.tscovers the rule itself. The existing suite covers the rewritten call sites;tsc --noEmit,oxlint,biomeandprettierare clean, andvitest run --changedpasses apart from two failures that also reproduce onmainin this environment (lib/logger/pretty-stdout.spec.tscolour detection and agit clonesubmodule test blocked byprotocol.file.allow).