From 073994ec5526b112c10ccdee6a3a02778415c40e Mon Sep 17 00:00:00 2001 From: Yui T Date: Wed, 10 Dec 2014 15:44:36 -0800 Subject: [PATCH] Addres code review --- src/compiler/binder.ts | 3 +++ src/compiler/checker.ts | 35 ++++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index dfb5482b09c..a458779e52f 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -387,6 +387,9 @@ module ts { switch (node.kind) { case SyntaxKind.TypeParameter: bindDeclaration(node, SymbolFlags.TypeParameter, SymbolFlags.TypeParameterExcludes, /*isBlockScopeContainer*/ false); + if ((node).expression) { + (node).expression.parent = node; + } break; case SyntaxKind.Parameter: if (isBindingPattern((node).name)) { diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index fd79e04ad2e..f8280ffd230 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7020,7 +7020,7 @@ module ts { // Grammar Checking if (node.expression) { var sourceFile = getSourceFileOfNode(node); - grammarErrorOnFirstToken(sourceFile, node.expression, Diagnostics.Type_expected); + grammarErrorOnFirstToken(node.expression, Diagnostics.Type_expected); } checkSourceElement(node.constraint); @@ -7275,10 +7275,9 @@ module ts { function checkTupleType(node: TupleTypeNode) { // Grammar checking - var sourceFile = getSourceFileOfNode(node); - checkGrammarForDisallowedTrailingComma(sourceFile, node.elementTypes); - if (node.elementTypes.length === 0) { - grammarErrorOnNode(sourceFile, node, Diagnostics.A_tuple_type_element_list_cannot_be_empty); + var hasErrorFromDisallowedTrailingComma = checkGrammarForDisallowedTrailingComma(node.elementTypes); + if (!hasErrorFromDisallowedTrailingComma && node.elementTypes.length === 0) { + grammarErrorOnNode(node, Diagnostics.A_tuple_type_element_list_cannot_be_empty); } forEach(node.elementTypes, checkSourceElement); @@ -8669,22 +8668,35 @@ module ts { } // GRAMMAR CHECKING - function checkGrammarForDisallowedTrailingComma(sourceFile: SourceFile, list: NodeArray): void { + function checkGrammarForDisallowedTrailingComma(list: NodeArray): boolean { if (list && list.hasTrailingComma) { var start = list.end - ",".length; var end = list.end; + var sourceFile = getSourceFileOfNode(list[0]); grammarErrorAtPos(sourceFile, start, end - start, Diagnostics.Trailing_comma_not_allowed); + return true; } } function hasParseDiagnostics(sourceFile: SourceFile): boolean { - return sourceFile.parseDiagnostics.length > 0 ? true : false; + return sourceFile.parseDiagnostics.length > 0; } - function grammarErrorOnFirstToken(sourceFile: SourceFile, node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): void { + function scanToken(scanner: Scanner, pos: number) { + scanner.setTextPos(pos); + scanner.scan(); + var start = scanner.getTokenPos(); + scanner.setTextPos(start); + scanner.scan(); + return start; + } + + function grammarErrorOnFirstToken(node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): void { + var sourceFile = getSourceFileOfNode(node); if (!hasParseDiagnostics(sourceFile)) { - var start = skipTrivia(sourceFile.text, node.pos); - diagnostics.push(createFileDiagnostic(sourceFile, start, node.end - start, message, arg0, arg1, arg2)); + var scanner = createScanner(compilerOptions.target, /*skipTrivia*/ true, sourceFile.text); + var start = scanToken(scanner, node.pos); + diagnostics.push(createFileDiagnostic(sourceFile, start, scanner.getTextPos() - start, message, arg0, arg1, arg2)); } } @@ -8694,7 +8706,8 @@ module ts { } } - function grammarErrorOnNode(sourceFile: SourceFile, node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): void { + function grammarErrorOnNode(node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): void { + var sourceFile = getSourceFileOfNode(node); if (!hasParseDiagnostics(sourceFile)) { var span = getErrorSpanForNode(node); var start = span.end > span.pos ? skipTrivia(sourceFile.text, span.pos) : span.pos;