From d2e419155a548c9f19584ef5c6d8388abcf1811f Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 5 Nov 2026 04:59:25 -0800 Subject: [PATCH 1/3] fix: stop shipping ES2023 array built-ins to an es2022 build target `Array.prototype.findLastIndex` is ES2023. `vite.config.mts` builds with `target: 'es2022'` and Vite downlevels syntax only -- it never polyfills a built-in -- so the call shipped untouched and is simply absent below Chrome 97 / Safari 15.4 / Firefox 104, inside the range es2022 claims. In `toggleSwitch` the throw is inside `bootstrap()` at `src/main.ts:179`, before `app.mount('#vue-app')` on line 182, so the app never mounts: a blank page rather than a missing toggle. Sentry CLOUD-FRONTEND-PROD-3FQ (issue 7830684654) on release 1.59.5, served pin 072c3c1140172bbc8ff25cbdd1644cea3ec2f16c. Two events, both Sogou Explorer 1.0 on Chromium 94 (`Chrome/94.0.4606.71 ... SE 2.X MetaSr 1.0`). The receiver is a real Array -- `switchItems` comes from `items.map(...)` six lines above the throw -- so this is the missing built-in, not an array-like receiver. `comfy/no-es2023-array-copy-method` already existed to prevent exactly this, with the message "ES2023 array method is not polyfilled for build target es2022", but its set held only toReversed/toSorted/toSpliced/with. findLast and findLastIndex were never added, which is how this reached production under a rule written to stop it. They are in the set now, the rule is renamed `no-es2023-array-method` since the set is no longer copy-only, and its scope widens from `src/**` to include `packages/*/src/**` -- account-ui ships in the same browser bundle and held a live `.toSorted()` that nothing would catch. Completing the rule means the 11 other in-scope call sites have to be clean, and each was the same break waiting for the same cohort, so all are converted. Three are Agent-surface; the defect itself has no Agent caller. `es-toolkit/compat` is not an option: its `findLastIndex` ends in `subArray.findLastIndex(doesMatch)` and its `findLast` in `source.slice(...).findLast(...)`, so both call the native ES2023 method and would move the crash one frame deeper. Lowering `tsconfig` to lib ES2022 was considered and rejected: it is more general, but it drags in 9 src test files plus browser_tests/ (16 sites), tools/ (15) and apps/billing-web, all Node or Playwright code where ES2023 is legitimate, and tsc's own "Do you need to change your target library?" text suggests the wrong fix. The toggleSwitch test deletes the own-property off `Array.prototype` and restores the saved descriptor afterwards. Without the fix it reproduces the production error verbatim: `TypeError: switchItems.findLastIndex is not a function`. --- .oxlintrc.json | 12 +++-- .../src/billing/tab/billingBannerView.ts | 4 +- src/composables/videoConcat/videoSequence.ts | 5 +- src/core/graph/widgets/dynamicWidgets.ts | 5 +- .../assets/composables/media/assetMappers.ts | 9 ++-- .../assets/composables/useAssetSelection.ts | 3 +- src/scripts/ui/toggleSwitch.test.ts | 31 ++++++++++- src/scripts/ui/toggleSwitch.ts | 4 +- src/stores/executionStore.ts | 3 +- src/stores/queueStore.ts | 3 +- src/utils/arrayUtil.test.ts | 51 +++++++++++++++++++ src/utils/arrayUtil.ts | 19 +++++++ .../extensions/agent/AgentPanelRoot.vue | 21 ++++---- .../composables/agent/useAgentSession.ts | 4 +- .../agent/services/agent/agentTranscript.ts | 3 +- tools/oxlint-plugins/comfy.ts | 4 +- tools/oxlint-plugins/restrictedSyntax.test.ts | 13 +++-- tools/oxlint-plugins/restrictedSyntax.ts | 14 ++--- 18 files changed, 166 insertions(+), 42 deletions(-) create mode 100644 src/utils/arrayUtil.test.ts create mode 100644 src/utils/arrayUtil.ts diff --git a/.oxlintrc.json b/.oxlintrc.json index d2a0e71e4cb..9e8d0d4dc0c 100644 --- a/.oxlintrc.json +++ b/.oxlintrc.json @@ -277,10 +277,16 @@ } }, { - "files": ["src/**/*.{js,mjs,cjs,ts,mts,cts,vue}"], - "excludeFiles": ["src/**/*.test.{ts,tsx}"], + "files": [ + "src/**/*.{js,mjs,cjs,ts,mts,cts,vue}", + "packages/*/src/**/*.{js,mjs,cjs,ts,mts,cts,vue}" + ], + "excludeFiles": [ + "src/**/*.test.{ts,tsx}", + "packages/*/src/**/*.test.{ts,tsx}" + ], "rules": { - "comfy/no-es2023-array-copy-method": "error" + "comfy/no-es2023-array-method": "error" } }, { diff --git a/packages/account-ui/src/billing/tab/billingBannerView.ts b/packages/account-ui/src/billing/tab/billingBannerView.ts index c71141d7ce8..526e82d641d 100644 --- a/packages/account-ui/src/billing/tab/billingBannerView.ts +++ b/packages/account-ui/src/billing/tab/billingBannerView.ts @@ -203,7 +203,5 @@ export function selectBillingBanners( const suppressCredit = views.some(({ kind }) => CREDIT_SUPPRESSORS.has(kind)) return views .filter(({ kind }) => !(suppressCredit && CREDIT_KINDS.has(kind))) - .toSorted( - (a, b) => STACK_ORDER.indexOf(a.kind) - STACK_ORDER.indexOf(b.kind) - ) + .sort((a, b) => STACK_ORDER.indexOf(a.kind) - STACK_ORDER.indexOf(b.kind)) } diff --git a/src/composables/videoConcat/videoSequence.ts b/src/composables/videoConcat/videoSequence.ts index 2f446bd65c8..aba56fcb79b 100644 --- a/src/composables/videoConcat/videoSequence.ts +++ b/src/composables/videoConcat/videoSequence.ts @@ -1,3 +1,5 @@ +import { findLastIndex } from '@/utils/arrayUtil' + interface SequencePosition { index: number offset: number @@ -19,7 +21,8 @@ export function locateSequenceTime( const starts = sequenceStarts(durations) const lastIndex = durations.length - 1 if (lastIndex < 0) return { index: 0, offset: 0 } - const index = starts.findLastIndex( + const index = findLastIndex( + starts, (start, i) => start <= time && (i === lastIndex || durations[i] > 0) ) if (index < 0) return { index: 0, offset: 0 } diff --git a/src/core/graph/widgets/dynamicWidgets.ts b/src/core/graph/widgets/dynamicWidgets.ts index 3aef8b4fd62..fe06c84742c 100644 --- a/src/core/graph/widgets/dynamicWidgets.ts +++ b/src/core/graph/widgets/dynamicWidgets.ts @@ -35,6 +35,7 @@ import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { WidgetValue } from '@/types/simplifiedWidget' import { widgetId } from '@comfyorg/graph-model/widgetId' import { dynamicGroupWidget } from './dynamicGroupWidget' +import { findLast, findLastIndex } from '@/utils/arrayUtil' type MatchTypeNode = LGraphNode & Pick, 'onConnectionsChange'> & { @@ -518,7 +519,7 @@ function addAutogrowGroup( groupName, node ).name - const lastIndex = node.inputs.findLastIndex((inp) => + const lastIndex = findLastIndex(node.inputs, (inp) => inp.name.startsWith(targetName) ) const insertionIndex = lastIndex === -1 ? node.inputs.length : lastIndex + 1 @@ -548,7 +549,7 @@ function autogrowInputConnected(index: number, node: AutogrowNode) { const input = node.inputs.at(index) if (!input) return const groupName = input.name.slice(0, input.name.lastIndexOf('.')) - const lastInput = node.inputs.findLast((inp) => + const lastInput = findLast(node.inputs, (inp) => inp.name.startsWith(groupName + '.') ) const ordinal = resolveAutogrowOrdinal(input.name, groupName, node) diff --git a/src/platform/assets/composables/media/assetMappers.ts b/src/platform/assets/composables/media/assetMappers.ts index 38284b48c63..0ad1a436f57 100644 --- a/src/platform/assets/composables/media/assetMappers.ts +++ b/src/platform/assets/composables/media/assetMappers.ts @@ -14,6 +14,7 @@ import { getMediaTypeFromFilename, isPreviewableMediaType } from '@/utils/formatUtil' +import { findLast } from '@/utils/arrayUtil' /** * Extract asset type from tags array @@ -133,11 +134,9 @@ export function unflattenOutputAssets( const grouped = [...assetsByJob.entries()].map(([job_id, assets]) => { const ordered = [...assets].sort(byCreatedAtAsc) const representative = - [...ordered] - .sort(byIsTemp) - .findLast((asset) => - isPreviewableMediaType(getMediaTypeFromFilename(asset.name)) - ) ?? ordered.at(-1)! + findLast([...ordered].sort(byIsTemp), (asset) => + isPreviewableMediaType(getMediaTypeFromFilename(asset.name)) + ) ?? ordered.at(-1)! return { ...representative, id: job_id, diff --git a/src/platform/assets/composables/useAssetSelection.ts b/src/platform/assets/composables/useAssetSelection.ts index 82b155f05aa..471fb7d5540 100644 --- a/src/platform/assets/composables/useAssetSelection.ts +++ b/src/platform/assets/composables/useAssetSelection.ts @@ -7,6 +7,7 @@ import { getAssetOutputCount, getTotalAssetOutputCount } from '@/platform/assets/utils/outputAssetCountUtil' +import { findLastIndex } from '@/utils/arrayUtil' export function useAssetSelection() { const selectionStore = useAssetSelectionStore() @@ -129,7 +130,7 @@ export function useAssetSelection() { function setSelectedIds(ids: string[], allAssets: AssetItem[]) { selectionStore.setSelection(ids) const selected = new Set(ids) - const anchorIndex = allAssets.findLastIndex((asset) => + const anchorIndex = findLastIndex(allAssets, (asset) => selected.has(asset.id) ) setAnchor(anchorIndex, anchorIndex >= 0 ? allAssets[anchorIndex].id : null) diff --git a/src/scripts/ui/toggleSwitch.test.ts b/src/scripts/ui/toggleSwitch.test.ts index 7b6f71ab2d8..fa7df6a134b 100644 --- a/src/scripts/ui/toggleSwitch.test.ts +++ b/src/scripts/ui/toggleSwitch.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it, vi } from 'vitest' +import { afterEach, describe, expect, it, vi } from 'vitest' import { toggleSwitch } from './toggleSwitch' @@ -70,6 +70,35 @@ describe('toggleSwitch', () => { }) }) + describe('on a browser without Array.prototype.findLastIndex', () => { + const descriptor = Object.getOwnPropertyDescriptor( + Array.prototype, + 'findLastIndex' + ) + + afterEach(() => { + if (descriptor) { + Object.defineProperty(Array.prototype, 'findLastIndex', descriptor) + } + }) + + it('still honours the last selected item', () => { + delete (Array.prototype as { findLastIndex?: unknown }).findLastIndex + expect('findLastIndex' in Array.prototype).toBe(false) + + const container = toggleSwitch('mode', [ + { text: 'a', selected: true }, + { text: 'b', value: 'B', selected: true }, + { text: 'c' } + ]) + + expect(readState(container)).toEqual({ + selected: [false, true, false], + checked: [false, true, false] + }) + }) + }) + it('renders value, tooltip and radio grouping', () => { const container = toggleSwitch('mode', [ { text: 'Fast', value: 'fast', tooltip: 'Go fast' }, diff --git a/src/scripts/ui/toggleSwitch.ts b/src/scripts/ui/toggleSwitch.ts index ff16cabfb4f..b5b2b9da2e4 100644 --- a/src/scripts/ui/toggleSwitch.ts +++ b/src/scripts/ui/toggleSwitch.ts @@ -1,3 +1,5 @@ +import { findLastIndex } from '@/utils/arrayUtil' + import { $el } from './utils' interface ToggleSwitchItem { @@ -31,7 +33,7 @@ export function toggleSwitch( : { ...item, value: item.value ?? item.text } ) const initialIndex = Math.max( - switchItems.findLastIndex((item) => item.selected), + findLastIndex(switchItems, (item) => item.selected), 0 ) diff --git a/src/stores/executionStore.ts b/src/stores/executionStore.ts index 8f52d0eda3e..ea8a828ef1e 100644 --- a/src/stores/executionStore.ts +++ b/src/stores/executionStore.ts @@ -44,6 +44,7 @@ import { zeroUuid } from '@comfyorg/graph-model/uuid' import { classifyCloudValidationError } from '@/utils/executionErrorUtil' import { executionIdToNodeLocatorId } from '@/utils/graphTraversalUtil' import { createRafCoalescer } from '@/utils/rafBatch' +import { findLast } from '@/utils/arrayUtil' type RuntimeExecutionError = Omit< ExecutionErrorWsMessage, @@ -1267,7 +1268,7 @@ export const useExecutionStore = defineStore('execution', () => { const startedJobIds = Object.keys(queuedJobs.value).filter( (jobId) => queuedJobs.value[jobId].executionStartedAt !== undefined ) - const ownedJobId = startedJobIds.findLast((jobId) => + const ownedJobId = findLast(startedJobIds, (jobId) => rememberStampAndCheckActiveWorkflow(jobId) ) executionIdToLocatorCache.clear() diff --git a/src/stores/queueStore.ts b/src/stores/queueStore.ts index b16b9ac4e8c..9f93fdc3c5a 100644 --- a/src/stores/queueStore.ts +++ b/src/stores/queueStore.ts @@ -23,6 +23,7 @@ import { useExecutionStore } from '@/stores/executionStore' import { useJobModelDownloadStore } from '@/stores/jobModelDownloadStore' import { tryNormalizeNodeExecutionId } from '@comfyorg/graph-model/nodeIdentification' import { useSettingStore } from '@/platform/settings/settingStore' +import { findLast } from '@/utils/arrayUtil' enum TaskItemDisplayStatus { Running = 'Running', @@ -85,7 +86,7 @@ export class TaskItemImpl { const previewable = this.previewableOutputs // Prefer the last saved media file (most recent result) over temp previews return ( - previewable.findLast((output) => output.type === 'output') ?? + findLast(previewable, (output) => output.type === 'output') ?? previewable.at(-1) ) } diff --git a/src/utils/arrayUtil.test.ts b/src/utils/arrayUtil.test.ts new file mode 100644 index 00000000000..189814cddc9 --- /dev/null +++ b/src/utils/arrayUtil.test.ts @@ -0,0 +1,51 @@ +import { 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', () => { + it('returns the last matching element', () => { + expect(findLast([1, 2, 3, 4], (value) => value % 2 === 0)).toBe(4) + }) + + it('returns undefined with no match', () => { + expect(findLast([1, 3], (value) => value % 2 === 0)).toBeUndefined() + }) + + it('returns undefined when empty', () => { + expect(findLast([], () => true)).toBeUndefined() + }) + + it('distinguishes a matched falsy element from no match', () => { + expect(findLast([0], (value) => value === 0)).toBe(0) + }) +}) diff --git a/src/utils/arrayUtil.ts b/src/utils/arrayUtil.ts new file mode 100644 index 00000000000..fd1fce8294a --- /dev/null +++ b/src/utils/arrayUtil.ts @@ -0,0 +1,19 @@ +/** ES2022-safe equivalent of `Array.prototype.findLastIndex`. */ +export function findLastIndex( + 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( + array: readonly T[], + predicate: (value: T, index: number, array: readonly T[]) => unknown +): T | undefined { + const index = findLastIndex(array, predicate) + return index === -1 ? undefined : array[index] +} diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 4d927210994..4425775870a 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -196,6 +196,7 @@ import { attachAgentWriteOverwriteWatch } from './crdt/agentWriteOverwrites' import { attachDocOpMinter } from './crdt/docOpMinter' import { attachRestoreOpMinter } from './crdt/restoreOpMinter' import { useAgentCrdtFollower } from './crdt/useAgentCrdtFollower' +import { findLast } from '@/utils/arrayUtil' const CrdtDevPanel = defineAsyncComponent( () => import('./crdt/CrdtDevPanel.vue') @@ -1379,15 +1380,17 @@ useEventListener( const completedTurnClaimedCanvasContent = ref(false) function completedTurnCanClaimCanvasContent(): boolean { - const lastCanvasTool = conversationMessages.value - .at(-1) - ?.parts.findLast( - (part) => - part.type === 'tool' && - (part.name === 'clear_canvas' - ? part.ok === true - : part.ok !== false && toolCanClaimCanvasContent(part.name)) - ) + const lastParts = conversationMessages.value.at(-1)?.parts + const lastCanvasTool = lastParts + ? findLast( + lastParts, + (part) => + part.type === 'tool' && + (part.name === 'clear_canvas' + ? part.ok === true + : part.ok !== false && toolCanClaimCanvasContent(part.name)) + ) + : undefined return ( lastCanvasTool?.type === 'tool' && lastCanvasTool.name !== 'clear_canvas' ) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 7c69db71cc8..346be2e69f3 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -74,6 +74,7 @@ import { useAgentWorkflowTabBindingStore } from '@/workbench/extensions/agent/st import type { ComposerAttachment } from '@/workbench/extensions/agent/types/composerAttachment' import type { WorkflowReference } from '@/workbench/extensions/agent/types/workflowReference' import { serializeWorkflowReferences } from '@/workbench/extensions/agent/utils/workflowReferenceText' +import { findLast } from '@/utils/arrayUtil' export interface AgentEventSource { subscribe(listener: (raw: unknown) => void): () => void @@ -3009,7 +3010,8 @@ export function useAgentSession(deps: AgentSessionDeps) { if (latest && !isTerminalTurnStatus(latest.status)) return { kind: 'streaming', - pendingAsk: rows.findLast( + pendingAsk: findLast( + rows, (row) => !isTerminalTurnStatus(row.status) && row.pending_ask !== undefined )?.pending_ask diff --git a/src/workbench/extensions/agent/services/agent/agentTranscript.ts b/src/workbench/extensions/agent/services/agent/agentTranscript.ts index 0830a0b2754..57cc92fce6f 100644 --- a/src/workbench/extensions/agent/services/agent/agentTranscript.ts +++ b/src/workbench/extensions/agent/services/agent/agentTranscript.ts @@ -22,6 +22,7 @@ import { isOpenAskPart, toAskOrNoticePart } from './agentMessageParts' +import { findLast } from '@/utils/arrayUtil' type AttachmentKind = 'image' | 'video' | 'audio' @@ -544,7 +545,7 @@ function keepAuthoritativeAsk( isAskRetired: (askId: string) => boolean ): void { const rowAsk = row.status === 'streaming' ? row.pending_ask : undefined - const priorAvailableAskId = message.parts.findLast((part) => { + const priorAvailableAskId = findLast(message.parts, (part) => { const askId = askIdOf(part) return ( isOpenAskPart(part) && diff --git a/tools/oxlint-plugins/comfy.ts b/tools/oxlint-plugins/comfy.ts index 0b506be960e..f37e10d0f35 100644 --- a/tools/oxlint-plugins/comfy.ts +++ b/tools/oxlint-plugins/comfy.ts @@ -20,7 +20,7 @@ import { noDeprecatedApiSchema, noDirectSelectionWrite, noDomInComputed, - noEs2023ArrayCopyMethod, + noEs2023ArrayMethod, noJsPrivateClassMembers, noMisplacedSpecFiles, noNewZodForRemoteApiTypes, @@ -55,7 +55,7 @@ export default { 'no-js-extension-imports': noJsExtensionImports, 'no-js-private-class-members': noJsPrivateClassMembers, 'no-duplicate-ingest-type': noDuplicateIngestType, - 'no-es2023-array-copy-method': noEs2023ArrayCopyMethod, + 'no-es2023-array-method': noEs2023ArrayMethod, 'no-import-actual': noImportActual, 'no-misplaced-spec-files': noMisplacedSpecFiles, 'no-mocked-in-expect': noMockedInExpect, diff --git a/tools/oxlint-plugins/restrictedSyntax.test.ts b/tools/oxlint-plugins/restrictedSyntax.test.ts index 24c829b4d5d..ae4fb214ea2 100644 --- a/tools/oxlint-plugins/restrictedSyntax.test.ts +++ b/tools/oxlint-plugins/restrictedSyntax.test.ts @@ -200,6 +200,8 @@ const method = 'map' as const items[method]() items.sort() void fn +items.findLast((item) => item > 1) +items.findLastIndex((item) => item > 1) ` }, { @@ -215,6 +217,7 @@ void items source: `const items = [1, 2] items.toReversed() items['toSorted']() +items.findLastIndex((item) => item > 1) ` }, { @@ -506,9 +509,9 @@ describe('restricted syntax rules', () => { ) }) - it('rejects statically named ES2023 array copy calls outside unit tests', () => { - const copyFindings = findingsFor('no-es2023-array-copy-method') - expect(locations(copyFindings)).toEqual([ + it('rejects statically named ES2023 array method calls outside unit tests', () => { + const arrayFindings = findingsFor('no-es2023-array-method') + expect(locations(arrayFindings)).toEqual([ ['arrayCopy.ts', 2], ['arrayCopy.ts', 3], ['arrayCopy.ts', 4], @@ -517,9 +520,11 @@ describe('restricted syntax rules', () => { ['arrayCopy.ts', 7], ['arrayCopy.ts', 8], ['arrayCopy.ts', 9], + ['arrayCopy.ts', 15], + ['arrayCopy.ts', 16], ['arrayCopy.vue', 2] ]) - expect(copyFindings.every(({ severity }) => severity === 'error')).toBe( + expect(arrayFindings.every(({ severity }) => severity === 'error')).toBe( true ) }) diff --git a/tools/oxlint-plugins/restrictedSyntax.ts b/tools/oxlint-plugins/restrictedSyntax.ts index a5e75f88f20..f73b314414f 100644 --- a/tools/oxlint-plugins/restrictedSyntax.ts +++ b/tools/oxlint-plugins/restrictedSyntax.ts @@ -13,14 +13,16 @@ const DOM_METHOD_MESSAGES = new Map([ ]) const PRIMEVUE_MODULE = /^(?:primevue(?:\/|$)|@primevue(?:\/|$)|@primeuix(?:\/|$))/ -const ES2023_ARRAY_COPY_METHODS = new Set([ +const ES2023_ARRAY_METHODS = new Set([ + 'findLast', + 'findLastIndex', 'toReversed', 'toSorted', 'toSpliced', 'with' ]) -const ES2023_ARRAY_COPY_MESSAGE = - 'ES2023 array method is not polyfilled for build target es2022; use the matching ES2022-safe non-mutating equivalent.' +const ES2023_ARRAY_MESSAGE = + 'ES2023 array method is not polyfilled for build target es2022; use the matching ES2022-safe equivalent (findLast/findLastIndex live in @/utils/arrayUtil).' const VITEST_MOCK_METHOD_NAMES = new Set([ 'mockClear', 'mockReset', @@ -402,14 +404,14 @@ export const noPrimeVueImports = restrictModules( 'PrimeVue usage is banned. Use the project design-system components instead.' ) -export const noEs2023ArrayCopyMethod = { +export const noEs2023ArrayMethod = { create(context: RuleContext) { return { CallExpression(node: CallExpression) { if (node.callee.type !== 'MemberExpression') return const method = staticPropertyName(node.callee as MemberExpression) - if (method !== undefined && ES2023_ARRAY_COPY_METHODS.has(method)) { - context.report({ node, message: ES2023_ARRAY_COPY_MESSAGE }) + if (method !== undefined && ES2023_ARRAY_METHODS.has(method)) { + context.report({ node, message: ES2023_ARRAY_MESSAGE }) } } } From bd2c8f4f1b8697facd96cfb3d3f75171690c4ddc Mon Sep 17 00:00:00 2001 From: Alexander Brown Date: Thu, 5 Nov 2026 18:09:39 +0000 Subject: [PATCH 2/3] test: cover reverse-search callback mutation --- src/utils/arrayUtil.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/utils/arrayUtil.test.ts b/src/utils/arrayUtil.test.ts index 189814cddc9..bf21f119115 100644 --- a/src/utils/arrayUtil.test.ts +++ b/src/utils/arrayUtil.test.ts @@ -48,4 +48,15 @@ describe('findLast', () => { it('distinguishes a matched falsy element from no match', () => { expect(findLast([0], (value) => value === 0)).toBe(0) }) + + 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) + }) }) From ba9aa2ac205d9742ac11affd4b544f5908bc0a94 Mon Sep 17 00:00:00 2001 From: Alexander Brown Date: Thu, 5 Nov 2026 18:18:05 +0000 Subject: [PATCH 3/3] fix: preserve reverse-search semantics and compatibility coverage --- src/scripts/ui/toggleSwitch.test.ts | 3 +- src/utils/arrayUtil.test.ts | 35 ++++++++++++------- src/utils/arrayUtil.ts | 7 ++-- tools/oxlint-plugins/restrictedSyntax.test.ts | 10 +++++- tools/oxlint-plugins/restrictedSyntax.ts | 2 +- 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/scripts/ui/toggleSwitch.test.ts b/src/scripts/ui/toggleSwitch.test.ts index fa7df6a134b..81d06daa987 100644 --- a/src/scripts/ui/toggleSwitch.test.ts +++ b/src/scripts/ui/toggleSwitch.test.ts @@ -83,8 +83,7 @@ describe('toggleSwitch', () => { }) it('still honours the last selected item', () => { - delete (Array.prototype as { findLastIndex?: unknown }).findLastIndex - expect('findLastIndex' in Array.prototype).toBe(false) + Reflect.deleteProperty(Array.prototype, 'findLastIndex') const container = toggleSwitch('mode', [ { text: 'a', selected: true }, diff --git a/src/utils/arrayUtil.test.ts b/src/utils/arrayUtil.test.ts index bf21f119115..21f6e799c42 100644 --- a/src/utils/arrayUtil.test.ts +++ b/src/utils/arrayUtil.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import { findLast, findLastIndex } from './arrayUtil' @@ -33,20 +33,31 @@ describe('findLastIndex', () => { }) describe('findLast', () => { - it('returns the last matching element', () => { - expect(findLast([1, 2, 3, 4], (value) => value % 2 === 0)).toBe(4) - }) - - it('returns undefined with no match', () => { - expect(findLast([1, 3], (value) => value % 2 === 0)).toBeUndefined() - }) + beforeEach(() => { + const descriptor = Object.getOwnPropertyDescriptor( + Array.prototype, + 'findLast' + ) + Reflect.deleteProperty(Array.prototype, 'findLast') - it('returns undefined when empty', () => { - expect(findLast([], () => true)).toBeUndefined() + return () => { + if (descriptor) { + Object.defineProperty(Array.prototype, 'findLast', descriptor) + } + } }) - it('distinguishes a matched falsy element from no match', () => { - expect(findLast([0], (value) => value === 0)).toBe(0) + 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', () => { diff --git a/src/utils/arrayUtil.ts b/src/utils/arrayUtil.ts index fd1fce8294a..ec7fa369893 100644 --- a/src/utils/arrayUtil.ts +++ b/src/utils/arrayUtil.ts @@ -14,6 +14,9 @@ export function findLast( array: readonly T[], predicate: (value: T, index: number, array: readonly T[]) => unknown ): T | undefined { - const index = findLastIndex(array, predicate) - return index === -1 ? undefined : array[index] + for (let index = array.length - 1; index >= 0; index--) { + const value = array[index] + if (predicate(value, index, array)) return value + } + return undefined } diff --git a/tools/oxlint-plugins/restrictedSyntax.test.ts b/tools/oxlint-plugins/restrictedSyntax.test.ts index ae4fb214ea2..eb3c086de6b 100644 --- a/tools/oxlint-plugins/restrictedSyntax.test.ts +++ b/tools/oxlint-plugins/restrictedSyntax.test.ts @@ -15,6 +15,7 @@ const privateSigil = '#' const probeDirs = { source: path.resolve('src/__restricted_syntax_probes__'), + package: path.resolve('packages/__restricted_syntax_probes__'), app: path.resolve('apps/__restricted_syntax_probes__/src'), remote: path.resolve('src/platform/remote/__restricted_syntax_probes__'), selection: path.resolve('src/composables/graph/__restricted_syntax_probes__'), @@ -204,6 +205,12 @@ items.findLast((item) => item > 1) items.findLastIndex((item) => item > 1) ` }, + ...['arrayPackage.ts', 'arrayPackage.test.ts'].map((name) => ({ + file: path.join(probeDirs.package, 'src', name), + source: `const items = [1, 2] +items.findLastIndex((item) => item > 1) +` + })), { file: path.join(probeDirs.source, 'arrayCopy.vue'), source: `