Skip to content

Bugfix transform needed for optional chain expressions with spread arguments #13001

@jridgewell

Description

@jridgewell

Bug Report

  • I would like to work on a fix!

Current behavior

Re: https://stackoverflow.com/questions/66609657/optional-chaining-function-prototype-apply-was-called-on-undefined-which-is-a

There's a bug in Chrome when invoking a bare optional call (not a method on an object) when there is a spread arguments and a following argument:

Input Code
REPL (Chome throws, Firefox/Safari are fine)

const fn = null;
const n = null;
const o = {};

// Fine
fn?.(...[]);
o?.toString(...[], 1);
o?.toString?.(...[], 1);
n?.method?.(...[], 1);

// Throws (must be any argument that follows a ...spread)
fn?.(...[], 1); 
fn?.(...[], ...[]);
o.method?.(...[], 1);
// This one is most surprising!
n?.method(...[], 1);

Expected behavior

None of these should throw in Chrome

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": "Chrome > 80",
        "bugfixes": true
      }
    ]
  ]
}

Possible Solution

We need to add a new bugfix transform that detects these ...foo spread arguments inside an optional chain, and forces the transform.

Additional context

Edits by @JLHwung

  • Added link to v8 issue for reference.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions