Skip to content

Fix thisArg binding in polyArrFindIndex / polyArrFindLastIndex polyfills#562

Merged
nev21 merged 4 commits into
mainfrom
copilot/fix-bitwise-or-in-polyfills
May 17, 2026
Merged

Fix thisArg binding in polyArrFindIndex / polyArrFindLastIndex polyfills#562
nev21 merged 4 commits into
mainfrom
copilot/fix-bitwise-or-in-polyfills

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 15, 2026

polyArrFindIndex and polyArrFindLastIndex were using bitwise OR (|) when resolving callback context, which coerced object thisArg values and broke callback this binding. This updates both polyfills to use logical fallback semantics as intended.

  • Polyfill callback context fix

    • Replaced thisArg | theArray with thisArg || theArray in:
      • lib/src/polyfills/array.ts (polyArrFindIndex)
      • lib/src/polyfills/array.ts (polyArrFindLastIndex)
    • Effect: callback this now correctly uses the provided thisArg, and falls back to theArray only when thisArg is not provided/falsy.
  • Regression coverage

    • Added focused tests in lib/test/src/common/polyfills/array.test.ts for both functions to verify:
      • explicit thisArg is preserved as callback this
      • default fallback binds callback this to the source array
// before (buggy)
callbackFn[CALL](thisArg | theArray as any, value, index, theArray);

// after
callbackFn[CALL](thisArg || theArray as any, value, index, theArray);

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • accounts.google.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=4244 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-7608247 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,17861204591039112903,10939758914733537520,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,16105935546789010716,16606217994416365594,4 --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /home/REDACTED/.cache/puppeteer/chrome/linux-148.0.7778.97/chrome-linux64/chrome /home/REDACTED/.cache/puppeteer/chrome/linux-148.0.7778.97/chrome-linux64/chrome --user-data-dir=/tmp/karma-7608247 --enable-automation --no-default-browser-check --no-first-run --disable-default-apps --disable-popup-blocking --disable-translate --disable-REDACTED-timer-throttling --disable-renderer-REDACTEDing --disable-device-discovery-notifications --no-sandbox --disable-gpu --disable-web-security --disable-dev-shm-usage http://localhost:9876/?id=7608247 --headless --disable-gpu --disable-dev-shm-usage --remote-debugging-port=9222 (dns block)
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=4481 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-68332849 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,7502828240700176064,4166064653183156238,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,16762107709327361543,16977408465753334385,4 --trace-process-track-uuid=3190708989122997041 (dns block)
  • clients2.google.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=4244 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-7608247 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,17861204591039112903,10939758914733537520,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,16105935546789010716,16606217994416365594,4 --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /home/REDACTED/.cache/puppeteer/chrome/linux-148.0.7778.97/chrome-linux64/chrome /home/REDACTED/.cache/puppeteer/chrome/linux-148.0.7778.97/chrome-linux64/chrome --user-data-dir=/tmp/karma-7608247 --enable-automation --no-default-browser-check --no-first-run --disable-default-apps --disable-popup-blocking --disable-translate --disable-REDACTED-timer-throttling --disable-renderer-REDACTEDing --disable-device-discovery-notifications --no-sandbox --disable-gpu --disable-web-security --disable-dev-shm-usage http://localhost:9876/?id=7608247 --headless --disable-gpu --disable-dev-shm-usage --remote-debugging-port=9222 (dns block)
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=4481 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-68332849 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,7502828240700176064,4166064653183156238,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,16762107709327361543,16977408465753334385,4 --trace-process-track-uuid=3190708989122997041 (dns block)
  • redirector.gvt1.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=4244 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-7608247 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,17861204591039112903,10939758914733537520,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,16105935546789010716,16606217994416365594,4 --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /home/REDACTED/.cache/puppeteer/chrome/linux-148.0.7778.97/chrome-linux64/chrome /home/REDACTED/.cache/puppeteer/chrome/linux-148.0.7778.97/chrome-linux64/chrome --user-data-dir=/tmp/karma-7608247 --enable-automation --no-default-browser-check --no-first-run --disable-default-apps --disable-popup-blocking --disable-translate --disable-REDACTED-timer-throttling --disable-renderer-REDACTEDing --disable-device-discovery-notifications --no-sandbox --disable-gpu --disable-web-security --disable-dev-shm-usage http://localhost:9876/?id=7608247 --headless --disable-gpu --disable-dev-shm-usage --remote-debugging-port=9222 (dns block)
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=4481 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-68332849 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,7502828240700176064,4166064653183156238,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,16762107709327361543,16977408465753334385,4 --trace-process-track-uuid=3190708989122997041 (dns block)
  • www.google.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=4244 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-7608247 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,17861204591039112903,10939758914733537520,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,16105935546789010716,16606217994416365594,4 --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /home/REDACTED/.cache/puppeteer/chrome/linux-148.0.7778.97/chrome-linux64/chrome /home/REDACTED/.cache/puppeteer/chrome/linux-148.0.7778.97/chrome-linux64/chrome --user-data-dir=/tmp/karma-7608247 --enable-automation --no-default-browser-check --no-first-run --disable-default-apps --disable-popup-blocking --disable-translate --disable-REDACTED-timer-throttling --disable-renderer-REDACTEDing --disable-device-discovery-notifications --no-sandbox --disable-gpu --disable-web-security --disable-dev-shm-usage http://localhost:9876/?id=7608247 --headless --disable-gpu --disable-dev-shm-usage --remote-debugging-port=9222 (dns block)
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=4481 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-68332849 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,7502828240700176064,4166064653183156238,262144 --disable-features=PaintHolding --variations-seed-version --pseudonymization-salt-handle=7,i,16762107709327361543,16977408465753334385,4 --trace-process-track-uuid=3190708989122997041 (dns block)
  • www.googleapis.com
    • Triggering command: /usr/local/bin/node node install.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Fix bug: Use logical OR in polyArrFindIndex and polyArrFindLastIndex Fix thisArg binding in polyArrFindIndex / polyArrFindLastIndex polyfills May 15, 2026
Copilot finished work on behalf of MSNev May 15, 2026 20:43
Copilot AI requested a review from MSNev May 15, 2026 20:43
@MSNev MSNev marked this pull request as ready for review May 15, 2026 23:48
@MSNev MSNev requested a review from a team as a code owner May 15, 2026 23:48
Copilot AI review requested due to automatic review settings May 15, 2026 23:48
@MSNev MSNev requested a review from a team as a code owner May 15, 2026 23:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes callback this binding in array find-index polyfills and adds regression coverage for the corrected behavior.

Changes:

  • Updates polyArrFindIndex and polyArrFindLastIndex callback invocation context handling.
  • Adds tests verifying object thisArg binding and default array binding.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lib/src/polyfills/array.ts Adjusts callback thisArg fallback logic for find-index polyfills.
lib/test/src/common/polyfills/array.test.ts Adds regression tests for callback context binding.
Comments suppressed due to low confidence (1)

lib/src/polyfills/array.ts:275

  • Using || still replaces explicitly supplied falsy thisArg values such as 0, false, or "" with the source array, so callbacks cannot observe those valid bindings. Use a check that only falls back when no usable default was intended (for example omitted/nullish, per the API docs) and add a regression case for falsy thisArg values.
        if (idx in theArray && callbackFn[CALL](thisArg || theArray as any, theArray[idx], idx, theArray)) {

Comment thread lib/src/polyfills/array.ts Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented May 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.43%. Comparing base (26b4766) to head (745458e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #562   +/-   ##
=======================================
  Coverage   99.43%   99.43%           
=======================================
  Files         150      150           
  Lines        4452     4452           
  Branches      956      955    -1     
=======================================
  Hits         4427     4427           
  Misses         25       25           
Files with missing lines Coverage Δ
lib/src/polyfills/array.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread lib/src/polyfills/array.ts Outdated
Copilot AI and others added 4 commits May 17, 2026 10:08
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@nev21 nev21 force-pushed the copilot/fix-bitwise-or-in-polyfills branch from 3879d25 to 745458e Compare May 17, 2026 17:09
@nev21 nev21 requested a review from Copilot May 17, 2026 17:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@nev21 nev21 enabled auto-merge (squash) May 17, 2026 17:39
@nev21 nev21 merged commit d65f853 into main May 17, 2026
13 of 14 checks passed
@nev21 nev21 deleted the copilot/fix-bitwise-or-in-polyfills branch May 17, 2026 17:39
nev21 added a commit that referenced this pull request May 19, 2026
## Release v0.14.0

This PR increases the version to `0.14.0` and updates the changelog with
all significant changes since v0.13.0.

### Summary of Changes

#### Features
- New array helpers and array-like detection (#525)
- `strReplace` and `strReplaceAll` string helpers (#527)
- `strCapitalizeWords` helper (#528)
- `strTruncate`, `strCount`, `strAt`, `strMatchAll` helpers (#529, #530)
- `arrFlatMap` with ES5 polyfill (#533)
- Typing utilities and expanded TSDoc examples (#535)
- `isAsyncIterable` and `isIntegerInRange` helpers (#536)
- `strStartsWithAny`, `strEndsWithAny`, `strWrap`, `strUnwrap`,
`strNormalizeNewlines` (#543)
- New object utility helpers and prototype pollution hardening (#564,
#565)

#### Bug Fixes
- Fix ES2015 built-in type errors in consumers by adding lib reference
directive to published declarations (#558)
- Fix `thisArg` binding in `polyArrFindIndex` / `polyArrFindLastIndex`
polyfills (#562)
- Fix falsy `thisArg` (0, `''`, `false`) being overridden in
`arrForEach`, `iterForOf`, `objForEachKey` (#566)

#### Repository Improvements
- Drop Node.js 16 from CI matrix and add Node.js 24 (#549)
- Upgrade Grunt devDependency to v1.6.2 (#552)
- Add funding metadata to published package manifests (#554)

### Files Updated
- `CHANGELOG.md` — Added v0.14.0 entry
- `package.json` — Version bumped to 0.14.0
- `lib/package.json` — Version bumped to 0.14.0
- `README.md` — Updated recommended version specification
@nev21 nev21 added this to the 0.14.0 milestone May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Bitwise OR (|) instead of logical OR (||) in polyArrFindIndex/polyArrFindLastIndex

4 participants