Merge pull request #173 from Microsoft/singleDefaultClause
Check for multiple 'default' clauses.
This commit is contained in:
Коммит
9bb17a66cd
|
@ -19,7 +19,9 @@ module ts {
|
|||
if (array) {
|
||||
var len = array.length;
|
||||
for (var i = 0; i < len; i++) {
|
||||
if (array[i] === value) return true;
|
||||
if (array[i] === value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
@ -29,7 +31,9 @@ module ts {
|
|||
if (array) {
|
||||
var len = array.length;
|
||||
for (var i = 0; i < len; i++) {
|
||||
if (array[i] === value) return i;
|
||||
if (array[i] === value) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
|
@ -41,7 +45,9 @@ module ts {
|
|||
result = [];
|
||||
for (var i = 0, len = array.length; i < len; i++) {
|
||||
var item = array[i];
|
||||
if (f(item)) result.push(item);
|
||||
if (f(item)) {
|
||||
result.push(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
|
|
@ -98,6 +98,7 @@ module ts {
|
|||
Type_expected: { code: -9999999, category: DiagnosticCategory.Error, key: "Type expected." },
|
||||
A_constructor_implementation_cannot_be_declared_in_an_ambient_context: { code: 1111, category: DiagnosticCategory.Error, key: "A constructor implementation cannot be declared in an ambient context." },
|
||||
A_class_member_cannot_be_declared_optional: { code: 1112, category: DiagnosticCategory.Error, key: "A class member cannot be declared optional." },
|
||||
A_default_clause_cannot_appear_more_than_once_in_a_switch_statement: { code: 1113, category: DiagnosticCategory.Error, key: "A 'default' clause cannot appear more than once in a 'switch' statement." },
|
||||
Duplicate_identifier_0: { code: 2000, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
|
||||
new_T_cannot_be_used_to_create_an_array_Use_new_Array_T_instead: { code: 2068, category: DiagnosticCategory.Error, key: "'new T[]' cannot be used to create an array. Use 'new Array<T>()' instead." },
|
||||
Multiple_constructor_implementations_are_not_allowed: { code: 2070, category: DiagnosticCategory.Error, key: "Multiple constructor implementations are not allowed." },
|
||||
|
|
|
@ -384,7 +384,10 @@
|
|||
"category": "Error",
|
||||
"code": 1112
|
||||
},
|
||||
|
||||
"A 'default' clause cannot appear more than once in a 'switch' statement.": {
|
||||
"category": "Error",
|
||||
"code": 1113
|
||||
},
|
||||
|
||||
"Duplicate identifier '{0}'.": {
|
||||
"category": "Error",
|
||||
|
|
|
@ -766,7 +766,7 @@ module ts {
|
|||
return false;
|
||||
}
|
||||
|
||||
// Parses a list of elements
|
||||
// Parses a semicolon-delimited list of elements
|
||||
function parseList<T extends Node>(kind: ParsingContext, parseElement: () => T): NodeArray<T> {
|
||||
var saveParsingContext = parsingContext;
|
||||
parsingContext |= 1 << kind;
|
||||
|
@ -789,7 +789,7 @@ module ts {
|
|||
return result;
|
||||
}
|
||||
|
||||
// Parses a comma delimited list of elements
|
||||
// Parses a comma-delimited list of elements
|
||||
function parseDelimitedList<T extends Node>(kind: ParsingContext, parseElement: () => T, trailingCommaBehavior: TrailingCommaBehavior): NodeArray<T> {
|
||||
var saveParsingContext = parsingContext;
|
||||
parsingContext |= 1 << kind;
|
||||
|
@ -2063,6 +2063,16 @@ module ts {
|
|||
parseExpected(SyntaxKind.OpenBraceToken);
|
||||
node.clauses = parseList(ParsingContext.SwitchClauses, parseCaseOrDefaultClause);
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
==== tests/cases/compiler/switchStatementsWithMultipleDefaults.ts (8 errors) ====
|
||||
|
||||
var x = 10;
|
||||
|
||||
switch (x) {
|
||||
case 1:
|
||||
case 2:
|
||||
default: // No issues.
|
||||
break;
|
||||
default: // Error; second 'default' clause.
|
||||
~~~~~~~~
|
||||
!!! A 'default' clause cannot appear more than once in a 'switch' statement.
|
||||
default: // Error; third 'default' clause.
|
||||
~~~~~~~~
|
||||
!!! A 'default' clause cannot appear more than once in a 'switch' statement.
|
||||
case 3:
|
||||
x *= x;
|
||||
}
|
||||
|
||||
switch (x) {
|
||||
default: // No issues.
|
||||
break;
|
||||
case 100:
|
||||
switch (x * x) {
|
||||
default: // No issues.
|
||||
default: // Error; second 'default' clause.
|
||||
~~~~~~~~
|
||||
!!! A 'default' clause cannot appear more than once in a 'switch' statement.
|
||||
break;
|
||||
case 10000:
|
||||
x /= x;
|
||||
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.
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
|
||||
var x = 10;
|
||||
|
||||
switch (x) {
|
||||
case 1:
|
||||
case 2:
|
||||
default: // No issues.
|
||||
break;
|
||||
default: // Error; second 'default' clause.
|
||||
default: // Error; third 'default' clause.
|
||||
case 3:
|
||||
x *= x;
|
||||
}
|
||||
|
||||
switch (x) {
|
||||
default: // No issues.
|
||||
break;
|
||||
case 100:
|
||||
switch (x * x) {
|
||||
default: // No issues.
|
||||
default: // Error; second 'default' clause.
|
||||
break;
|
||||
case 10000:
|
||||
x /= x;
|
||||
default: // Error, third 'default' clause
|
||||
def\u0061ult: // Error, fourth 'default' clause.
|
||||
// Errors on fifth-seventh
|
||||
default: return;
|
||||
default: default:
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче