Skip to content

Commit fa04d4d

Browse files
Changed error spans for duplicate default clauses, added tests for it.
1 parent 4ac676f commit fa04d4d

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/compiler/parser.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,9 +2065,12 @@ module ts {
20652065
parseExpected(SyntaxKind.CloseBraceToken);
20662066

20672067
// Error on duplicate 'default' clauses.
2068-
var defaultClauses = filter(node.clauses, clause => clause.kind === SyntaxKind.DefaultClause);
2069-
for (var i = 1, len = defaultClauses.length; i < len; i++) {
2070-
grammarErrorOnNode(defaultClauses[i], Diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement);
2068+
var defaultClauses: CaseOrDefaultClause[] = filter(node.clauses, clause => clause.kind === SyntaxKind.DefaultClause);
2069+
for (var i = 1, n = defaultClauses.length; i < n; i++) {
2070+
var clause = defaultClauses[i];
2071+
var start = skipTrivia(file.text, clause.pos);
2072+
var end = clause.statements.length > 0 ? clause.statements[0].pos : clause.end;
2073+
grammarErrorAtPos(start, end - start, Diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement);
20712074
}
20722075

20732076
return finishNode(node);

tests/baselines/reference/switchStatementsWithMultipleDefaults.errors.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
==== tests/cases/compiler/switchStatementsWithMultipleDefaults.ts (4 errors) ====
1+
==== tests/cases/compiler/switchStatementsWithMultipleDefaults.ts (8 errors) ====
22

33
var x = 10;
44

@@ -24,14 +24,25 @@
2424
switch (x * x) {
2525
default: // No issues.
2626
default: // Error; second 'default' clause.
27-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28-
break;
29-
~~~~~~~~~~~~~~~~~~~~~~
27+
~~~~~~~~
3028
!!! A 'default' clause cannot appear more than once in a 'switch' statement.
29+
break;
3130
case 10000:
3231
x /= x;
33-
default:
32+
default: // Error, third 'default' clause
3433
~~~~~~~~
34+
!!! A 'default' clause cannot appear more than once in a 'switch' statement.
35+
def\u0061ult: // Error, fourth 'default' clause.
36+
~~~~~~~~~~~~~
37+
!!! A 'default' clause cannot appear more than once in a 'switch' statement.
38+
// Errors on fifth-seventh
39+
default: return;
40+
~~~~~~~~
41+
!!! A 'default' clause cannot appear more than once in a 'switch' statement.
42+
default: default:
43+
~~~~~~~~
44+
!!! A 'default' clause cannot appear more than once in a 'switch' statement.
45+
~~~~~~~~
3546
!!! A 'default' clause cannot appear more than once in a 'switch' statement.
3647
}
3748
}

tests/cases/compiler/switchStatementsWithMultipleDefaults.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ switch (x) {
2222
break;
2323
case 10000:
2424
x /= x;
25-
default:
25+
default: // Error, third 'default' clause
26+
def\u0061ult: // Error, fourth 'default' clause.
27+
// Errors on fifth-seventh
28+
default: return;
29+
default: default:
2630
}
2731
}

0 commit comments

Comments
 (0)