You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: keep destructuring catch parameters working with regenerator 7.29.17 - #197
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:
construn=async()=>{try{awaitfail();}catch({ errors }){returnerrors;}};// ReferenceError: _ref4 is not defined
The generated output writes an identifier that was never declared:
Two plugins in the current stack disagree about scope bookkeeping.
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.
@babel/plugin-transform-regenerator@7.29.17 (which added destructuring-catch support) renames the catch parameter binding while lowering the handler:
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:
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.
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
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: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
标题“fix: keep destructuring catch parameters working with regenerator 7.29.17”与变更集直接相关。变更新增 registerCatchParamBinding 插件,修复异步函数和异步生成器中解构 catch 参数的 ReferenceError,并补充了回归测试。标题说明了修复对象和触发环境,描述具体,没有使用模糊用语。
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A test that destructures a
catchparameter inside anasyncfunction or anasyncgenerator throwsReferenceError: _ref is not definedat runtime, even though the code is valid and the transform reports no error:The generated output writes an identifier that was never declared:
Root cause
Two plugins in the current stack disagree about scope bookkeeping.
babel-preset-umi@1.8.xpins@babel/plugin-transform-destructuring@7.4.4(nested dependency, so it cannot simply be deduped). ItsCatchClausevisitor replaces the pattern parameter with a generated identifier via direct node mutation:...and never registers a binding for it.
scope.crawl()was only added upstream in 7.18.0.@babel/plugin-transform-regenerator@7.29.17(which added destructuring-catch support) renames the catch parameter binding while lowering the handler:Scope#renameis a silent no-op whenhandlerScope.getBinding(name)isundefined. The temporary is therefore declared correctly (var errors, _t;) but the body keeps referring to the unregistered_ref.Version matrix, same
babel.transformSynccall,@babel/core@7.29.17+regenerator@7.29.17:plugin-transform-destructuring_ref_t_ref_t_ref_t_t_t_t_t_t_tregenerator7.29.16 and everyregenerator8.x from before the change are unaffected;@babel/core7.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 onCatchClause#exit(aftertransform-destructuringhas rewritten the parameter, beforetransform-regeneratorvisits the enclosing function) and refreshes the catch scope when the parameter has no binding:registerCatchParamBindingis placed first in the plugin list injsTransformer.ts. Because it guards on a missing binding, it is inert once the dependency stack is healthy.Verification
tests/destructuring-catch.test.tsxcovers bothasync functionandasync generator; it fails withReferenceErrorbefore the change and passes after.destructuring@7.18.0(a stack where the bug cannot occur), the workaround changes 0 of 5 compared outputs — ordinarycatch (e), parameterlesscatch {}, destructuring, nested catches, and a shadowedconst errorsinside the handler are byte-identical with and without the plugin.react-component/async-validatorwent frompromise.spec.tsfailing with_ref2 is not definedto 116/116 tests passing using this patchedrc-test.Cleanup
This plugin can be deleted once
babel-preset-umiships 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
catch参数时可能出现的问题,确保捕获的错误字段及嵌套属性能够正常使用。