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;

testdata/baselines/reference/submodule/conformance/asyncWithVarShadowing_es6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ async function fn39(x) {
394394
async function fn40(x) {
395395
try {
396396
}
397-
catch {
397+
catch (_a) {
398398
var x;
399399
}
400400
}

testdata/baselines/reference/submodule/conformance/asyncWithVarShadowing_es6.js.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@
444444
+async function fn40(x) {
445445
+ try {
446446
+ }
447-
+ catch {
447+
+ catch (_a) {
448448
+ var x;
449449
+ }
450450
}

testdata/baselines/reference/submodule/conformance/awaitUsingDeclarations.1(target=es2015).js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ try {
323323
await result_4;
324324
}
325325
}
326-
catch {
326+
catch (_a) {
327327
const env_6 = { stack: [], error: void 0, hasError: false };
328328
try {
329329
const d24 = __addDisposableResource(env_6, { async [Symbol.asyncDispose]() { } }, true);

testdata/baselines/reference/submodule/conformance/awaitUsingDeclarations.1(target=es2015).js.diff

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,8 @@
292292
}
293293
catch (e_4) {
294294
env_5.error = e_4;
295-
@@= skipped -14, +12 lines =@@
296-
await result_4;
297-
}
298-
}
299-
- catch (_a) {
300-
+ catch {
295+
@@= skipped -17, +15 lines =@@
296+
catch (_a) {
301297
const env_6 = { stack: [], error: void 0, hasError: false };
302298
try {
303299
- const d24 = __addDisposableResource(env_6, { [Symbol.asyncDispose]() {
@@ -307,7 +303,7 @@
307303
}
308304
catch (e_5) {
309305
env_6.error = e_5;
310-
@@= skipped -20, +18 lines =@@
306+
@@= skipped -17, +15 lines =@@
311307
finally {
312308
const env_7 = { stack: [], error: void 0, hasError: false };
313309
try {

testdata/baselines/reference/submodule/conformance/awaitUsingDeclarations.1(target=es2017).js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ try {
323323
await result_4;
324324
}
325325
}
326-
catch {
326+
catch (_a) {
327327
const env_6 = { stack: [], error: void 0, hasError: false };
328328
try {
329329
const d24 = __addDisposableResource(env_6, { async [Symbol.asyncDispose]() { } }, true);

testdata/baselines/reference/submodule/conformance/awaitUsingDeclarations.1(target=es2017).js.diff

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,4 @@
143143
+ }
144144
}
145145
};
146-
{
147-
@@= skipped -89, +87 lines =@@
148-
await result_4;
149-
}
150-
}
151-
- catch (_a) {
152-
+ catch {
153-
const env_6 = { stack: [], error: void 0, hasError: false };
154-
try {
155-
const d24 = __addDisposableResource(env_6, { async [Symbol.asyncDispose]() { } }, true);
146+
{

testdata/baselines/reference/submodule/conformance/awaitUsingDeclarations.1(target=es5).js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ try {
323323
await result_4;
324324
}
325325
}
326-
catch {
326+
catch (_a) {
327327
const env_6 = { stack: [], error: void 0, hasError: false };
328328
try {
329329
const d24 = __addDisposableResource(env_6, { async [Symbol.asyncDispose]() { } }, true);

testdata/baselines/reference/submodule/conformance/awaitUsingDeclarations.1(target=es5).js.diff

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,8 @@
292292
}
293293
catch (e_4) {
294294
env_5.error = e_4;
295-
@@= skipped -14, +12 lines =@@
296-
await result_4;
297-
}
298-
}
299-
- catch (_a) {
300-
+ catch {
295+
@@= skipped -17, +15 lines =@@
296+
catch (_a) {
301297
const env_6 = { stack: [], error: void 0, hasError: false };
302298
try {
303299
- const d24 = __addDisposableResource(env_6, { [Symbol.asyncDispose]() {
@@ -307,7 +303,7 @@
307303
}
308304
catch (e_5) {
309305
env_6.error = e_5;
310-
@@= skipped -20, +18 lines =@@
306+
@@= skipped -17, +15 lines =@@
311307
finally {
312308
const env_7 = { stack: [], error: void 0, hasError: false };
313309
try {

testdata/baselines/reference/submodule/conformance/tryStatements.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ function fn() {
2323
//// [tryStatements.js]
2424
function fn() {
2525
try { }
26-
catch { }
26+
catch (_a) { }
2727
try { }
28-
catch {
28+
catch (_b) {
2929
try { }
30-
catch {
30+
catch (_c) {
3131
try { }
32-
catch { }
32+
catch (_d) { }
3333
}
3434
try { }
35-
catch { }
35+
catch (_e) { }
3636
}
3737
try { }
3838
catch (x) {
@@ -41,7 +41,7 @@ function fn() {
4141
try { }
4242
finally { }
4343
try { }
44-
catch { }
44+
catch (_f) { }
4545
finally { }
4646
try { }
4747
catch (z) { }

testdata/baselines/reference/submodule/conformance/tryStatements.js.diff

Lines changed: 0 additions & 33 deletions
This file was deleted.

testdata/baselines/reference/submodule/conformance/usingDeclarations.1(target=es2015).js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ try {
510510
__disposeResources(env_5);
511511
}
512512
}
513-
catch {
513+
catch (_a) {
514514
const env_6 = { stack: [], error: void 0, hasError: false };
515515
try {
516516
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);

testdata/baselines/reference/submodule/conformance/usingDeclarations.1(target=es2015).js.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@
380380
}
381381
}
382382
- catch (_b) {
383-
+ catch {
383+
+ catch (_a) {
384384
const env_6 = { stack: [], error: void 0, hasError: false };
385385
try {
386386
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);

testdata/baselines/reference/submodule/conformance/usingDeclarations.1(target=es2017).js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ try {
510510
__disposeResources(env_5);
511511
}
512512
}
513-
catch {
513+
catch (_a) {
514514
const env_6 = { stack: [], error: void 0, hasError: false };
515515
try {
516516
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);

testdata/baselines/reference/submodule/conformance/usingDeclarations.1(target=es2017).js.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@
327327
}
328328
}
329329
- catch (_b) {
330-
+ catch {
330+
+ catch (_a) {
331331
const env_6 = { stack: [], error: void 0, hasError: false };
332332
try {
333333
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);

testdata/baselines/reference/submodule/conformance/usingDeclarations.1(target=es5).js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ try {
510510
__disposeResources(env_5);
511511
}
512512
}
513-
catch {
513+
catch (_a) {
514514
const env_6 = { stack: [], error: void 0, hasError: false };
515515
try {
516516
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);

testdata/baselines/reference/submodule/conformance/usingDeclarations.1(target=es5).js.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@
380380
}
381381
}
382382
- catch (_b) {
383-
+ catch {
383+
+ catch (_a) {
384384
const env_6 = { stack: [], error: void 0, hasError: false };
385385
try {
386386
const d24 = __addDisposableResource(env_6, { [Symbol.dispose]() { } }, false);

0 commit comments

Comments
 (0)