Repository navigation
Conversation
This branch has not been deployed
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.
Problem
enableArrayMethods()overridesslicewith an optimized implementation. The helper that normalizes the start and end arguments only clamps them to the array bounds:The specification applies
ToIntegerOrInfinitybefore clamping. As a result, non-integer and non-numeric arguments are interpreted differently from nativesliceand from Immer without the plugin:slice(NaN)returns[]instead of the whole array.slice(1.5)returns[undefined, undefined, undefined]for[1, 2, 3, 4]instead of[2, 3, 4].slice(-1.5)returns twoundefinedentries instead of[4].slice(0.5, 3.5)returns[undefined, undefined, undefined]instead of[1, 2, 3].The same helper is used for the
splicestart index, so this also corrects the insertion index used for cross-reference handling there.Fix
I added a
toIntegerOrInfinityhelper and used it insidenormalizeSliceIndex.Number.isNaNandMath.truncmirror the specification conversion, and unary+keeps the nativeTypeErrorfor symbols.This does not change the existing explicit
nullend behavior; that case is covered separately by #1321.Tests
Added
__tests__/array-methods-slice-index.js, which runs the same argument matrix with the plugin on and off and withautoFreezeon and off. It coversNaN, positive and negative fractions, numeric strings, and infinities.Result: 44 passed (44).
The full suite passes: 4257 passed, 8 skipped. Reverting the source change makes 12 of the new tests fail, for example
slice(-1.5)yields[undefined, undefined]instead of[4], and the standalone reproduction reports 6 of 11 cases differing from nativeslice.Notes
This is plugin-only: the unoptimized path already matches native
slice. I could not find an existing issue for this exact argument coercion case, so the report here is based on a direct comparison between the plugin, the unoptimized path, and nativeslice.