Skip to content

fix(prefer-spread): skip TypedArray and ArrayBuffer constructor calls#2871

Merged
sindresorhus merged 2 commits intosindresorhus:mainfrom
kaigritun:fix/prefer-spread-typed-arrays
Feb 10, 2026
Merged

fix(prefer-spread): skip TypedArray and ArrayBuffer constructor calls#2871
sindresorhus merged 2 commits intosindresorhus:mainfrom
kaigritun:fix/prefer-spread-typed-arrays

Conversation

@kaigritun
Copy link
Contributor

Fixes #2818

Problem

The prefer-spread rule was incorrectly suggesting to spread ArrayBuffer and TypedArray instances when using .slice(). This is problematic because:

  1. ArrayBuffer has no [Symbol.iterator], so spreading fails:

    Type 'ArrayBuffer' must have a '[Symbol.iterator]()' method that returns an iterator. (ts 2488)
    
  2. TypedArray.slice() returns the same typed array (e.g., Uint8Array), but spreading converts it to number[], silently breaking code expecting the original type.

    As mentioned in the issue:

    var sliced = u8.slice(0); // <--- Uint8Array
    var spread = [...u8]; // <--- number[]

Solution

Added detection for direct constructor calls to skip reporting:

  • new ArrayBuffer(10).slice()
  • new SharedArrayBuffer(10).slice()
  • new Uint8Array([1,2,3]).slice(0)
  • new Blob([]).slice()
  • new File([], 'test').slice()
  • All TypedArray variants (Int8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array)

Limitations

This fix detects direct constructor calls (new Uint8Array(...).slice()) but does not detect when a variable holds a TypedArray instance, as that would require type information not available to ESLint rules. The existing workaround of naming variables arrayBuffer, blob, etc. still applies for those cases.

Tests

  • Moved new Uint8Array([10, 20, 30, 40, 50]).slice() from invalid to valid tests
  • Added comprehensive test cases for all TypedArray constructors with .slice() and .slice(0)

@github-actions github-actions bot changed the title fix(prefer-spread): skip TypedArray and ArrayBuffer constructor calls fix(prefer-spread): skip TypedArray and ArrayBuffer constructor calls Feb 8, 2026
Comment on lines +61 to +62
'Blob',
'File',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not typed array.

__

and you can use rules/shared/typed-array.js

Fixes sindresorhus#2818

The rule was incorrectly suggesting to spread `ArrayBuffer` and `TypedArray`
instances when using `.slice()`. This is problematic because:

1. `ArrayBuffer` has no `[Symbol.iterator]`, so spreading fails with:
   "Type 'ArrayBuffer' must have a '[Symbol.iterator]()' method"

2. `TypedArray.slice()` returns the same typed array (e.g., `Uint8Array`),
   but spreading converts it to `number[]`, which silently breaks code
   expecting the original type.

This fix adds detection for direct constructor calls like:
- `new ArrayBuffer(10).slice()`
- `new Uint8Array([1,2,3]).slice(0)`
- `new Blob([]).slice()`
- `new File([], 'test').slice()`

And all TypedArray variants (Int8Array, Uint8Array, Float32Array, etc.)

Note: This does not detect when a variable holds a TypedArray instance,
as that would require type information not available to ESLint rules.
The existing workaround of naming variables `arrayBuffer`, `blob`, etc.
still applies for those cases.
@kaigritun kaigritun force-pushed the fix/prefer-spread-typed-arrays branch from 88a6473 to 9e14fc9 Compare February 8, 2026 23:08
@kaigritun
Copy link
Contributor Author

Thanks for the review! Fixed both issues:

  1. Removed Blob and File - you're right, they're not TypedArrays
  2. Now importing from rules/shared/typed-array.js instead of maintaining a separate list

@sindresorhus sindresorhus merged commit 42a558f into sindresorhus:main Feb 10, 2026
18 checks passed
@Qix-
Copy link

Qix- commented Feb 14, 2026

Might be neither here nor there @sindresorhus but OP is an AI Agent. Feels very XZ-utils vibes.

https://socket.dev/blog/ai-agent-lands-prs-in-major-oss-projects-targets-maintainers-via-cold-outreach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unicorn/prefer-spread: ArrayBuffer.prototype.slice incorrectly linted

4 participants