GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||
GeezerVison is showing github.com — fix(regenerator): support destructuring catch parameters by JLHwung · Pull Request #18732 · babel/babel Conversation
|
| Q | A |
|---|---|
| Fixed Issues? | regenerator transform drops the catch param pattern (REPL) |
| Patch: Bug Fix? | Yes |
| Major: Breaking Change? | |
| Minor: New Feature? | |
| Tests Added + Pass? | Yes |
| Documentation PR Link | |
| Any Dependency Changes? | |
| Need backport? | No |
| License | MIT |
When a try statement containing a yield had a pattern as its catch parameter, the pattern was dropped: the exception was assigned to a temporary variable and the bindings of the pattern were never created, so their references silently resolved to outer variables.
Given the input
function *f() { try { throw [1]; yield 0; } catch ([e]) { console.log(e); } } f().next(0);
The output throws e is not defined:
var _marked = /*#__PURE__*/_regenerator().m(f); function f() { var _t; return _regenerator().w(function (_context) { while (1) switch (_context.p = _context.n) { case 0: _context.p = 0; throw [1]; case 1: _context.n = 3; break; case 2: _context.p = 2; _t = _context.v; console.log(e); case 3: return _context.a(2); } }, _marked, null, [[0, 2]]); } f().next(0);
In this PR we pass through catch patterns when possible, unless there is yield or await directly nested in the catch param, which is very rare. In those case we will give up the pass-through and delegate to the @babel/plugin-transform-destructuring transform to handle.
Update: currently merging is blocked by #18617.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63965 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
commit: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
There is a regression here.
function* g() { var x = 10; try { throw 2; } catch (x) { var x = 3; yield x; } yield x; } console.log([...g()]);
If only regenerator is enabled, its output will fail silently.
function* g() { for (var i = 1; i <= 2; i++) { try { throw { x: i }; } catch ({ x }) { yield () => x; } } } const [first, second] = [...g()]; console.log(first(), second());
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Thank you for the report, this regression has been fixed on #18617, please review that PR. I can not reproduce this issue when rebasing this PR on top of #18617. The main branch is not affected merely because the previous For the time being I will mark this PR as do-not-merge until #18617 is landed. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
f356e11 to
d693fca
Compare
…ters
When a `try` statement containing a yield had a pattern as its `catch`
parameter, the pattern was dropped: the exception was assigned to a
temporary variable and the bindings of the pattern were never created,
so their references silently resolved to outer variables.
The bindings of the catch parameter, an identifier or a pattern, are
now renamed to temporary variables with `scope.rename`, and the
parameter is assigned from the caught exception at the start of the
catch block. A `yield` or `await` inside the pattern still throws and
points to the destructuring transform.
An optional catch binding (`catch {}`) no longer crashes either.
The `this`/`arguments` detection now visits the computed keys of
nested methods and classes, which run with the generator's receiver,
and leaves alone `arguments` references to a binding inside the
generator, such as `catch (arguments)`.
Assisted-by: Claude Opus 5.5
d693fca to
b0a9799
Compare
|
#18617 has been merged, this PR should be ready for review. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
These examples seem somewhat extreme, so they do not block this PR.
function* g(obj = { [arguments.length]() {} }) { yield Object.keys(obj); } console.log(g().next().value); // main → ["0"] // PR → ReferenceError: _args is not defined
function* g() { for (let arguments = 1; arguments < 3; arguments++) { yield arguments; } } console.log([...g()]); // main → [1, 2] // PR → [1]
function* g() { try { yield 1; } catch ([__proto__]) { yield { __proto__, __proto__: null }; } } console.log(g().next().value); // main → 1 // PR → SyntaxError: Duplicate __proto__ fields are not allowed in object literals
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@liuxingbaoyu Thank you for the report, they are fixed in #18763. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.