Issue 563: exact array-conversion lint policy #
Status:
implemented;
issues 563,
564,
and 565 closed by commits on main.
Issue: https://github.com/Aquaticat/Monochromatic/issues/563.
Problem #
unicorn/no-useless-spread (oxlint 1.85.0,
error via correctness) removes the spread in [...x.m()]
whenever m is a method name that returns a fresh array on Array.prototype.
It has no type information,
so it also rewrites strings,
typed arrays,
and iterators.
--fix applies it because the rule emits a SafeFix despite its fix_dangerous metadata.
Evidence gathered #
Reproductions on oxlint 1.85.0 #
[...text.slice(0, 3,),].some(...)becomestext.slice(0, 3,).some(...): TS2339.Array.from(typed.map(double,),)becomes[...typed.map(double,),]viaunicorn/prefer-spread, thentyped.map(double,)viano-useless-spread:Uint8Arrayinstead ofnumber[], silent when inferred.unicorn/prefer-spreadalone rewritesscoopGrams.slice()to[...scoopGrams](typed array becomesnumber[]) andmenuCode.slice()to[...menuCode](caught afterwards byno-misused-spread).- Autofixed diagnostics never print under the wrapper's fix loop (
oxlint-wrapper.ts:254-277), sooxlint-guidance.tsentries cannot warn about an applied fix.
Upstream #
- oxc issue 26159 tracks the
no-useless-spreadstring case; PRs 26160 and 26615 are open and unreviewed. - eslint-plugin-unicorn removed the unsafe fix in PR 1996 (2022) and now reports unknown receivers without a fix.
- oxlint cannot disable one rule's CLI autofix,
and neither
no-useless-spreadnorprefer-spreadhas options. - oxlint JS plugins receive no type information (oxc issue 19962); tsgolint has no custom-rule mechanism.
- unicorn
prefer-iterator-to-arrayis the closest precedent for explicit conversions (.toArray()).
Prototype #
Branch prototype/issue-563-explicit-conversions,
worktree ~/temp/agent/issue-563-conversions,
directory package/oxlint-plugin/no-restricted-syntax/prototype-issue-563/.
The branch is committed and pushed after building the forbidden-strings scanner in the worktree;
issue 563 carries a pointer comment.
- Narrow-typed helpers (
copyArray,numbersOf,collect,codePointsOf) make TypeScript 7.0.2 reject every wrong receiver pairing tried (8@ts-expect-errorcontrols; a TS2578 positive control proves the probe). - Because
copyArrayonly accepts arrays, a syntax-only rule can reportcopyArray(x.m())as a useless copy with no false positive in code that type-checks. It must not autofix: on code that fails type-checking the unwrap would recreate the silent break. - Learning cost of the helper vocabulary:
four names,
met only when the narrow ban fires (1 current hit),
listed in the lint message;
a wrong pick fails type-checking.
copyArray,numbersOf, andcodePointsOferrors read plainly ('string' is not assignable to 'readonly unknown[]').collectwith alength?: neverguard produced'number' is not assignable to 'undefined'; a string-literal guard type puts the remedy into the TypeScript error text instead. - Repo-wide run of the prototype rule over
package/: 1 spread of an ambiguous method result (a deliberate test fixture), 175 spreads of other calls, 465 spreads of values, 15Array.fromcalls; 280 of the 656 hits sit in tests, fixtures, or fuzz packages.
Built-ins replace the helpers #
builtins.prototype.ts shows built-in spellings already let TypeScript reject every wrong receiver tried
(6 @ts-expect-error controls):
iterator.toArray() exists only on iterators;
.values().toArray() covers sets,
maps,
arrays,
and typed arrays but not strings;
array.toSpliced(0,) copies arrays only.
What built-ins cannot do without types:
tell a needless x.map(f,).values().toArray() on an array
from a needed one on a typed array.
The user noted progressive enhancement applies:
a type-free baseline,
with type information
narrowing messages and enabling safe fixes where the bridge can answer.
Candidate policies #
- Narrow ban with built-in vocabulary:
ban
[...x.m()]for ambiguousm; authors use.toArray(),.values().toArray(), string APIs. - Narrow ban with typed helper vocabulary: the same ban plus helpers and a report-only useless-copy check.
- Broad ban:
every sole spread and
Array.fromconversion; 656 current hits. - Type-aware rule over the TypeScript 7 bridge in
prefer-readonly-parameter-type.
Settled so far #
- Scope excludes the string-only
prefer-spreadtono-misused-spreadchain (user answer, Q2). - Upstream fixes are not awaited;
repo precedent replaces problematic upstream rules with project rules
(
package/config/oxlint/src/rule/restriction.ts:115-140).
prefer-spread is not needed off for the baseline (correction) #
An earlier recommendation to turn unicorn/prefer-spread off was retracted.
Its Array.from(x.m(),) rewrite lands in the banned shape,
but the baseline only reports,
so the author sees an error instead of a silent break and the fix loop has no second fix to oscillate with.
Its concat arm only reports;
its no-argument toSpliced() arm cannot type-check anyway.
Its slice() arm is a separate standalone defect:
scoopGrams.slice() becomes [...scoopGrams],
a [...value] spread the baseline does not ban.
Repo uses of single-argument Array.from are 2 deliberate string code-point splits,
each suppressing prefer-spread
(package/git-policy/cli/src/parser/commit-normalise.ts:122,
package/module/i18n-compose/src/render-helpers.ts:147).
Decisions (2026-09-25) #
- Baseline:
no-restricted-syntax/no-useless-spreadaterrorreplacesunicorn/no-useless-spread. Non-method checks keep safe autofixes;[...x.m(),]for ambiguous method names (includingsplit) reports without a fix and names the built-in spellings (Q6). - Type-aware enhancement ships in this session under its own issue: where TypeScript resolves the file, the rule names the receiver kind and autofixes proven arrays (Q8).
unicorn/prefer-spreadis replaced, not turned off, under its own issue, in this session (Q7):Array.from(x,)to[...x]keeps its fix (equivalent for every iterable);x.slice()fixes only when types prove an array. Without types it still reports, without a fix (user correction: code without types holds itself to a higher standard to compensate), naming the unambiguous spellings:[...x]for arrays,new Uint8Array(x,)style constructors for typed arrays, plainxfor immutable strings. With types, a proven typed-array or string receiver is not reported.- No upstream contact; oxc issue 26159 is cited.
Implementation findings #
- Rules:
package/oxlint-plugin/no-restricted-syntax/src/rule/no-useless-spread/,package/oxlint-plugin/no-restricted-syntax/src/rule/prefer-spread/, shared evidence inpackage/oxlint-plugin/no-restricted-syntax/src/rule/spread-evidence/. Tests:package/oxlint-plugin/no-restricted-syntax/src/spread-rules.unit.test.ts; removing either typed-array or ambiguity guard fails the issue 563 or 565 tests (negative controls run before commit). - The TypeScript 7 bridge is imported from the readonly plugin's
/tssubpath; the bundled@monochromatic-dev/config-oxlintoutput places it in one shared chunk, so both plugins use one TypeScript process. sourceCode.isGlobalReferencedropsArrayandObjectunder the shared config's explicitenv; both rules resolve globals by the absence of a file-local declaration instead (doc/troubleshooting/oxlint-js-plugin-global-reference-env.md).- A repo-wide run found
Buffer.concat(chunks,)reported as an ambiguous arrayconcatin an untyped file; undeclared receivers are now treated as host namespaces. - Enabling both rules repo-wide adds no diagnostics outside ignored test fixtures,
except two
prefer-spreadwarnings in untyped bench analysis scripts that already carry 270 lint problems and are not lint-clean targets. - The two
unicorn/prefer-spreadsuppressions aroundArray.from(string,)were removed; the project rule leavesArray.fromof strings alone. - Known limitation shared with upstream:
[...arr]andarr.slice()differ on sparse arrays (spread fills holes withundefined); types cannot prove density, so fixes on proven arrays assume dense arrays.
Open questions #
- None.
Posting the three upstream drafts is tracked for the user in repo issues 567
(oxc issue 26159 comment),
568
(
prefer-spreadissue), and 569 (isGlobalReferenceissue), each with a runbook whose local steps were dry-run.