Skip to content

Commit 7439ee3

Browse files
committed
Merge pull request #651 from Microsoft/testForDiagnosticBounds
Add asserts for bounds on diagnostics
2 parents bf084f1 + fad5650 commit 7439ee3

File tree

6 files changed

+13
-6
lines changed

6 files changed

+13
-6
lines changed

src/compiler/core.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ module ts {
198198

199199
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: any[]): Diagnostic;
200200
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage): Diagnostic {
201+
Debug.assert(start >= 0, "start must be non-negative, is " + start);
202+
Debug.assert(length >= 0, "length must be non-negative, is " + length);
203+
201204
var text = getLocaleSpecificMessage(message.key);
202205

203206
if (arguments.length > 4) {
@@ -252,6 +255,9 @@ module ts {
252255
}
253256

254257
export function flattenDiagnosticChain(file: SourceFile, start: number, length: number, diagnosticChain: DiagnosticMessageChain, newLine: string): Diagnostic {
258+
Debug.assert(start >= 0, "start must be non-negative, is " + start);
259+
Debug.assert(length >= 0, "length must be non-negative, is " + length);
260+
255261
var code = diagnosticChain.code;
256262
var category = diagnosticChain.category;
257263
var messageText = "";

src/compiler/parser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ module ts {
8181
export function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): Diagnostic {
8282
node = getErrorSpanForNode(node);
8383
var file = getSourceFileOfNode(node);
84-
var start = skipTrivia(file.text, node.pos);
84+
var start = node.kind === SyntaxKind.Missing ? node.pos : skipTrivia(file.text, node.pos);
8585
var length = node.end - start;
8686

8787
return createFileDiagnostic(file, start, length, message, arg0, arg1, arg2);
@@ -2876,10 +2876,11 @@ module ts {
28762876
parseExpected(SyntaxKind.VarKeyword);
28772877
node.declarations = parseVariableDeclarationList(flags, /*noIn*/false);
28782878
parseSemicolon();
2879+
finishNode(node);
28792880
if (!node.declarations.length && file.syntacticErrors.length === errorCountBeforeVarStatement) {
28802881
grammarErrorOnNode(node, Diagnostics.Variable_declaration_list_cannot_be_empty);
28812882
}
2882-
return finishNode(node);
2883+
return node;
28832884
}
28842885

28852886
function parseFunctionDeclaration(pos?: number, flags?: NodeFlags): FunctionDeclaration {

tests/baselines/reference/parserEqualsGreaterThanAfterFunction1.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
function =>
33
~~
44
!!! Identifier expected.
5-
5+
66
!!! Function implementation is missing or not immediately following the declaration.

tests/baselines/reference/parserEqualsGreaterThanAfterFunction2.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
!!! ',' expected.
99

1010
!!! ')' expected.
11-
11+
1212
!!! Function implementation is missing or not immediately following the declaration.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
==== tests/cases/conformance/parser/ecmascript5/VariableDeclarations/parserVariableDeclaration6.ts (1 errors) ====
22
var
3-
3+
~~~
44
!!! Variable declaration list cannot be empty.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
==== tests/cases/conformance/parser/ecmascript5/VariableDeclarations/parserVariableDeclaration8.ts (1 errors) ====
22
var ;
3-
3+
~~~~~
44
!!! Variable declaration list cannot be empty.

0 commit comments

Comments
 (0)