From ffe2d0400bf8ac457cc4a9890e21a88cc443b289 Mon Sep 17 00:00:00 2001 From: Sloane Sturzenegger Date: Mon, 21 Aug 2023 15:09:14 -0700 Subject: [PATCH] Polyfill Array.prototype.at (#44436) fixes issue #44141 , discussion #44148 This is missing and has caused me issues in production. Seems like a great polyfill to have, given that Next already polyfills so many adjacent Array methods. ## Bug - [x] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [x] Related issues linked using `fixes #number` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) Co-authored-by: Eyas Valdez <37156127+spiltbeans@users.noreply.github.com> Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com> --- .../src/rules/no-unwanted-polyfillio.ts | 1 + packages/next-polyfill-module/src/index.js | 23 +++++++++++++++++++ packages/next-polyfill-nomodule/src/index.js | 1 + 3 files changed, 25 insertions(+) diff --git a/packages/eslint-plugin-next/src/rules/no-unwanted-polyfillio.ts b/packages/eslint-plugin-next/src/rules/no-unwanted-polyfillio.ts index b1ac5f2780d4..8732a730f381 100644 --- a/packages/eslint-plugin-next/src/rules/no-unwanted-polyfillio.ts +++ b/packages/eslint-plugin-next/src/rules/no-unwanted-polyfillio.ts @@ -3,6 +3,7 @@ import { defineRule } from '../utils/define-rule' // Keep in sync with next.js polyfills file : https://github.com/vercel/next.js/blob/master/packages/next-polyfill-nomodule/src/index.js const NEXT_POLYFILLED_FEATURES = [ 'Array.prototype.@@iterator', + 'Array.prototype.at', 'Array.prototype.copyWithin', 'Array.prototype.fill', 'Array.prototype.find', diff --git a/packages/next-polyfill-module/src/index.js b/packages/next-polyfill-module/src/index.js index bdb8268e9311..fa8f4c904ce5 100644 --- a/packages/next-polyfill-module/src/index.js +++ b/packages/next-polyfill-module/src/index.js @@ -118,3 +118,26 @@ if (!Object.fromEntries) { }, {}) } } + +/** + * Available in: + * Internet Explorer: never + * Edge: 92 + * Firefox: 90 + * Chrome: 92 + * Safari: 15.4 + * + * https://caniuse.com/mdn-javascript_builtins_array_at + */ +// Modified from TC39 at proposal polyfill: https://github.com/tc39/proposal-relative-indexing-method#polyfill +if (!Array.prototype.at) { + Array.prototype.at = function at(n) { + let i = Math.trunc(n) || 0 + + if (i < 0) i += this.length + + if (i < 0 || i >= this.length) return undefined + + return this[i] + } +} diff --git a/packages/next-polyfill-nomodule/src/index.js b/packages/next-polyfill-nomodule/src/index.js index 740973850bfb..67401e7b60c8 100644 --- a/packages/next-polyfill-nomodule/src/index.js +++ b/packages/next-polyfill-nomodule/src/index.js @@ -1,5 +1,6 @@ /* eslint-disable import/no-extraneous-dependencies */ // Keep in sync with eslint no-unwanted-polyfillio rule: https://github.com/vercel/next.js/blob/master/packages/eslint-plugin-next/lib/rules/no-unwanted-polyfillio.js +import 'core-js/features/array/at' import 'core-js/features/array/copy-within' import 'core-js/features/array/fill' import 'core-js/features/array/find'