Skip to content

Commit fa5d50b

Browse files
authored
Port optional catch clause variable declaration transform (#1273)
1 parent 4caf653 commit fa5d50b

26 files changed

+56
-118
lines changed

internal/ast/ast.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2927,8 +2927,12 @@ func (node *CatchClause) Clone(f NodeFactoryCoercible) *Node {
29272927
}
29282928

29292929
func (node *CatchClause) computeSubtreeFacts() SubtreeFacts {
2930-
return propagateSubtreeFacts(node.VariableDeclaration) |
2930+
res := propagateSubtreeFacts(node.VariableDeclaration) |
29312931
propagateSubtreeFacts(node.Block)
2932+
if node.VariableDeclaration == nil {
2933+
res |= SubtreeContainsES2019
2934+
}
2935+
return res
29322936
}
29332937

29342938
func (node *CatchClause) propagateSubtreeFacts() SubtreeFacts {

internal/transformers/estransforms/optionalcatch.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,25 @@ type optionalCatchTransformer struct {
1111
}
1212

1313
func (ch *optionalCatchTransformer) visit(node *ast.Node) *ast.Node {
14-
return node // !!!
14+
if node.SubtreeFacts()&ast.SubtreeContainsES2019 == 0 {
15+
return node
16+
}
17+
switch node.Kind {
18+
case ast.KindCatchClause:
19+
return ch.visitCatchClause(node.AsCatchClause())
20+
default:
21+
return ch.Visitor().VisitEachChild(node)
22+
}
23+
}
24+
25+
func (ch *optionalCatchTransformer) visitCatchClause(node *ast.CatchClause) *ast.Node {
26+
if node.VariableDeclaration == nil {
27+
return ch.Factory().NewCatchClause(
28+
ch.Factory().NewVariableDeclaration(ch.Factory().NewTempVariable(), nil, nil, nil),
29+
ch.Visitor().Visit(node.Block),
30+
)
31+
}
32+
return ch.Visitor().VisitEachChild(node.AsNode())
1533
}
1634

1735
func newOptionalCatchTransformer(emitContext *printer.EmitContext) *transformers.Transformer {

testdata/baselines/reference/submodule/compiler/constructorWithIncompleteTypeAnnotation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ class BasicFeatures {
424424
var xx = c;
425425
retVal += ;
426426
try { }
427-
catch { }
427+
catch (_a) { }
428428
Property;
429429
retVal += c.Member();
430430
retVal += xx.Foo() ? 0 : 1;

testdata/baselines/reference/submodule/compiler/constructorWithIncompleteTypeAnnotation.js.diff

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,7 @@
3030
if(retValue) { }
3131
}
3232
TypeScriptAllInOne.Program = Program;
33-
@@= skipped -130, +131 lines =@@
34-
var xx = c;
35-
retVal += ;
36-
try { }
37-
- catch (_a) { }
38-
+ catch { }
39-
Property;
40-
retVal += c.Member();
41-
retVal += xx.Foo() ? 0 : 1;
42-
@@= skipped -48, +48 lines =@@
33+
@@= skipped -178, +179 lines =@@
4334
}
4435
}
4536
class CLASS {

testdata/baselines/reference/submodule/compiler/controlFlowDestructuringVariablesInTryCatch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ try {
3131
var [d = 1] = [];
3232
var { e = 1 } = {};
3333
}
34-
catch {
34+
catch (_a) {
3535
console.error("error");
3636
}
3737
a;

testdata/baselines/reference/submodule/compiler/controlFlowDestructuringVariablesInTryCatch.js.diff

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,4 @@
77
-"use strict";
88
try {
99
var a = f1();
10-
var [b] = f2();
11-
@@= skipped -8, +7 lines =@@
12-
var [d = 1] = [];
13-
var { e = 1 } = {};
14-
}
15-
-catch (_a) {
16-
+catch {
17-
console.error("error");
18-
}
19-
a;
10+
var [b] = f2();

testdata/baselines/reference/submodule/compiler/potentiallyUnassignedVariableInCatch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ try {
1818
foo = 1234;
1919
}
2020
}
21-
catch {
21+
catch (_a) {
2222
foo;
2323
}

testdata/baselines/reference/submodule/compiler/potentiallyUnassignedVariableInCatch.js.diff

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,4 @@
77
-"use strict";
88
let foo;
99
try {
10-
if (Math.random() > 0.5) {
11-
foo = 1234;
12-
}
13-
}
14-
-catch (_a) {
15-
+catch {
16-
foo;
17-
}
10+
if (Math.random() > 0.5) {

testdata/baselines/reference/submodule/compiler/tryCatchFinallyControlFlow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ const main = () => {
551551
hoge = 'hoge!';
552552
return;
553553
}
554-
catch {
554+
catch (_a) {
555555
return;
556556
}
557557
finally {

testdata/baselines/reference/submodule/compiler/tryCatchFinallyControlFlow.js.diff

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,4 @@
77
-"use strict";
88
// Repro from #34797
99
function f1() {
10-
let a = null;
11-
@@= skipped -220, +219 lines =@@
12-
hoge = 'hoge!';
13-
return;
14-
}
15-
- catch (_a) {
16-
+ catch {
17-
return;
18-
}
19-
finally {
10+
let a = null;

0 commit comments

Comments
 (0)