diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index ec2b37f291..b132a7d13c 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2065,9 +2065,12 @@ module ts { parseExpected(SyntaxKind.CloseBraceToken); // Error on duplicate 'default' clauses. - var defaultClauses = filter(node.clauses, clause => clause.kind === SyntaxKind.DefaultClause); - for (var i = 1, len = defaultClauses.length; i < len; i++) { - grammarErrorOnNode(defaultClauses[i], Diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement); + 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); diff --git a/tests/baselines/reference/switchStatementsWithMultipleDefaults.errors.txt b/tests/baselines/reference/switchStatementsWithMultipleDefaults.errors.txt index fef743e13e..2ddcc832e0 100644 --- a/tests/baselines/reference/switchStatementsWithMultipleDefaults.errors.txt +++ b/tests/baselines/reference/switchStatementsWithMultipleDefaults.errors.txt @@ -1,4 +1,4 @@ -==== tests/cases/compiler/switchStatementsWithMultipleDefaults.ts (4 errors) ==== +==== tests/cases/compiler/switchStatementsWithMultipleDefaults.ts (8 errors) ==== var x = 10; @@ -24,14 +24,25 @@ switch (x * x) { default: // No issues. default: // Error; second 'default' clause. - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - break; - ~~~~~~~~~~~~~~~~~~~~~~ + ~~~~~~~~ !!! A 'default' clause cannot appear more than once in a 'switch' statement. + break; case 10000: x /= x; - default: + default: // Error, third 'default' clause ~~~~~~~~ +!!! A 'default' clause cannot appear more than once in a 'switch' statement. + def\u0061ult: // Error, fourth 'default' clause. + ~~~~~~~~~~~~~ +!!! A 'default' clause cannot appear more than once in a 'switch' statement. + // Errors on fifth-seventh + default: return; + ~~~~~~~~ +!!! A 'default' clause cannot appear more than once in a 'switch' statement. + default: default: + ~~~~~~~~ +!!! A 'default' clause cannot appear more than once in a 'switch' statement. + ~~~~~~~~ !!! A 'default' clause cannot appear more than once in a 'switch' statement. } } \ No newline at end of file diff --git a/tests/cases/compiler/switchStatementsWithMultipleDefaults.ts b/tests/cases/compiler/switchStatementsWithMultipleDefaults.ts index 8e0f9121b4..33a73cd492 100644 --- a/tests/cases/compiler/switchStatementsWithMultipleDefaults.ts +++ b/tests/cases/compiler/switchStatementsWithMultipleDefaults.ts @@ -22,6 +22,10 @@ switch (x) { break; case 10000: x /= x; - default: + default: // Error, third 'default' clause + def\u0061ult: // Error, fourth 'default' clause. + // Errors on fifth-seventh + default: return; + default: default: } } \ No newline at end of file