Skip to content

fix: handle empty array pattern in safari-rest-destructuring-rhs-array bugfix plugin - #18206

Closed
contactjawad wants to merge 1 commit into
babel:mainfrom
contactjawad:fix-safari-rest-empty-array-pattern
Closed

contactjawad wants to merge 1 commit into
babel:mainfrom
contactjawad:fix-safari-rest-empty-array-pattern

Conversation

@contactjawad

Copy link
Copy Markdown

What

shouldTransform in @babel/plugin-bugfix-safari-rest-destructuring-rhs-array no longer crashes on an empty array pattern (const [] = arr, [] = arr, nested const [[]] = [[]]). It now correctly returns false for them.

Why

The ArrayPattern visitor calls shouldTransform for every ArrayPattern, including empty ones. shouldTransform did:

const elementPaths = path.get("elements");
const elementPathsLength = elementPaths.length;
if (elementPaths[elementPathsLength - 1].isRestElement()) {

For an empty pattern elementPathsLength is 0, so elementPaths[-1] is undefined and .isRestElement() throws TypeError: Cannot read properties of undefined (reading 'isRestElement'), aborting compilation of otherwise-valid code. Empty array patterns are legal JavaScript and have no trailing rest element.

How

Guard the empty case before the index access:

if (elementPathsLength === 0) {
  return false;
}

Test

Added const [] = []; and [] = []; to negativeCases, and nested const [[]] = [[]]; to negativeNestedCases, in test/util.skip-bundled.js — all expect shouldTransform(...) === false. These throw a TypeError before the change and pass after.

…empty array pattern

shouldTransform read elementPaths[elementPathsLength - 1] and called
.isRestElement() on it without guarding an empty ArrayPattern. For `const [] = arr`
(or nested `const [[]] = [[]]`) the length is 0, so the index is -1 and the
access yields undefined, throwing `TypeError: Cannot read properties of
undefined (reading 'isRestElement')` and crashing the transform on valid input.
An empty pattern has no rest element, so return false early.
@github-actions

Copy link
Copy Markdown

❌ Automation signals

Activity patterns show signs of automation.

View full analysis →

This is an automated analysis by AgentScan

@babel-bot

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/62037

@pkg-pr-new

pkg-pr-new Bot commented Aug 25, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: fb98267

JLHwung
JLHwung previously approved these changes Aug 25, 2026
@JLHwung
JLHwung dismissed their stale review August 25, 2026 12:24

AI policy.

@nicolo-ribaudo

Copy link
Copy Markdown
Member

This looks 100% LLM-generated, including the PR description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants