fix(prefer-spread): skip TypedArray and ArrayBuffer constructor calls#2871
Merged
sindresorhus merged 2 commits intosindresorhus:mainfrom Feb 10, 2026
Merged
Conversation
prefer-spread): skip TypedArray and ArrayBuffer constructor calls
sindresorhus
reviewed
Feb 8, 2026
rules/prefer-spread.js
Outdated
Comment on lines
+61
to
+62
| 'Blob', | ||
| 'File', |
Owner
There was a problem hiding this comment.
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.
88a6473 to
9e14fc9
Compare
Contributor
Author
|
Thanks for the review! Fixed both issues:
|
fisker
approved these changes
Feb 9, 2026
|
Might be neither here nor there @sindresorhus but OP is an AI Agent. Feels very XZ-utils vibes. |
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.
Fixes #2818
Problem
The
prefer-spreadrule was incorrectly suggesting to spreadArrayBufferandTypedArrayinstances when using.slice(). This is problematic because:ArrayBuffer has no
[Symbol.iterator], so spreading fails:TypedArray.slice() returns the same typed array (e.g.,
Uint8Array), but spreading converts it tonumber[], silently breaking code expecting the original type.As mentioned in the issue:
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()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 variablesarrayBuffer,blob, etc. still applies for those cases.Tests
new Uint8Array([10, 20, 30, 40, 50]).slice()from invalid to valid tests.slice()and.slice(0)