Move catch block checks to the grammar checker.

This commit is contained in:
Cyrus Najmabadi
2014-11-18 23:43:12 -08:00
parent 07f41dc786
commit 7fb21a25f6
2 changed files with 15 additions and 10 deletions

View File

@@ -3089,23 +3089,14 @@ module ts {
parseExpected(SyntaxKind.CatchKeyword);
parseExpected(SyntaxKind.OpenParenToken);
var variable = parseIdentifier();
var typeAnnotationColonStart = scanner.getTokenPos();
var typeAnnotationColonLength = scanner.getTextPos() - typeAnnotationColonStart;
var typeAnnotation = parseTypeAnnotation();
parseExpected(SyntaxKind.CloseParenToken);
var result = <CatchBlock>parseBlock(/* ignoreMissingOpenBrace */ false, /*checkForStrictMode*/ false);
result.kind = SyntaxKind.CatchBlock;
result.pos = pos;
result.variable = variable;
result.type = typeAnnotation;
if (typeAnnotation) {
errorAtPos(typeAnnotationColonStart, typeAnnotationColonLength, Diagnostics.Catch_clause_parameter_cannot_have_a_type_annotation);
}
if (isInStrictMode && isEvalOrArgumentsIdentifier(variable)) {
// It is a SyntaxError if a TryStatement with a Catch occurs within strict code and the Identifier of the
// Catch production is eval or arguments
reportInvalidUseInStrictMode(variable);
}
return result;
}
@@ -4086,6 +4077,7 @@ module ts {
switch (node.kind) {
case SyntaxKind.ArrowFunction: return visitArrowFunction(<FunctionExpression>node);
case SyntaxKind.CallSignature: return visitCallSignature(<SignatureDeclaration>node);
case SyntaxKind.CatchBlock: return visitCatchBlock(<CatchBlock>node);
case SyntaxKind.Constructor: return visitConstructor(<ConstructorDeclaration>node);
case SyntaxKind.ConstructorType: return visitConstructorType(<SignatureDeclaration>node);
case SyntaxKind.ConstructSignature: return visitConstructSignature(<SignatureDeclaration>node);
@@ -4136,6 +4128,18 @@ module ts {
checkParameterList(node.parameters);
}
function visitCatchBlock(node: CatchBlock) {
if (node.type) {
var colonStart = skipTrivia(sourceText, node.variable.end);
grammarErrorAtPos(colonStart, ":".length, Diagnostics.Catch_clause_parameter_cannot_have_a_type_annotation);
}
if (node.flags & NodeFlags.ParsedInStrictMode && isEvalOrArgumentsIdentifier(node.variable)) {
// It is a SyntaxError if a TryStatement with a Catch occurs within strict code and the Identifier of the
// Catch production is eval or arguments
reportInvalidUseInStrictMode(node.variable);
}
}
function visitConstructor(node: ConstructorDeclaration) {
checkParameterList(node.parameters) ||
checkConstructorTypeParameters(node) ||

View File

@@ -584,6 +584,7 @@ module ts {
export interface CatchBlock extends Block {
variable: Identifier;
type?: TypeNode;
}
export interface ClassDeclaration extends Declaration {