Repository navigation
fix: stop shipping ES2023 array built-ins to an es2022 build target #23005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d2e4191
bd2c8f4
ba9aa2a
d5c5d76
04ada8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,73 @@ | ||||||||||||||||||||||||||||||||||||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import { findLast, findLastIndex } from './arrayUtil' | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| describe('findLastIndex', () => { | ||||||||||||||||||||||||||||||||||||
| it.for([ | ||||||||||||||||||||||||||||||||||||
| { name: 'returns the last match', array: [1, 2, 3, 4], expected: 3 }, | ||||||||||||||||||||||||||||||||||||
| { name: 'returns -1 with no match', array: [1, 1], expected: -1 }, | ||||||||||||||||||||||||||||||||||||
| { name: 'returns -1 when empty', array: [], expected: -1 } | ||||||||||||||||||||||||||||||||||||
| ])('$name', ({ array, expected }) => { | ||||||||||||||||||||||||||||||||||||
| expect(findLastIndex(array, (value) => value % 2 === 0)).toBe(expected) | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| it('searches from the end and stops at the first match', () => { | ||||||||||||||||||||||||||||||||||||
| const predicate = vi.fn((value: number) => value === 2) | ||||||||||||||||||||||||||||||||||||
| expect(findLastIndex([1, 2, 3], predicate)).toBe(1) | ||||||||||||||||||||||||||||||||||||
| expect(predicate.mock.calls.map(([value]) => value)).toEqual([3, 2]) | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| it('passes the index and the array to the predicate', () => { | ||||||||||||||||||||||||||||||||||||
| const array = ['a', 'b'] | ||||||||||||||||||||||||||||||||||||
| const predicate = vi.fn(() => false) | ||||||||||||||||||||||||||||||||||||
| findLastIndex(array, predicate) | ||||||||||||||||||||||||||||||||||||
| expect(predicate.mock.calls).toEqual([ | ||||||||||||||||||||||||||||||||||||
| ['b', 1, array], | ||||||||||||||||||||||||||||||||||||
| ['a', 0, array] | ||||||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| it('treats any truthy predicate result as a match', () => { | ||||||||||||||||||||||||||||||||||||
| expect(findLastIndex([0, 1, 0], (value) => value)).toBe(1) | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| describe('findLast', () => { | ||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Run these existing cases without native I checked the installed
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in ba9aa2a. Every findLast result case now runs with the native method removed and restores its descriptor afterward. The helper suite passes in that environment. |
||||||||||||||||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||||||||||||||||
| const descriptor = Object.getOwnPropertyDescriptor( | ||||||||||||||||||||||||||||||||||||
| Array.prototype, | ||||||||||||||||||||||||||||||||||||
| 'findLast' | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| Reflect.deleteProperty(Array.prototype, 'findLast') | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return () => { | ||||||||||||||||||||||||||||||||||||
| if (descriptor) { | ||||||||||||||||||||||||||||||||||||
| Object.defineProperty(Array.prototype, 'findLast', descriptor) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| it.for([ | ||||||||||||||||||||||||||||||||||||
| { name: 'returns the last match', array: [1, 2, 3, 4], expected: 4 }, | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| name: 'returns undefined with no match', | ||||||||||||||||||||||||||||||||||||
| array: [1, 3], | ||||||||||||||||||||||||||||||||||||
| expected: undefined | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| { name: 'returns undefined when empty', array: [], expected: undefined }, | ||||||||||||||||||||||||||||||||||||
| { name: 'returns a matched falsy element', array: [0], expected: 0 } | ||||||||||||||||||||||||||||||||||||
| ])('$name', ({ array, expected }) => { | ||||||||||||||||||||||||||||||||||||
| expect(findLast(array, (value) => value % 2 === 0)).toBe(expected) | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| it('returns the matched value before the predicate changes the array', () => { | ||||||||||||||||||||||||||||||||||||
| const array = [1, 2, 3] | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| expect( | ||||||||||||||||||||||||||||||||||||
| findLast(array, () => { | ||||||||||||||||||||||||||||||||||||
| array.pop() | ||||||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| ).toBe(3) | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** ES2022-safe equivalent of `Array.prototype.findLastIndex`. */ | ||
| export function findLastIndex<T>( | ||
| array: readonly T[], | ||
| predicate: (value: T, index: number, array: readonly T[]) => unknown | ||
| ): number { | ||
| for (let index = array.length - 1; index >= 0; index--) { | ||
| if (predicate(array[index], index, array)) return index | ||
| } | ||
| return -1 | ||
| } | ||
|
|
||
| /** ES2022-safe equivalent of `Array.prototype.findLast`. */ | ||
| export function findLast<T>( | ||
| array: readonly T[], | ||
| predicate: (value: T, index: number, array: readonly T[]) => unknown | ||
| ): T | undefined { | ||
| for (let index = array.length - 1; index >= 0; index--) { | ||
| const value = array[index] | ||
| if (predicate(value, index, array)) return value | ||
| } | ||
| return undefined | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include TSX files in the package source lint glob.
The package source glob lists
js,mjs,cjs,ts,mts,cts, andvue, but nottsx. The same override excludes.test.tsxfiles, so the omission is not intentional. A shipped TSX file underpackages/*/srcthat callsfindLastor another restricted method would not be reported. That call can fail in an ES2022 browser. No such call is present now, so this prevents a future regression.🛠️ Suggested fix
Committable suggestion