Skip to content

Commit 75c8ecb

Browse files
authored
Merge pull request microsoft#17517 from tinganho/IgnoredCatchParameter
Ignored catch parameter
2 parents d262567 + 3da1a53 commit 75c8ecb

21 files changed

Lines changed: 202 additions & 115 deletions

src/compiler/binder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2921,7 +2921,10 @@ namespace ts {
29212921
function computeCatchClause(node: CatchClause, subtreeFlags: TransformFlags) {
29222922
let transformFlags = subtreeFlags;
29232923

2924-
if (node.variableDeclaration && isBindingPattern(node.variableDeclaration.name)) {
2924+
if (!node.variableDeclaration) {
2925+
transformFlags |= TransformFlags.AssertESNext;
2926+
}
2927+
else if (isBindingPattern(node.variableDeclaration.name)) {
29252928
transformFlags |= TransformFlags.AssertES2015;
29262929
}
29272930

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ namespace ts {
481481
Type,
482482
ResolvedBaseConstructorType,
483483
DeclaredType,
484-
ResolvedReturnType
484+
ResolvedReturnType,
485485
}
486486

487487
const enum CheckMode {

src/compiler/emitter.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,10 +2133,12 @@ namespace ts {
21332133
function emitCatchClause(node: CatchClause) {
21342134
const openParenPos = writeToken(SyntaxKind.CatchKeyword, node.pos);
21352135
write(" ");
2136-
writeToken(SyntaxKind.OpenParenToken, openParenPos);
2137-
emit(node.variableDeclaration);
2138-
writeToken(SyntaxKind.CloseParenToken, node.variableDeclaration ? node.variableDeclaration.end : openParenPos);
2139-
write(" ");
2136+
if (node.variableDeclaration) {
2137+
writeToken(SyntaxKind.OpenParenToken, openParenPos);
2138+
emit(node.variableDeclaration);
2139+
writeToken(SyntaxKind.CloseParenToken, node.variableDeclaration.end);
2140+
write(" ");
2141+
}
21402142
emit(node.block);
21412143
}
21422144

src/compiler/factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,14 +2128,14 @@ namespace ts {
21282128
: node;
21292129
}
21302130

2131-
export function createCatchClause(variableDeclaration: string | VariableDeclaration, block: Block) {
2131+
export function createCatchClause(variableDeclaration: string | VariableDeclaration | undefined, block: Block) {
21322132
const node = <CatchClause>createSynthesizedNode(SyntaxKind.CatchClause);
21332133
node.variableDeclaration = typeof variableDeclaration === "string" ? createVariableDeclaration(variableDeclaration) : variableDeclaration;
21342134
node.block = block;
21352135
return node;
21362136
}
21372137

2138-
export function updateCatchClause(node: CatchClause, variableDeclaration: VariableDeclaration, block: Block) {
2138+
export function updateCatchClause(node: CatchClause, variableDeclaration: VariableDeclaration | undefined, block: Block) {
21392139
return node.variableDeclaration !== variableDeclaration
21402140
|| node.block !== block
21412141
? updateNode(createCatchClause(variableDeclaration, block), node)

src/compiler/parser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4799,11 +4799,16 @@ namespace ts {
47994799
function parseCatchClause(): CatchClause {
48004800
const result = <CatchClause>createNode(SyntaxKind.CatchClause);
48014801
parseExpected(SyntaxKind.CatchKeyword);
4802-
if (parseExpected(SyntaxKind.OpenParenToken)) {
4802+
4803+
if (parseOptional(SyntaxKind.OpenParenToken)) {
48034804
result.variableDeclaration = parseVariableDeclaration();
4805+
parseExpected(SyntaxKind.CloseParenToken);
4806+
}
4807+
else {
4808+
// Keep shape of node to avoid degrading performance.
4809+
result.variableDeclaration = undefined;
48044810
}
48054811

4806-
parseExpected(SyntaxKind.CloseParenToken);
48074812
result.block = parseBlock(/*ignoreMissingOpenBrace*/ false);
48084813
return finishNode(result);
48094814
}

src/compiler/transformers/es2015.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2491,7 +2491,7 @@ namespace ts {
24912491
const catchVariable = getGeneratedNameForNode(errorRecord);
24922492
const returnMethod = createTempVariable(/*recordTempVariable*/ undefined);
24932493
const values = createValuesHelper(context, expression, node.expression);
2494-
const next = createCall(createPropertyAccess(iterator, "next" ), /*typeArguments*/ undefined, []);
2494+
const next = createCall(createPropertyAccess(iterator, "next"), /*typeArguments*/ undefined, []);
24952495

24962496
hoistVariableDeclaration(errorRecord);
24972497
hoistVariableDeclaration(returnMethod);
@@ -3173,6 +3173,7 @@ namespace ts {
31733173
function visitCatchClause(node: CatchClause): CatchClause {
31743174
const ancestorFacts = enterSubtree(HierarchyFacts.BlockScopeExcludes, HierarchyFacts.BlockScopeIncludes);
31753175
let updated: CatchClause;
3176+
Debug.assert(!!node.variableDeclaration, "Catch clause variable should always be present when downleveling ES2015.");
31763177
if (isBindingPattern(node.variableDeclaration.name)) {
31773178
const temp = createTempVariable(/*recordTempVariable*/ undefined);
31783179
const newVariableDeclaration = createVariableDeclaration(temp);

src/compiler/transformers/esnext.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ namespace ts {
101101
return visitExpressionStatement(node as ExpressionStatement);
102102
case SyntaxKind.ParenthesizedExpression:
103103
return visitParenthesizedExpression(node as ParenthesizedExpression, noDestructuringValue);
104+
case SyntaxKind.CatchClause:
105+
return visitCatchClause(node as CatchClause);
104106
default:
105107
return visitEachChild(node, visitor, context);
106108
}
@@ -212,6 +214,17 @@ namespace ts {
212214
return visitEachChild(node, noDestructuringValue ? visitorNoDestructuringValue : visitor, context);
213215
}
214216

217+
function visitCatchClause(node: CatchClause): CatchClause {
218+
if (!node.variableDeclaration) {
219+
return updateCatchClause(
220+
node,
221+
createVariableDeclaration(createTempVariable(/*recordTempVariable*/ undefined)),
222+
visitNode(node.block, visitor, isBlock)
223+
);
224+
}
225+
return visitEachChild(node, visitor, context);
226+
}
227+
215228
/**
216229
* Visits a BinaryExpression that contains a destructuring assignment.
217230
*

src/compiler/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,7 +1808,7 @@ namespace ts {
18081808
export interface CatchClause extends Node {
18091809
kind: SyntaxKind.CatchClause;
18101810
parent?: TryStatement;
1811-
variableDeclaration: VariableDeclaration;
1811+
variableDeclaration?: VariableDeclaration;
18121812
block: Block;
18131813
}
18141814

@@ -4020,7 +4020,6 @@ namespace ts {
40204020
ContainsBindingPattern = 1 << 23,
40214021
ContainsYield = 1 << 24,
40224022
ContainsHoistedDeclarationOrCompletion = 1 << 25,
4023-
40244023
ContainsDynamicImport = 1 << 26,
40254024

40264025
// Please leave this as 1 << 29.

tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(138,13): error T
3434
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(141,32): error TS1005: '{' expected.
3535
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(143,13): error TS1005: 'try' expected.
3636
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(159,24): error TS1109: Expression expected.
37-
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(159,30): error TS1005: '(' expected.
37+
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(159,30): error TS1005: '{' expected.
3838
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(159,31): error TS2304: Cannot find name 'Property'.
3939
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(166,13): error TS2365: Operator '+=' cannot be applied to types 'number' and 'void'.
4040
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(180,40): error TS2447: The '^' operator is not allowed for boolean types. Consider using '!==' instead.
@@ -323,7 +323,7 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(261,1): error TS
323323
~~~~~
324324
!!! error TS1109: Expression expected.
325325
~
326-
!!! error TS1005: '(' expected.
326+
!!! error TS1005: '{' expected.
327327
~~~~~~~~
328328
!!! error TS2304: Cannot find name 'Property'.
329329
retVal += c.Member();

tests/baselines/reference/constructorWithIncompleteTypeAnnotation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ var BasicFeatures = (function () {
441441
var xx = c;
442442
retVal += ;
443443
try { }
444-
catch () { }
444+
catch (_a) { }
445445
Property;
446446
retVal += c.Member();
447447
retVal += xx.Foo() ? 0 : 1;

0 commit comments

Comments
 (0)