Skip to content

fix: reject untranspiled ??, &&=, ||=, ??= before yield expression - #18679

Merged
JLHwung merged 2 commits into
mainfrom
jlhwung/fix-regenerator-logical-assignment
Nov 24, 2026
Merged

JLHwung merged 2 commits into
mainfrom
jlhwung/fix-regenerator-logical-assignment

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? The regenerator transform result of a ||= yield b always evaluate b
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? No
License MIT

Currently, the regenerator on main transforms

function* f() { a ||= yield b; }

to

function f() {
  var _t;
  return _regenerator().w(function (_context) {
    while (1) switch (_context.n) {
      case 0:
        _t = a;              // read the target
        _context.n = 1;
        return b;            // evaluate b and yield it, unconditionally
      case 1:
        a = _t ||= _context.v;   // operator applied only after resuming
      case 2:
        return _context.a(2);
    }
  }, _marked);
}

where b is always evaluated and the ||= is applied after the generator resumes. The same issue also happens for nullish coalescing as well.

Since the generator feature predates logical assignment and nullish coalescing by a couple of years, I think it is fair that the regenerator transform does not support lowering these features. In this PR we throw a compilation error if the regenerator transform is enabled but plugins for those features are not. This is done in the second commit.

An alternative solution is to piggyback the lowering of these two features here, which I don't think is very sustainable since in the long term we will have to cherry pick more features here.

We also fix an AST node reuse issue during the review session, it is fixed in the first commit.

@JLHwung
JLHwung added this pull request to stack #18587 November 20, 2026 21:42
@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories 7.x: needs backport labels Nov 20, 2026
@babel-bot

babel-bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: dd6dcfb

@JLHwung
JLHwung force-pushed the jlhwung/fix-regenerator-logical-assignment branch from 6604c2c to 358c0fa Compare November 23, 2026 13:59
@JLHwung
JLHwung force-pushed the jlhwung/fix-regenerator-logical-assignment branch 6 times, most recently from b9fc44d to d7bf5d1 Compare November 23, 2026 19:35
Base automatically changed from jlhwung/fix-proto-to-assign-non-setters to main November 24, 2026 13:07
…||` and `&&`

When the value of `(yield a) || b` or `(yield a) && b` is used, the
temporary variable holding the left operand was used both in the jump
test and in the result, reusing the same AST node. It is now cloned.

Assisted-by: Claude Opus 5.5
…??` with `yield`

`a ||= yield b` was compiled like an arithmetic compound assignment:
the right-hand side was always evaluated and the target always
assigned. `a ?? (yield b)` failed with an assertion error.

Both now throw, asking for @babel/plugin-transform-logical-assignment-operators
or @babel/plugin-transform-nullish-coalescing-operator. Those plugins
lower the expressions before the regenerator transform reaches them,
whatever the plugin order, and every target without generators needs
them anyway.

Assisted-by: Claude Fable 5.2
Assisted-by: Claude Opus 5.5
@JLHwung
JLHwung force-pushed the jlhwung/fix-regenerator-logical-assignment branch from d7bf5d1 to dd6dcfb Compare November 24, 2026 13:07
@JLHwung
JLHwung added this pull request to the merge queue Nov 24, 2026
Merged via the queue into main with commit 0089b70 Nov 24, 2026
41 checks passed
@JLHwung
JLHwung deleted the jlhwung/fix-regenerator-logical-assignment branch November 24, 2026 16:01
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18711.

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

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants