Repository navigation
Conversation
`Array.prototype.toSorted()` (ES2023) is not available in Chrome < 110, Safari < 16 and Firefox < 115. Since WordPress#52213, calling it in the editor throws `TypeError: ... .toSorted is not a function` in those browsers, which crashes the editor (e.g. when opening the inserter). - Replace browser-side `.toSorted( cmp )` calls with `[ ...array ].sort( cmp )`, or `.sort( cmp )` when chained directly on a `.filter()` result that is already a new array. Behavior is unchanged: a new sorted array is returned and the input is not mutated. - Add a `no-restricted-syntax` ESLint rule against `toSorted`, exempting Node-only tooling (`bin/`, `tools/`, `babel-plugin-makepot`), where it is supported. Fixes WordPress#86129. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🤖 PR meta 🤖👋 Welcome👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @mdtanjid0! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. 🎉 PropsUnlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @mohammad.tanjid@automattic.com. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the PR. I think reverting to Curious to hear what @jsnajdr and others thing about the lint rule however, which codifies an exception to the browserlist rules. |
|
I'd rather prefer #86159 which adds the |
|
Closing in favor of #86159. |
What?
Closes #86129
Replaces browser-side
Array.prototype.toSorted()calls with an explicit copy-then-sort, and adds an ESLint rule sotoSortedisn't reintroduced in code that runs in the browser.Why?
#52213 switched several sorts to
Array.prototype.toSorted()(ES2023).toSortedis not available in Chrome < 110, Safari < 16 or Firefox < 115, and the polyfill build doesn't include it because those browsers are outside the browserslist targets. In those browsers the call throwsTypeError: … .toSorted is not a function, which crashes the editor, for example when opening the inserter (orderByinblock-editor/src/utils/sorting.ts).Some of these browser versions cannot be upgraded on the operating systems they run on. For example, the last Chrome for macOS 10.11/10.12 is 103, and the last Chrome for Windows 7/8.1 is 109. Before #52213 the editor loaded in these browsers.
How?
array.toSorted( cmp )→[ ...array ].sort( cmp )in all 13 browser-side call sites. The behavior is the same: a new sorted array is returned, the input is not mutated, andArray.prototype.sortis stable..filter()(blocks/src/api/factory.ts,core-fields/…/media_thumbnail/view.tsx), it becomes a plain.sort(), becausefilteralready returns a new array.noArrayToSortedentry to the sharedrestrictedSyntaxlist intools/eslint/config.mjs. A new override exempts Node-only tooling (bin/,tools/,packages/babel-plugin-makepot), wheretoSortedis supported, so the existing calls there stay as they are.test/performancehas its ownno-restricted-syntaxconfig and is unaffected.Testing Instructions
delete Array.prototype.toSortedto simulate Chrome < 110 / Safari < 16.toSortedstill deleted, smoke-test the other changed paths:npx eslinton any file underpackages/*/srcthat calls.toSorted()should now reportno-restricted-syntax.Automated checks run locally:
npm run test:unitfor the affected areas (block-editor utils, blocks API, gallery/navigation/playlist, editor collab-sidebar/collaborators-presence/post-taxonomies, views, media-fields, core-fields, fields and media-utils media-edit): 107 files and 1,457 tests pass.Array.prototype.toSorteddeleted in a test,orderByandsortImageBlocksthrow on trunk and pass on this branch.Testing Instructions for Keyboard
No UI changes.
Screenshots or screencast
N/A. There are no visual changes.
Use of AI Tools
This PR was authored with Claude Code (Claude Opus 5.5). It was used to locate the call sites, apply the mechanical replacement, write the ESLint rule, and run the lint and unit tests above. I reviewed the diff and the test results.
🤖 Generated with Claude Code