Skip to content

Port optional catch clause variable declaration transform #1273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2927,8 +2927,12 @@ func (node *CatchClause) Clone(f NodeFactoryCoercible) *Node {
}

func (node *CatchClause) computeSubtreeFacts() SubtreeFacts {
return propagateSubtreeFacts(node.VariableDeclaration) |
res := propagateSubtreeFacts(node.VariableDeclaration) |
propagateSubtreeFacts(node.Block)
if node.VariableDeclaration == nil {
res |= SubtreeContainsES2019
}
return res
}

func (node *CatchClause) propagateSubtreeFacts() SubtreeFacts {
Expand Down
20 changes: 19 additions & 1 deletion internal/transformers/estransforms/optionalcatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,25 @@ type optionalCatchTransformer struct {
}

func (ch *optionalCatchTransformer) visit(node *ast.Node) *ast.Node {
return node // !!!
if node.SubtreeFacts()&ast.SubtreeContainsES2019 == 0 {
return node
}
switch node.Kind {
case ast.KindCatchClause:
return ch.visitCatchClause(node.AsCatchClause())
default:
return ch.Visitor().VisitEachChild(node)
}
}

func (ch *optionalCatchTransformer) visitCatchClause(node *ast.CatchClause) *ast.Node {
if node.VariableDeclaration == nil {
return ch.Factory().NewCatchClause(
ch.Factory().NewVariableDeclaration(ch.Factory().NewTempVariable(), nil, nil, nil),
ch.Visitor().Visit(node.Block),
)
}
return ch.Visitor().VisitEachChild(node.AsNode())
}

func newOptionalCatchTransformer(emitContext *printer.EmitContext) *transformers.Transformer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ class BasicFeatures {
var xx = c;
retVal += ;
try { }
catch { }
catch (_a) { }
Property;
retVal += c.Member();
retVal += xx.Foo() ? 0 : 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,7 @@
if(retValue) { }
}
TypeScriptAllInOne.Program = Program;
@@= skipped -130, +131 lines =@@
var xx = c;
retVal += ;
try { }
- catch (_a) { }
+ catch { }
Property;
retVal += c.Member();
retVal += xx.Foo() ? 0 : 1;
@@= skipped -48, +48 lines =@@
@@= skipped -178, +179 lines =@@
}
}
class CLASS {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ try {
var [d = 1] = [];
var { e = 1 } = {};
}
catch {
catch (_a) {
console.error("error");
}
a;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,4 @@
-"use strict";
try {
var a = f1();
var [b] = f2();
@@= skipped -8, +7 lines =@@
var [d = 1] = [];
var { e = 1 } = {};
}
-catch (_a) {
+catch {
console.error("error");
}
a;
var [b] = f2();
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ try {
foo = 1234;
}
}
catch {
catch (_a) {
foo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,4 @@
-"use strict";
let foo;
try {
if (Math.random() > 0.5) {
foo = 1234;
}
}
-catch (_a) {
+catch {
foo;
}
if (Math.random() > 0.5) {
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ const main = () => {
hoge = 'hoge!';
return;
}
catch {
catch (_a) {
return;
}
finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,4 @@
-"use strict";
// Repro from #34797
function f1() {
let a = null;
@@= skipped -220, +219 lines =@@
hoge = 'hoge!';
return;
}
- catch (_a) {
+ catch {
return;
}
finally {
let a = null;
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ async function fn39(x) {
async function fn40(x) {
try {
}
catch {
catch (_a) {
var x;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@
+async function fn40(x) {
+ try {
+ }
+ catch {
+ catch (_a) {
+ var x;
+ }
}
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ try {
await result_4;
}
}
catch {
catch (_a) {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
const d24 = __addDisposableResource(env_6, { async [Symbol.asyncDispose]() { } }, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,8 @@
}
catch (e_4) {
env_5.error = e_4;
@@= skipped -14, +12 lines =@@
await result_4;
}
}
- catch (_a) {
+ catch {
@@= skipped -17, +15 lines =@@
catch (_a) {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
- const d24 = __addDisposableResource(env_6, { [Symbol.asyncDispose]() {
Expand All @@ -307,7 +303,7 @@
}
catch (e_5) {
env_6.error = e_5;
@@= skipped -20, +18 lines =@@
@@= skipped -17, +15 lines =@@
finally {
const env_7 = { stack: [], error: void 0, hasError: false };
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ try {
await result_4;
}
}
catch {
catch (_a) {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
const d24 = __addDisposableResource(env_6, { async [Symbol.asyncDispose]() { } }, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,4 @@
+ }
}
};
{
@@= skipped -89, +87 lines =@@
await result_4;
}
}
- catch (_a) {
+ catch {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
const d24 = __addDisposableResource(env_6, { async [Symbol.asyncDispose]() { } }, true);
{
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ try {
await result_4;
}
}
catch {
catch (_a) {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
const d24 = __addDisposableResource(env_6, { async [Symbol.asyncDispose]() { } }, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,8 @@
}
catch (e_4) {
env_5.error = e_4;
@@= skipped -14, +12 lines =@@
await result_4;
}
}
- catch (_a) {
+ catch {
@@= skipped -17, +15 lines =@@
catch (_a) {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
- const d24 = __addDisposableResource(env_6, { [Symbol.asyncDispose]() {
Expand All @@ -307,7 +303,7 @@
}
catch (e_5) {
env_6.error = e_5;
@@= skipped -20, +18 lines =@@
@@= skipped -17, +15 lines =@@
finally {
const env_7 = { stack: [], error: void 0, hasError: false };
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ function fn() {
//// [tryStatements.js]
function fn() {
try { }
catch { }
catch (_a) { }
try { }
catch {
catch (_b) {
try { }
catch {
catch (_c) {
try { }
catch { }
catch (_d) { }
}
try { }
catch { }
catch (_e) { }
}
try { }
catch (x) {
Expand All @@ -41,7 +41,7 @@ function fn() {
try { }
finally { }
try { }
catch { }
catch (_f) { }
finally { }
try { }
catch (z) { }
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ try {
__disposeResources(env_5);
}
}
catch {
catch (_a) {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@
}
}
- catch (_b) {
+ catch {
+ catch (_a) {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ try {
__disposeResources(env_5);
}
}
catch {
catch (_a) {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
}
}
- catch (_b) {
+ catch {
+ catch (_a) {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ try {
__disposeResources(env_5);
}
}
catch {
catch (_a) {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@
}
}
- catch (_b) {
+ catch {
+ catch (_a) {
const env_6 = { stack: [], error: void 0, hasError: false };
try {
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);