refactor(linters): add prefer-coerce-array rule - #45488
Draft
secustor wants to merge 2 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()`
15 tasks
`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
Adds a
renovate/prefer-coerce-arrayoxlint rule and applies it acrosslib/.x ?? []andcoerceArray(x)say the same thing, but only the second one is searchable and consistent, so the rule flags a??whose default is a bare[]literal. A meaningful default (x ?? [1]) and an asserted one (x ?? ([] as Foo[])) still pass, as does a destructuring or parameter default, where the helper has no equivalent form.The rule is enabled for
lib/**(including specs).tools/**is left out because it may not import fromlib/.Applying it rewrote 153 sites in 84 files. Three of those the helper cannot cover, and they are handled individually rather than silenced wholesale:
lib/workers/repository/package-files.ts— tuple destructuring default;coerceArray()widens the tuple to a union array, so the?? []stays with a disable commentlib/workers/repository/updates/generate.spec.ts— defaultsmatchAll(), which yields an iterator rather than an array; same, with a disable commentlib/modules/manager/mise/upgradeable-tooling.ts— declares aRecord<string, string>return, so the?? []fallback was the wrong shape to begin with and becomescoerceObject()A fourth,
lib/modules/platform/github/index.ts, defaulted anObject.entries(...).find()tuple only to destructure one element out of it; that one is hoisted into a named variable in a separate commit, which drops the?? []altogether.The rewrite is mechanical and behaviour-preserving otherwise: for a
T[] | null | undefinedinput,coerceArray(x)andx ?? []are equivalent.#45216(coerceObject) builds on this branch — the two touch overlapping lines in several files, so they are best reviewed and merged in order.Context
Please select one of the following:
coerceArray#45217AI 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-array.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).