Repository navigation
fix(codemode): skip holes when flatMap flattens the mapper's array - #57394
aniruddhaadak80 wants to merge 1 commit into
Conversation
flatMap spread the array the mapper returned into the result, so absent (hole) entries became explicit `undefined` values: `[1].flatMap(() => [, 2])` returned `[undefined, 2]` where JS returns `[2]`, and `[1].flatMap(() => Array(2))` returned `[undefined, undefined]` where JS returns `[]`. FlattenIntoArray appends an element only when the index is present on the mapped array, so walk it with an `in` check instead of spreading. An explicit `undefined` element is still kept, and the flatten stays one level deep.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
|
The following comment was made by an LLM, it may be inaccurate: |
|
Upstream already landed an equivalent fix for #56925 (the issue was closed at 06:13 UTC today), so this branch is now conflicting and redundant. Closing. |
|
Thanks for the fix and the clear write-up! The latest |
Issue for this PR
Closes #56925
Type of change
What does this PR do?
Array.prototype.flatMapin the interpreter spread the array the mapper returned into the result (values.push(...mapped.items)). A spread copies indices0..length-1whether or not they are present, so absent (hole) entries became explicitundefined:[1].flatMap(() => [, 2])returned[undefined, 2]; JS returns[2][1].flatMap(() => Array(2))returned[undefined, undefined]; JS returns[]The spec flattens with
FlattenIntoArray, which appends a mapped element only when the index is actually present (HasProperty) — so holes contribute nothing while an explicitundefinedelement is kept. Walk the mapped array with anincheck instead of spreading it; the flatten stays one level deep either way.How did you verify your code works?
Added tests in
packages/codemode/test/parity.test.tscovering the cases from the issue (holes skipped,Array(2)collapses to[], resulting length, explicitundefinedkept).bun test test/parity.test.ts— new test passesbun test(whole codemode package) — 1398 pass; the single failure (RegExp > String methods read and update lastIndex like exec) also fails on an unmodifieddevcheckoutbun run typecheckinpackages/codemode— cleanScreenshots / recordings
Not a UI change.
Checklist