Skip to content

fix: keep destructuring catch parameters working with regenerator 7.29.17 - #197

Merged
zombieJ merged 2 commits into
react-component:mainfrom
yoyo837:fix/destructuring-catch-param
Dec 3, 2026
Merged

zombieJ merged 2 commits into
react-component:mainfrom
yoyo837:fix/destructuring-catch-param

Conversation

@yoyo837

@yoyo837 yoyo837 commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Problem

A test that destructures a catch parameter inside an async function or an async generator throws ReferenceError: _ref is not defined at runtime, even though the code is valid and the transform reports no error:

const run = async () => {
  try {
    await fail();
  } catch ({ errors }) {
    return errors;
  }
};
// ReferenceError: _ref4 is not defined

The generated output writes an identifier that was never declared:

case 2:
  _context.p = 2;
  _ref4 = _context.v;        // ← undeclared
  errors = _ref4.errors;
  return _context.a(2, errors);

Root cause

Two plugins in the current stack disagree about scope bookkeeping.

  1. babel-preset-umi@1.8.x pins @babel/plugin-transform-destructuring@7.4.4 (nested dependency, so it cannot simply be deduped). Its CatchClause visitor replaces the pattern parameter with a generated identifier via direct node mutation:

    node.param = scope.generateUidIdentifier('ref');

    ...and never registers a binding for it. scope.crawl() was only added upstream in 7.18.0.

  2. @babel/plugin-transform-regenerator@7.29.17 (which added destructuring-catch support) renames the catch parameter binding while lowering the handler:

    handlerScope.rename(name, self.makeTempVar().name);

    Scope#rename is a silent no-op when handlerScope.getBinding(name) is undefined. The temporary is therefore declared correctly (var errors, _t;) but the body keeps referring to the unregistered _ref.

Version matrix, same babel.transformSync call, @babel/core@7.29.17 + regenerator@7.29.17:

plugin-transform-destructuring emitted identifier declared result
7.4.4 _ref _t ❌ ReferenceError
7.16.0 _ref _t ❌
7.17.7 _ref _t ❌
7.18.0 _t _t ✅
7.20.0 _t _t ✅
7.29.17 _t _t ✅

regenerator 7.29.16 and every regenerator 8.x from before the change are unaffected; @babel/core 7.29.16 vs 7.29.17 makes no difference. Reported upstream as babel/babel#18828 with a standalone reproducer.

Fix

src/transformers/registerCatchParamBinding.ts — a tiny plugin that runs on CatchClause#exit (after transform-destructuring has rewritten the parameter, before transform-regenerator visits the enclosing function) and refreshes the catch scope when the parameter has no binding:

if (!path.scope.getBinding(param.name)) {
  path.scope.crawl();
}

registerCatchParamBinding is placed first in the plugin list in jsTransformer.ts. Because it guards on a missing binding, it is inert once the dependency stack is healthy.

Verification

  • New tests/destructuring-catch.test.tsx covers both async function and async generator; it fails with ReferenceError before the change and passes after.
  • No-op check: with destructuring@7.18.0 (a stack where the bug cannot occur), the workaround changes 0 of 5 compared outputs — ordinary catch (e), parameterless catch {}, destructuring, nested catches, and a shadowed const errors inside the handler are byte-identical with and without the plugin.
  • Downstream check: react-component/async-validator went from promise.spec.ts failing with _ref2 is not defined to 116/116 tests passing using this patched rc-test.

Cleanup

This plugin can be deleted once babel-preset-umi ships a @babel/plugin-transform-destructuring >= 7.18.0 (the version that registers the binding itself). The regression test should stay either way.

Summary by CodeRabbit

  • Bug 修复
    • 修复异步函数和异步生成器中解构 catch 参数时可能出现的问题,确保捕获的错误字段及嵌套属性能够正常使用。

babel-preset-umi@1.8.x pins @babel/plugin-transform-destructuring@7.4.4,
whose CatchClause visitor replaces catch ({ errors }) with a generated
_ref by direct node mutation and never registers a scope binding.
@babel/plugin-transform-regenerator 7.29.17+ renames catch-parameter
bindings, and Scope#rename is a silent no-op when the binding is
missing, so the emitted code writes an undeclared _ref and throws
ReferenceError: _ref is not defined.

A tiny plugin now refreshes the catch clause scope when the parameter has
no binding, so the generator itself keeps working regardless of which
destructuring plugin lowered the pattern.

Upstream: babel/babel#18828
Copilot AI balanced review requested due to automatic review settings December 3, 2026 03:41
@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 71d17dd3-f46b-450d-a061-d94a497c44a8
Commits

Reviewing files that changed from the base of the PR and between b785316 and 95a9d04.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

新增 Babel 插件处理 catch 参数的作用域绑定,并将插件加入 JavaScript 转换器。新增测试覆盖异步函数和异步生成器中的解构 catch 参数。

Changes

Catch 参数绑定处理

Layer / File(s) Summary
注册并验证 catch 参数绑定处理
src/transformers/registerCatchParamBinding.ts, src/transformers/jsTransformer.ts, tests/destructuring-catch.test.tsx
新增插件,在 catch 参数为标识符且作用域中没有对应绑定时重新扫描作用域。转换器注册该插件。测试覆盖异步函数中的字段解构和异步生成器中的嵌套解构。

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · previous review b7853

This adds a small workaround so destructured catch parameters in async functions keep working with newer regenerator versions. It does nothing when the binding is already registered, and tests cover async functions and async generators. Merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix: keep destructuring catch parameters working with regenerator 7.29.17”与变更集直接相关。变更新增 registerCatchParamBinding 插件,修复异步函数和异步生成器中解构 catch 参数的 ReferenceError,并补充了回归测试。标题说明了修复对象和触发环境,描述具体,没有使用模糊用语。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

兔子翻开转换器,
看见 catch 参数待绑定。
插件轻轻扫描作用域,
异步函数解出 errors,
生成器 yield 出 code。
测试通过,兔子竖耳听。

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e01073d1-3032-49ca-933d-09538798a349
Commits

Reviewing files that changed from the base of the PR and between 00b9016 and b785316.

Files selected for processing: 3

  • src/transformers/jsTransformer.ts
  • src/transformers/registerCatchParamBinding.ts
  • tests/destructuring-catch.test.tsx

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the destructured catch-parameter fix.

No open issues 🎉

What changed in this PR

Fixes destructured catch parameters in the Jest transformer when older destructuring transforms interact with regenerator 7.29.17.

Changes:

  • Adds a Babel plugin that refreshes catch scopes when parameter bindings are missing.
  • Registers the workaround first in the transformer’s plugin list.
  • Adds regression tests for async functions and async generators.
File Description
tests/​destructuring-catch.test.tsx Tests destructured catch parameters in both async forms.
src/​transformers/​registerCatchParamBinding.ts Repairs missing catch-parameter bindings.
src/​transformers/​jsTransformer.ts Enables the workaround in the Jest transformer.

Review effort: Balanced

@zombieJ
zombieJ merged commit 3dd980d into react-component:main Dec 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants