fix(arrays): clean up duplicate logic and improve array helpers#317600
Open
mohityadav8 wants to merge 20 commits into
Open
fix(arrays): clean up duplicate logic and improve array helpers#317600mohityadav8 wants to merge 20 commits into
mohityadav8 wants to merge 20 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors a few core utilities in src/vs/base/common/arrays.ts to reduce duplicated logic and adjust array helper implementations within the vs/base/common layer.
Changes:
- Reworked
concatArrays()to build results iteratively instead of using[].concat(...arrays). - Simplified
range()initialization logic. - Updated
withoutDuplicates()to delegate todistinct().
Comments suppressed due to low confidence (1)
src/vs/base/common/arrays.ts:594
withoutDuplicatespreviously preserved sparse-array holes asundefined(vianew Set(array)/ array iteration). Switching todistinct(array)changes behavior becausedistinctusesarray.filter(...), which skips holes entirely. If API compatibility for sparse arrays matters, consider keeping the old Set-based implementation here or updatingdistinct/this wrapper to iterate withfor...ofinstead offilter.
export function asArray<T>(x: T | T[]): T[];
export function asArray<T>(x: T | readonly T[]): readonly T[];
Author
|
@microsoft-github-policy-service agree |
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
src/vs/base/common/arrays.ts:593
withoutDuplicatesused to beArray.from(new Set(array)), which visits array holes via the iterator (treating them asundefined).distinct(array)usesfilter, which skips holes entirely, so sparse arrays will now lose the hole/undefinedentry and this is a behavior change despite the same signature. If you want to keep the previous semantics while reusingdistinct, consider callingdistinct(Array.from(array))(or keep the original Set-based implementation).
export function withoutDuplicates<T>(array: ReadonlyArray<T>): T[] {
return distinct(array);
}
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
src/vs/base/common/arrays.ts:213
- The manual copy loop in
concatArraysdoes not preserve sparse array holes: readingarr[i]and assigning toresult[...]will materialize holes as explicitundefinedentries, whereasArray.prototype.concatpreserves holes. If callers rely on hole semantics (e.g.i in arraychecks), consider copying only existing indices (e.g.if (i in arr)) and settingresult.length = offset + arr.lengthto keep holes.
for (const arr of arrays) {
const offset = result.length;
for (let i = 0; i < arr.length; i++) {
result[offset + i] = arr[i];
}
}
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Author
|
cc @mjbvz :D |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.
Summary
This PR improves several utilities in
src/vs/base/common/arrays.ts:range()withoutDuplicates()to reusedistinct()instead of duplicating logic, while preserving API compatibility[].concat(...arrays)inconcatArrays()with an iterative implementation to avoid spread argument limits and improve type safetyTesting
arrays.tsNotes
I intentionally did not remove deprecated exports (
move,insert,remove) since that would be a breaking API change and is better handled separately.