MOve switch checks to the grammar checker.

This commit is contained in:
Cyrus Najmabadi 2014-11-19 12:43:19 -08:00
parent e5e707709d
commit abbd89aa2b
2 changed files with 21 additions and 35 deletions

View File

@ -2947,16 +2947,6 @@ module ts {
inSwitchStatement = saveInSwitchStatement;
parseExpected(SyntaxKind.CloseBraceToken);
// Error on duplicate 'default' clauses.
var defaultClauses: CaseOrDefaultClause[] = filter(node.clauses, clause => clause.kind === SyntaxKind.DefaultClause);
for (var i = 1, n = defaultClauses.length; i < n; i++) {
var clause = defaultClauses[i];
var start = skipTrivia(file.text, clause.pos);
var end = clause.statements.length > 0 ? clause.statements[0].pos : clause.end;
grammarErrorAtPos(start, end - start, Diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement);
}
return finishNode(node);
}
@ -4039,6 +4029,7 @@ module ts {
case SyntaxKind.PostfixOperator: return visitPostfixOperator(<UnaryExpression>node);
case SyntaxKind.PrefixOperator: return visitPrefixOperator(<UnaryExpression>node);
case SyntaxKind.SetAccessor: return visitSetAccessor(<MethodDeclaration>node);
case SyntaxKind.SwitchStatement: return visitSwitchStatement(<SwitchStatement>node);
case SyntaxKind.TaggedTemplateExpression: return visitTaggedTemplateExpression(<TaggedTemplateExpression>node);
case SyntaxKind.TupleType: return visitTupleType(<TupleTypeNode>node);
case SyntaxKind.TypeReference: return visitTypeReference(<TypeReferenceNode>node);
@ -4515,6 +4506,25 @@ module ts {
}
}
function visitSwitchStatement(node: SwitchStatement) {
var firstDefaultClause: CaseOrDefaultClause;
// Error on duplicate 'default' clauses.
for (var i = 0, n = node.clauses.length; i < n; i++) {
var clause = node.clauses[i];
if (clause.kind === SyntaxKind.DefaultClause) {
if (firstDefaultClause === undefined) {
firstDefaultClause = clause;
}
else {
var start = skipTrivia(file.text, clause.pos);
var end = clause.statements.length > 0 ? clause.statements[0].pos : clause.end;
grammarErrorAtPos(start, end - start, Diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement);
}
}
}
}
function visitTaggedTemplateExpression(node: TaggedTemplateExpression) {
if (languageVersion < ScriptTarget.ES6) {
grammarErrorOnNode(node, Diagnostics.Tagged_templates_are_only_available_when_targeting_ECMAScript_6_and_higher);

View File

@ -1,15 +1,7 @@
tests/cases/compiler/switchStatementsWithMultipleDefaults.ts(9,5): error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
tests/cases/compiler/switchStatementsWithMultipleDefaults.ts(10,5): error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
tests/cases/compiler/switchStatementsWithMultipleDefaults.ts(21,13): error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
tests/cases/compiler/switchStatementsWithMultipleDefaults.ts(25,13): error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
tests/cases/compiler/switchStatementsWithMultipleDefaults.ts(26,13): error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
tests/cases/compiler/switchStatementsWithMultipleDefaults.ts(28,13): error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
tests/cases/compiler/switchStatementsWithMultipleDefaults.ts(28,22): error TS1108: A 'return' statement can only be used within a function body.
tests/cases/compiler/switchStatementsWithMultipleDefaults.ts(29,13): error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
tests/cases/compiler/switchStatementsWithMultipleDefaults.ts(29,22): error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
==== tests/cases/compiler/switchStatementsWithMultipleDefaults.ts (9 errors) ====
==== tests/cases/compiler/switchStatementsWithMultipleDefaults.ts (1 errors) ====
var x = 10;
@ -19,11 +11,7 @@ tests/cases/compiler/switchStatementsWithMultipleDefaults.ts(29,22): error TS111
default: // No issues.
break;
default: // Error; second 'default' clause.
~~~~~~~~
!!! error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
default: // Error; third 'default' clause.
~~~~~~~~
!!! error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
case 3:
x *= x;
}
@ -35,27 +23,15 @@ tests/cases/compiler/switchStatementsWithMultipleDefaults.ts(29,22): error TS111
switch (x * x) {
default: // No issues.
default: // Error; second 'default' clause.
~~~~~~~~
!!! error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
break;
case 10000:
x /= x;
default: // Error, third 'default' clause
~~~~~~~~
!!! error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
def\u0061ult: // Error, fourth 'default' clause.
~~~~~~~~~~~~~
!!! error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
// Errors on fifth-seventh
default: return;
~~~~~~~~
!!! error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.
default: default:
~~~~~~~~
!!! error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
~~~~~~~~
!!! error TS1113: A 'default' clause cannot appear more than once in a 'switch' statement.
}
}