Skip to content

Commit 635a773

Browse files
committed
Unite ParsingContext.ModuleElements and ParsingContext.BlockStatements
1 parent 32d57d9 commit 635a773

12 files changed

+54
-73
lines changed

src/compiler/parser.ts

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,18 +1122,15 @@ module ts {
11221122

11231123
switch (parsingContext) {
11241124
case ParsingContext.SourceElements:
1125-
case ParsingContext.ModuleElements:
1125+
case ParsingContext.BlockStatements:
1126+
case ParsingContext.SwitchClauseStatements:
11261127
// If we're in error recovery, then we don't want to treat ';' as an empty statement.
11271128
// The problem is that ';' can show up in far too many contexts, and if we see one
11281129
// and assume it's a statement, then we may bail out inappropriately from whatever
11291130
// we're parsing. For example, if we have a semicolon in the middle of a class, then
11301131
// we really don't want to assume the class is over and we're on a statement in the
11311132
// outer module. We just want to consume and move on.
11321133
return !(token === SyntaxKind.SemicolonToken && inErrorRecovery) && isStartOfStatement();
1133-
case ParsingContext.BlockStatements:
1134-
case ParsingContext.SwitchClauseStatements:
1135-
// During error recovery we don't treat empty statements as statements
1136-
return !(token === SyntaxKind.SemicolonToken && inErrorRecovery) && isStartOfStatement();
11371134
case ParsingContext.SwitchClauses:
11381135
return token === SyntaxKind.CaseKeyword || token === SyntaxKind.DefaultKeyword;
11391136
case ParsingContext.TypeMembers:
@@ -1243,7 +1240,6 @@ module ts {
12431240
}
12441241

12451242
switch (kind) {
1246-
case ParsingContext.ModuleElements:
12471243
case ParsingContext.BlockStatements:
12481244
case ParsingContext.SwitchClauses:
12491245
case ParsingContext.TypeMembers:
@@ -1454,15 +1450,13 @@ module ts {
14541450

14551451
function canReuseNode(node: Node, parsingContext: ParsingContext): boolean {
14561452
switch (parsingContext) {
1457-
case ParsingContext.ModuleElements:
1458-
return isReusableModuleElement(node);
1459-
14601453
case ParsingContext.ClassMembers:
14611454
return isReusableClassMember(node);
14621455

14631456
case ParsingContext.SwitchClauses:
14641457
return isReusableSwitchClause(node);
14651458

1459+
case ParsingContext.SourceElements:
14661460
case ParsingContext.BlockStatements:
14671461
case ParsingContext.SwitchClauseStatements:
14681462
return isReusableStatement(node);
@@ -1525,26 +1519,6 @@ module ts {
15251519
return false;
15261520
}
15271521

1528-
function isReusableModuleElement(node: Node) {
1529-
if (node) {
1530-
switch (node.kind) {
1531-
case SyntaxKind.ImportDeclaration:
1532-
case SyntaxKind.ImportEqualsDeclaration:
1533-
case SyntaxKind.ExportDeclaration:
1534-
case SyntaxKind.ExportAssignment:
1535-
case SyntaxKind.ClassDeclaration:
1536-
case SyntaxKind.InterfaceDeclaration:
1537-
case SyntaxKind.ModuleDeclaration:
1538-
case SyntaxKind.EnumDeclaration:
1539-
return true;
1540-
}
1541-
1542-
return isReusableStatement(node);
1543-
}
1544-
1545-
return false;
1546-
}
1547-
15481522
function isReusableClassMember(node: Node) {
15491523
if (node) {
15501524
switch (node.kind) {
@@ -1597,6 +1571,15 @@ module ts {
15971571
case SyntaxKind.LabeledStatement:
15981572
case SyntaxKind.DoStatement:
15991573
case SyntaxKind.DebuggerStatement:
1574+
case SyntaxKind.ImportDeclaration:
1575+
case SyntaxKind.ImportEqualsDeclaration:
1576+
case SyntaxKind.ExportDeclaration:
1577+
case SyntaxKind.ExportAssignment:
1578+
case SyntaxKind.ModuleDeclaration:
1579+
case SyntaxKind.ClassDeclaration:
1580+
case SyntaxKind.InterfaceDeclaration:
1581+
case SyntaxKind.EnumDeclaration:
1582+
case SyntaxKind.TypeAliasDeclaration:
16001583
return true;
16011584
}
16021585
}
@@ -1670,8 +1653,7 @@ module ts {
16701653
function parsingContextErrors(context: ParsingContext): DiagnosticMessage {
16711654
switch (context) {
16721655
case ParsingContext.SourceElements: return Diagnostics.Declaration_or_statement_expected;
1673-
case ParsingContext.ModuleElements: return Diagnostics.Declaration_or_statement_expected;
1674-
case ParsingContext.BlockStatements: return Diagnostics.Statement_expected;
1656+
case ParsingContext.BlockStatements: return Diagnostics.Declaration_or_statement_expected;
16751657
case ParsingContext.SwitchClauses: return Diagnostics.case_or_default_expected;
16761658
case ParsingContext.SwitchClauseStatements: return Diagnostics.Statement_expected;
16771659
case ParsingContext.TypeMembers: return Diagnostics.Property_or_signature_expected;
@@ -4600,7 +4582,7 @@ module ts {
46004582
function parseModuleBlock(): ModuleBlock {
46014583
let node = <ModuleBlock>createNode(SyntaxKind.ModuleBlock, scanner.getStartPos());
46024584
if (parseExpected(SyntaxKind.OpenBraceToken)) {
4603-
node.statements = parseList(ParsingContext.ModuleElements, /*checkForStrictMode*/ false, parseStatement);
4585+
node.statements = parseList(ParsingContext.BlockStatements, /*checkForStrictMode*/ false, parseStatement);
46044586
parseExpected(SyntaxKind.CloseBraceToken);
46054587
}
46064588
else {
@@ -4928,7 +4910,6 @@ module ts {
49284910

49294911
const enum ParsingContext {
49304912
SourceElements, // Elements in source file
4931-
ModuleElements, // Elements in module declaration
49324913
BlockStatements, // Statements in block
49334914
SwitchClauses, // Clauses in switch statement
49344915
SwitchClauseStatements, // Statements in switch clause
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/compiler/class2.ts(1,29): error TS1129: Statement expected.
1+
tests/cases/compiler/class2.ts(1,29): error TS1128: Declaration or statement expected.
22
tests/cases/compiler/class2.ts(1,45): error TS1128: Declaration or statement expected.
33

44

55
==== tests/cases/compiler/class2.ts (2 errors) ====
66
class foo { constructor() { static f = 3; } }
77
~~~~~~
8-
!!! error TS1129: Statement expected.
8+
!!! error TS1128: Declaration or statement expected.
99
~
1010
!!! error TS1128: Declaration or statement expected.

tests/baselines/reference/classUpdateTests.errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ tests/cases/compiler/classUpdateTests.ts(63,7): error TS2415: Class 'L' incorrec
88
tests/cases/compiler/classUpdateTests.ts(69,7): error TS2415: Class 'M' incorrectly extends base class 'G'.
99
Property 'p1' is private in type 'M' but not in type 'G'.
1010
tests/cases/compiler/classUpdateTests.ts(70,2): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties.
11-
tests/cases/compiler/classUpdateTests.ts(93,3): error TS1129: Statement expected.
11+
tests/cases/compiler/classUpdateTests.ts(93,3): error TS1128: Declaration or statement expected.
1212
tests/cases/compiler/classUpdateTests.ts(95,1): error TS1128: Declaration or statement expected.
13-
tests/cases/compiler/classUpdateTests.ts(99,3): error TS1129: Statement expected.
13+
tests/cases/compiler/classUpdateTests.ts(99,3): error TS1128: Declaration or statement expected.
1414
tests/cases/compiler/classUpdateTests.ts(101,1): error TS1128: Declaration or statement expected.
15-
tests/cases/compiler/classUpdateTests.ts(105,3): error TS1129: Statement expected.
15+
tests/cases/compiler/classUpdateTests.ts(105,3): error TS1128: Declaration or statement expected.
1616
tests/cases/compiler/classUpdateTests.ts(105,14): error TS1005: ';' expected.
1717
tests/cases/compiler/classUpdateTests.ts(107,1): error TS1128: Declaration or statement expected.
18-
tests/cases/compiler/classUpdateTests.ts(111,3): error TS1129: Statement expected.
18+
tests/cases/compiler/classUpdateTests.ts(111,3): error TS1128: Declaration or statement expected.
1919
tests/cases/compiler/classUpdateTests.ts(111,15): error TS1005: ';' expected.
2020
tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or statement expected.
2121

@@ -139,7 +139,7 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st
139139
constructor() {
140140
public p1 = 0; // ERROR
141141
~~~~~~
142-
!!! error TS1129: Statement expected.
142+
!!! error TS1128: Declaration or statement expected.
143143
}
144144
}
145145
~
@@ -149,7 +149,7 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st
149149
constructor() {
150150
private p1 = 0; // ERROR
151151
~~~~~~~
152-
!!! error TS1129: Statement expected.
152+
!!! error TS1128: Declaration or statement expected.
153153
}
154154
}
155155
~
@@ -159,7 +159,7 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st
159159
constructor() {
160160
public this.p1 = 0; // ERROR
161161
~~~~~~
162-
!!! error TS1129: Statement expected.
162+
!!! error TS1128: Declaration or statement expected.
163163
~
164164
!!! error TS1005: ';' expected.
165165
}
@@ -171,7 +171,7 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st
171171
constructor() {
172172
private this.p1 = 0; // ERROR
173173
~~~~~~~
174-
!!! error TS1129: Statement expected.
174+
!!! error TS1128: Declaration or statement expected.
175175
~
176176
!!! error TS1005: ';' expected.
177177
}

tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(22,35): error TS
55
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(22,39): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
66
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(24,28): error TS1005: ':' expected.
77
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(24,29): error TS1005: ',' expected.
8-
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(27,18): error TS1129: Statement expected.
8+
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(27,18): error TS1128: Declaration or statement expected.
99
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(27,26): error TS2304: Cannot find name 'bfs'.
1010
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(28,30): error TS1005: '=' expected.
1111
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(31,18): error TS1109: Expression expected.
@@ -129,7 +129,7 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(261,1): error TS
129129

130130
case = bfs.STATEMENTS(4);
131131
~~~~
132-
!!! error TS1129: Statement expected.
132+
!!! error TS1128: Declaration or statement expected.
133133
~~~
134134
!!! error TS2304: Cannot find name 'bfs'.
135135
if (retValue != 0) {

tests/baselines/reference/overloadingStaticFunctionsInFunctions.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(1,14): error TS1005: '(' expected.
2-
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(2,3): error TS1129: Statement expected.
2+
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(2,3): error TS1128: Declaration or statement expected.
33
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(2,10): error TS2304: Cannot find name 'test'.
4-
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(3,3): error TS1129: Statement expected.
4+
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(3,3): error TS1128: Declaration or statement expected.
55
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(3,10): error TS2304: Cannot find name 'test'.
66
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(3,15): error TS2304: Cannot find name 'name'.
77
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(3,19): error TS1005: ',' expected.
88
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(3,20): error TS2304: Cannot find name 'string'.
9-
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(4,3): error TS1129: Statement expected.
9+
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(4,3): error TS1128: Declaration or statement expected.
1010
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(4,10): error TS2304: Cannot find name 'test'.
1111
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(4,15): error TS2304: Cannot find name 'name'.
1212
tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(4,20): error TS1109: Expression expected.
@@ -20,12 +20,12 @@ tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(4,25): error TS100
2020
!!! error TS1005: '(' expected.
2121
static test()
2222
~~~~~~
23-
!!! error TS1129: Statement expected.
23+
!!! error TS1128: Declaration or statement expected.
2424
~~~~
2525
!!! error TS2304: Cannot find name 'test'.
2626
static test(name:string)
2727
~~~~~~
28-
!!! error TS1129: Statement expected.
28+
!!! error TS1128: Declaration or statement expected.
2929
~~~~
3030
!!! error TS2304: Cannot find name 'test'.
3131
~~~~
@@ -36,7 +36,7 @@ tests/cases/compiler/overloadingStaticFunctionsInFunctions.ts(4,25): error TS100
3636
!!! error TS2304: Cannot find name 'string'.
3737
static test(name?:any){ }
3838
~~~~~~
39-
!!! error TS1129: Statement expected.
39+
!!! error TS1128: Declaration or statement expected.
4040
~~~~
4141
!!! error TS2304: Cannot find name 'test'.
4242
~~~~

tests/baselines/reference/parserErrorRecoveryIfStatement6.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IfStatements/parserErrorRecoveryIfStatement6.ts(3,9): error TS2304: Cannot find name 'a'.
2-
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IfStatements/parserErrorRecoveryIfStatement6.ts(5,3): error TS1129: Statement expected.
2+
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IfStatements/parserErrorRecoveryIfStatement6.ts(5,3): error TS1128: Declaration or statement expected.
33

44

55
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IfStatements/parserErrorRecoveryIfStatement6.ts (2 errors) ====
@@ -11,7 +11,7 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IfStatements/parserErro
1111
}
1212
public f2() {
1313
~~~~~~
14-
!!! error TS1129: Statement expected.
14+
!!! error TS1128: Declaration or statement expected.
1515
}
1616
f3() {
1717
}

tests/baselines/reference/parserErrorRecovery_Block3.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(2,18): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement.
2-
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(4,5): error TS1129: Statement expected.
2+
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(4,5): error TS1128: Declaration or statement expected.
33
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(4,18): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement.
44

55

@@ -11,7 +11,7 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecov
1111

1212
private b(): boolean {
1313
~~~~~~~
14-
!!! error TS1129: Statement expected.
14+
!!! error TS1128: Declaration or statement expected.
1515
~~~~~~~
1616
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement.
1717
}

tests/baselines/reference/propertyWrappedInTry.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/propertyWrappedInTry.ts(3,5): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
2-
tests/cases/compiler/propertyWrappedInTry.ts(5,9): error TS1129: Statement expected.
2+
tests/cases/compiler/propertyWrappedInTry.ts(5,9): error TS1128: Declaration or statement expected.
33
tests/cases/compiler/propertyWrappedInTry.ts(5,16): error TS2304: Cannot find name 'bar'.
44
tests/cases/compiler/propertyWrappedInTry.ts(5,22): error TS2304: Cannot find name 'someInitThatMightFail'.
55
tests/cases/compiler/propertyWrappedInTry.ts(11,5): error TS1128: Declaration or statement expected.
@@ -17,7 +17,7 @@ tests/cases/compiler/propertyWrappedInTry.ts(17,1): error TS1128: Declaration or
1717

1818
public bar = someInitThatMightFail();
1919
~~~~~~
20-
!!! error TS1129: Statement expected.
20+
!!! error TS1128: Declaration or statement expected.
2121
~~~
2222
!!! error TS2304: Cannot find name 'bar'.
2323
~~~~~~~~~~~~~~~~~~~~~

tests/baselines/reference/staticClassProps.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/staticClassProps.ts(4,9): error TS1129: Statement expected.
1+
tests/cases/compiler/staticClassProps.ts(4,9): error TS1128: Declaration or statement expected.
22
tests/cases/compiler/staticClassProps.ts(6,1): error TS1128: Declaration or statement expected.
33

44

@@ -8,7 +8,7 @@ tests/cases/compiler/staticClassProps.ts(6,1): error TS1128: Declaration or stat
88
public foo() {
99
static z = 1;
1010
~~~~~~
11-
!!! error TS1129: Statement expected.
11+
!!! error TS1128: Declaration or statement expected.
1212
}
1313
}
1414
~

tests/baselines/reference/staticsInAFunction.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
tests/cases/compiler/staticsInAFunction.ts(1,13): error TS1005: '(' expected.
2-
tests/cases/compiler/staticsInAFunction.ts(2,4): error TS1129: Statement expected.
2+
tests/cases/compiler/staticsInAFunction.ts(2,4): error TS1128: Declaration or statement expected.
33
tests/cases/compiler/staticsInAFunction.ts(2,11): error TS2304: Cannot find name 'test'.
4-
tests/cases/compiler/staticsInAFunction.ts(3,4): error TS1129: Statement expected.
4+
tests/cases/compiler/staticsInAFunction.ts(3,4): error TS1128: Declaration or statement expected.
55
tests/cases/compiler/staticsInAFunction.ts(3,11): error TS2304: Cannot find name 'test'.
66
tests/cases/compiler/staticsInAFunction.ts(3,16): error TS2304: Cannot find name 'name'.
77
tests/cases/compiler/staticsInAFunction.ts(3,20): error TS1005: ',' expected.
88
tests/cases/compiler/staticsInAFunction.ts(3,21): error TS2304: Cannot find name 'string'.
9-
tests/cases/compiler/staticsInAFunction.ts(4,4): error TS1129: Statement expected.
9+
tests/cases/compiler/staticsInAFunction.ts(4,4): error TS1128: Declaration or statement expected.
1010
tests/cases/compiler/staticsInAFunction.ts(4,11): error TS2304: Cannot find name 'test'.
1111
tests/cases/compiler/staticsInAFunction.ts(4,16): error TS2304: Cannot find name 'name'.
1212
tests/cases/compiler/staticsInAFunction.ts(4,21): error TS1109: Expression expected.
@@ -20,12 +20,12 @@ tests/cases/compiler/staticsInAFunction.ts(4,26): error TS1005: ';' expected.
2020
!!! error TS1005: '(' expected.
2121
static test()
2222
~~~~~~
23-
!!! error TS1129: Statement expected.
23+
!!! error TS1128: Declaration or statement expected.
2424
~~~~
2525
!!! error TS2304: Cannot find name 'test'.
2626
static test(name:string)
2727
~~~~~~
28-
!!! error TS1129: Statement expected.
28+
!!! error TS1128: Declaration or statement expected.
2929
~~~~
3030
!!! error TS2304: Cannot find name 'test'.
3131
~~~~
@@ -36,7 +36,7 @@ tests/cases/compiler/staticsInAFunction.ts(4,26): error TS1005: ';' expected.
3636
!!! error TS2304: Cannot find name 'string'.
3737
static test(name?:any){}
3838
~~~~~~
39-
!!! error TS1129: Statement expected.
39+
!!! error TS1128: Declaration or statement expected.
4040
~~~~
4141
!!! error TS2304: Cannot find name 'test'.
4242
~~~~

0 commit comments

Comments
 (0)