diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index b27366c953c..4b3443334fd 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -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(node); case SyntaxKind.PrefixOperator: return visitPrefixOperator(node); case SyntaxKind.SetAccessor: return visitSetAccessor(node); + case SyntaxKind.SwitchStatement: return visitSwitchStatement(node); case SyntaxKind.TaggedTemplateExpression: return visitTaggedTemplateExpression(node); case SyntaxKind.TupleType: return visitTupleType(node); case SyntaxKind.TypeReference: return visitTypeReference(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); diff --git a/tests/baselines/reference/switchStatementsWithMultipleDefaults.errors.txt b/tests/baselines/reference/switchStatementsWithMultipleDefaults.errors.txt index 28c86a7f0f2..cd892f5faa2 100644 --- a/tests/baselines/reference/switchStatementsWithMultipleDefaults.errors.txt +++ b/tests/baselines/reference/switchStatementsWithMultipleDefaults.errors.txt @@ -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. } } \ No newline at end of file