linter: unicorn/no-useless-spread fixer removes a string spread and produces a TypeError
unicorn/no-useless-spread treats [...someString.slice(a, b)] as a useless spread and its fixer removes it, producing code that throws TypeError: ... is not a function at runtime.
The spread is what turns the string into an array of characters. Removing it leaves an array method being called on a String.
Version
oxlint 1.79.0
Reproduction
.oxlintrc.json:
{ "plugins": ["unicorn"], "categories": { "correctness": "error" } }repro.js:
const nif = '123456789'
export const a = [...nif.slice(0, 8)].reduce((acc, ch) => acc + Number(ch), 0)
const arr = [1, 2, 3, 4]
export const b = [...arr.slice(0, 2)].map((n) => n + 1)
export const c = [...nif.split('')].lengthoxlint --fix repro.js produces:
const nif = '123456789'
export const a = nif.slice(0, 8).reduce((acc, ch) => acc + Number(ch), 0) //
const arr = [1, 2, 3, 4]
export const b = arr.slice(0, 2).map((n) => n + 1) // correct
export const c = nif.split('').length // correctRunning the fixed file:
TypeError: "12345678".reduce is not a functionWhat is and is not wrong here
Lines b and c are fixed correctly — Array.prototype.slice and String.prototype.split both return arrays, so the spread really is useless there. Only line a is wrong.
That is what makes the diagnosis narrow: the rule appears to assume the receiver of .slice() is an array. String.prototype.slice returns a string, and for a string the spread is load-bearing — it is the conversion to string[].
Note that a bare [...someStringVariable] is not flagged, so the false positive seems specific to the method-call receiver rather than to string spreads in general.
Expected
[...expr] should not be reported when expr may be a string. If the receiver type is not known, the conservative answer is to skip the report rather than emit a fix that changes runtime behaviour.
Why this one bites hard
The fixer is --fix-safe by default, the rule sits in correctness, and the resulting file still lints clean afterwards — oxlint reports zero problems on the broken output. In our case the only thing that caught it was tsc; a JS-only project running oxlint --fix in CI would have shipped it. This is why I am reporting it separately from #6618 (closed, new Array(3), different mechanism).
Source: oxc-project/oxc