From 8dc9f751a3da213af79c3e11ffa2a774afa46983 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 16 Dec 2014 17:32:15 -0800 Subject: [PATCH] Complete grammar checking migration; there are still errors which will be fixed once pull master into the branch --- src/compiler/checker.ts | 111 +++++++++- .../diagnosticInformationMap.generated.ts | 1 + src/compiler/diagnosticMessages.json | 198 +++++++++--------- src/compiler/parser.ts | 38 ++-- src/compiler/types.ts | 1 + .../reference/ambientErrors.errors.txt | 16 +- 6 files changed, 243 insertions(+), 122 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 28a398ec40d..81280618d02 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7826,6 +7826,11 @@ module ts { } function checkBlock(node: Block) { + // Grammar checking for SyntaxKind.Block + if (node.kind === SyntaxKind.Block) { + checkGrammarForStatementInAmbientContext(node); + } + forEach(node.statements, checkSourceElement); if (isFunctionBlock(node) || node.kind === SyntaxKind.ModuleBlock) { checkFunctionExpressionBodies(node); @@ -8085,27 +8090,40 @@ module ts { } function checkExpressionStatement(node: ExpressionStatement) { + // Grammar checking + checkGrammarForStatementInAmbientContext(node) + checkExpression(node.expression); } function checkIfStatement(node: IfStatement) { + // Grammar checking + checkGrammarForStatementInAmbientContext(node); + checkExpression(node.expression); checkSourceElement(node.thenStatement); checkSourceElement(node.elseStatement); } function checkDoStatement(node: DoStatement) { + // Grammar checking + checkGrammarForStatementInAmbientContext(node); + checkSourceElement(node.statement); checkExpression(node.expression); } function checkWhileStatement(node: WhileStatement) { + // Grammar checking + checkGrammarForStatementInAmbientContext(node); + checkExpression(node.expression); checkSourceElement(node.statement); } function checkForStatement(node: ForStatement) { // Grammar checking + checkGrammarForStatementInAmbientContext(node); checkGrammarVariableDeclarations(node, node.declarations); if (node.declarations) forEach(node.declarations, checkVariableLikeDeclaration); @@ -8116,7 +8134,9 @@ module ts { } function checkForInStatement(node: ForInStatement) { - // Grammar checking + // Grammar checkingcheck + checkGrammarForStatementInAmbientContext(node); + var declarations = node.declarations; if (!checkGrammarVariableDeclarations(node, declarations)) { if (declarations && declarations.length > 1) { @@ -8166,6 +8186,7 @@ module ts { function checkBreakOrContinueStatement(node: BreakOrContinueStatement) { // Grammar checking + checkGrammarForStatementInAmbientContext(node); checkGrammarBreakOrContinueStatement(node); // TODO: Check that target label is valid @@ -8177,6 +8198,7 @@ module ts { function checkReturnStatement(node: ReturnStatement) { // Grammar checking + checkGrammarForStatementInAmbientContext(node); var parent = node.parent; var inFunctionBlock = false; var functionBlock = getContainingFunction(node); @@ -8208,6 +8230,8 @@ module ts { function checkWithStatement(node: WithStatement) { // Grammar checking for withStatement + checkGrammarForStatementInAmbientContext(node); + if (node.parserContextFlags & ParserContextFlags.StrictMode) { grammarErrorOnFirstToken(node, Diagnostics.with_statements_are_not_allowed_in_strict_mode); } @@ -8224,6 +8248,8 @@ module ts { function checkSwitchStatement(node: SwitchStatement) { // Grammar checking + checkGrammarForStatementInAmbientContext(node); + var firstDefaultClause: CaseOrDefaultClause; var hasDuplicateDefaultClause = false; @@ -8258,8 +8284,8 @@ module ts { } function checkLabeledStatement(node: LabeledStatement) { - // ensure that label is unique // Grammar checking + checkGrammarForStatementInAmbientContext(node); var current = node.parent; while (current) { if (isAnyFunction(current)) { @@ -8273,10 +8299,15 @@ module ts { current = current.parent; } + // ensure that label is unique checkSourceElement(node.statement); } function checkThrowStatement(node: ThrowStatement) { + // Grammar checking + checkGrammarForStatementInAmbientContext(node) + + // Type checking if (node.expression === undefined) { grammarErrorAfterFirstToken(node, Diagnostics.Line_break_not_permitted_here); } @@ -8287,6 +8318,9 @@ module ts { } function checkTryStatement(node: TryStatement) { + // Grammar checking + checkGrammarForStatementInAmbientContext(node); + checkBlock(node.tryBlock); var catchClause = node.catchClause; if (catchClause) { @@ -9108,6 +9142,10 @@ module ts { return checkImportDeclaration(node); case SyntaxKind.ExportAssignment: return checkExportAssignment(node); + case SyntaxKind.EmptyStatement: + return checkGrammarForStatementInAmbientContext(node); + case SyntaxKind.DebuggerStatement: + return checkGrammarForStatementInAmbientContext(node); } } @@ -9199,6 +9237,9 @@ module ts { // Fully type check a source file and collect the relevant diagnostics. function checkSourceFile(node: SourceFile) { + // Grammar checking + checkGrammarSourceFile(node); + var links = getNodeLinks(node); if (!(links.flags & NodeCheckFlags.TypeChecked)) { emitExtends = false; @@ -10697,6 +10738,7 @@ module ts { function checkGrammarForBodyInAmbientContext(body: Block | Expression, isConstructor: boolean): boolean { if (isInAmbientContext(body) && body && body.kind === SyntaxKind.Block) { + var diagnostic = isConstructor ? Diagnostics.A_constructor_implementation_cannot_be_declared_in_an_ambient_context : Diagnostics.A_function_implementation_cannot_be_declared_in_an_ambient_context; @@ -10727,6 +10769,71 @@ module ts { } } + function checkGrammarTopLevelElementForRequiredDeclareModifier(node: Node): boolean { + // A declare modifier is required for any top level .d.ts declaration except export=, interfaces and imports: + // categories: + // + // DeclarationElement: + // ExportAssignment + // export_opt InterfaceDeclaration + // export_opt ImportDeclaration + // export_opt ExternalImportDeclaration + // export_opt AmbientDeclaration + // + if (node.kind === SyntaxKind.InterfaceDeclaration || + node.kind === SyntaxKind.ImportDeclaration || + node.kind === SyntaxKind.ExportAssignment || + (node.flags & NodeFlags.Ambient)) { + + return false; + } + + return grammarErrorOnFirstToken(node, Diagnostics.A_declare_modifier_is_required_for_a_top_level_declaration_in_a_d_ts_file); + } + + function checkGrammarTopLevelElementsForRequiredDeclareModifier(file: SourceFile): boolean { + for (var i = 0, n = file.statements.length; i < n; i++) { + var decl = file.statements[i]; + if (isDeclaration(decl) || decl.kind === SyntaxKind.VariableStatement) { + if (checkGrammarTopLevelElementForRequiredDeclareModifier(decl)) { + return true; + } + } + } + } + + function checkGrammarSourceFile(node: SourceFile): boolean { + return isInAmbientContext(node) && checkGrammarTopLevelElementsForRequiredDeclareModifier(node); + } + + function checkGrammarForStatementInAmbientContext(node: Node): void { + if (isInAmbientContext(node)) { + // Find containing block which is either Block, ModuleBlock, SourceFile + if (isAnyFunction(node.parent)) { + grammarErrorOnFirstToken(node, Diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts) + return; + } + + // We are either parented by another statement, or some sort of block. + // If we're in a block, we only want to really report an error once + // to prevent noisyness. So use a bit on the block to indicate if + // this has already been reported, and don't report if it has. + // + if (node.parent.kind === SyntaxKind.Block || node.parent.kind === SyntaxKind.ModuleBlock || node.parent.kind === SyntaxKind.SourceFile) { + var links = getNodeLinks(node.parent); + // Check if the containing block ever report this error + if (!links.hasReportedStatementInAmbientContext) { + links.hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, Diagnostics.Statements_are_not_allowed_in_ambient_contexts); + } + } + else { + // We must be parented by a statement. If so, there's no need + // to report the error as our parent will have already done it. + // Debug.assert(isStatement(node.parent)); + } + } + } + function grammarErrorAfterFirstToken(node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): boolean { var sourceFile = getSourceFileOfNode(node); if (!hasParseDiagnostics(sourceFile)) { diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 9885cdcca12..31f43f8f42b 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -144,6 +144,7 @@ module ts { Array_element_destructuring_pattern_expected: { code: 1181, category: DiagnosticCategory.Error, key: "Array element destructuring pattern expected." }, A_destructuring_declaration_must_have_an_initializer: { code: 1182, category: DiagnosticCategory.Error, key: "A destructuring declaration must have an initializer.", isEarly: true }, Destructuring_declarations_are_not_allowed_in_ambient_contexts: { code: 1183, category: DiagnosticCategory.Error, key: "Destructuring declarations are not allowed in ambient contexts.", isEarly: true }, + An_implementation_cannot_be_declared_in_ambient_contexts: { code: 1184, category: DiagnosticCategory.Error, key: "An implementation cannot be declared in ambient contexts.", isEarly: true }, Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." }, Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." }, Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 7f45cda8f04..868efbf6edb 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -10,7 +10,7 @@ "'{0}' expected.": { "category": "Error", "code": 1005, - "isEarly": true + "isEarly": true }, "A file cannot have a reference to itself.": { "category": "Error", @@ -19,7 +19,7 @@ "Trailing comma not allowed.": { "category": "Error", "code": 1009, - "isEarly": true + "isEarly": true }, "'*/' expected.": { "category": "Error", @@ -32,57 +32,57 @@ "Catch clause parameter cannot have a type annotation.": { "category": "Error", "code": 1013, - "isEarly": true + "isEarly": true }, "A rest parameter must be last in a parameter list.": { "category": "Error", "code": 1014, - "isEarly": true + "isEarly": true }, "Parameter cannot have question mark and initializer.": { "category": "Error", "code": 1015, - "isEarly": true + "isEarly": true }, "A required parameter cannot follow an optional parameter.": { "category": "Error", "code": 1016, - "isEarly": true + "isEarly": true }, "An index signature cannot have a rest parameter.": { "category": "Error", "code": 1017, - "isEarly": true + "isEarly": true }, "An index signature parameter cannot have an accessibility modifier.": { "category": "Error", "code": 1018, - "isEarly": true + "isEarly": true }, "An index signature parameter cannot have a question mark.": { "category": "Error", "code": 1019, - "isEarly": true + "isEarly": true }, "An index signature parameter cannot have an initializer.": { "category": "Error", "code": 1020, - "isEarly": true + "isEarly": true }, "An index signature must have a type annotation.": { "category": "Error", "code": 1021, - "isEarly": true + "isEarly": true }, "An index signature parameter must have a type annotation.": { "category": "Error", "code": 1022, - "isEarly": true + "isEarly": true }, "An index signature parameter type must be 'string' or 'number'.": { "category": "Error", "code": 1023, - "isEarly": true + "isEarly": true }, "A class or interface declaration can only have one 'extends' clause.": { "category": "Error", @@ -103,22 +103,22 @@ "Accessibility modifier already seen.": { "category": "Error", "code": 1028, - "isEarly": true + "isEarly": true }, "'{0}' modifier must precede '{1}' modifier.": { "category": "Error", "code": 1029, - "isEarly": true + "isEarly": true }, "'{0}' modifier already seen.": { "category": "Error", "code": 1030, - "isEarly": true + "isEarly": true }, "'{0}' modifier cannot appear on a class element.": { "category": "Error", "code": 1031, - "isEarly": true + "isEarly": true }, "An interface declaration cannot have an 'implements' clause.": { "category": "Error", @@ -131,37 +131,37 @@ "Only ambient modules can use quoted names.": { "category": "Error", "code": 1035, - "isEarly": true + "isEarly": true }, "Statements are not allowed in ambient contexts.": { "category": "Error", "code": 1036, - "isEarly": true + "isEarly": true }, "A function implementation cannot be declared in an ambient context.": { "category": "Error", "code": 1037, - "isEarly": true + "isEarly": true }, "A 'declare' modifier cannot be used in an already ambient context.": { "category": "Error", "code": 1038, - "isEarly": true + "isEarly": true }, "Initializers are not allowed in ambient contexts.": { "category": "Error", "code": 1039, - "isEarly": true + "isEarly": true }, "'{0}' modifier cannot appear on a module element.": { "category": "Error", "code": 1044, - "isEarly": true + "isEarly": true }, "A 'declare' modifier cannot be used with an interface declaration.": { "category": "Error", "code": 1045, - "isEarly": true + "isEarly": true }, "A 'declare' modifier is required for a top level declaration in a .d.ts file.": { "category": "Error", @@ -170,57 +170,57 @@ "A rest parameter cannot be optional.": { "category": "Error", "code": 1047, - "isEarly": true + "isEarly": true }, "A rest parameter cannot have an initializer.": { "category": "Error", "code": 1048, - "isEarly": true + "isEarly": true }, "A 'set' accessor must have exactly one parameter.": { "category": "Error", "code": 1049, - "isEarly": true + "isEarly": true }, "A 'set' accessor cannot have an optional parameter.": { "category": "Error", "code": 1051, - "isEarly": true + "isEarly": true }, "A 'set' accessor parameter cannot have an initializer.": { "category": "Error", "code": 1052, - "isEarly": true + "isEarly": true }, "A 'set' accessor cannot have rest parameter.": { "category": "Error", "code": 1053, - "isEarly": true + "isEarly": true }, "A 'get' accessor cannot have parameters.": { "category": "Error", "code": 1054, - "isEarly": true + "isEarly": true }, "Accessors are only available when targeting ECMAScript 5 and higher.": { "category": "Error", "code": 1056, - "isEarly": true + "isEarly": true }, "Enum member must have initializer.": { "category": "Error", "code": 1061, - "isEarly": true + "isEarly": true }, "An export assignment cannot be used in an internal module.": { "category": "Error", "code": 1063, - "isEarly": true + "isEarly": true }, "Ambient enum elements can only have integer literal initializers.": { "category": "Error", "code": 1066, - "isEarly": true + "isEarly": true }, "Unexpected token. A constructor, method, accessor, or property was expected.": { "category": "Error", @@ -237,107 +237,107 @@ "Octal literals are not available when targeting ECMAScript 5 and higher.": { "category": "Error", "code": 1085, - "isEarly": true + "isEarly": true }, "An accessor cannot be declared in an ambient context.": { "category": "Error", "code": 1086, - "isEarly": true + "isEarly": true }, "'{0}' modifier cannot appear on a constructor declaration.": { "category": "Error", "code": 1089, - "isEarly": true + "isEarly": true }, "'{0}' modifier cannot appear on a parameter.": { "category": "Error", "code": 1090, - "isEarly": true + "isEarly": true }, "Only a single variable declaration is allowed in a 'for...in' statement.": { "category": "Error", "code": 1091, - "isEarly": true + "isEarly": true }, "Type parameters cannot appear on a constructor declaration.": { "category": "Error", "code": 1092, - "isEarly": true + "isEarly": true }, "Type annotation cannot appear on a constructor declaration.": { "category": "Error", "code": 1093, - "isEarly": true + "isEarly": true }, "An accessor cannot have type parameters.": { "category": "Error", "code": 1094, - "isEarly": true + "isEarly": true }, "A 'set' accessor cannot have a return type annotation.": { "category": "Error", "code": 1095, - "isEarly": true + "isEarly": true }, "An index signature must have exactly one parameter.": { "category": "Error", "code": 1096, - "isEarly": true + "isEarly": true }, "'{0}' list cannot be empty.": { "category": "Error", "code": 1097, - "isEarly": true + "isEarly": true }, "Type parameter list cannot be empty.": { "category": "Error", "code": 1098, - "isEarly": true + "isEarly": true }, "Type argument list cannot be empty.": { "category": "Error", "code": 1099, - "isEarly": true + "isEarly": true }, "Invalid use of '{0}' in strict mode.": { "category": "Error", "code": 1100, - "isEarly": true + "isEarly": true }, "'with' statements are not allowed in strict mode.": { "category": "Error", "code": 1101, - "isEarly": true + "isEarly": true }, "'delete' cannot be called on an identifier in strict mode.": { "category": "Error", "code": 1102, - "isEarly": true + "isEarly": true }, "A 'continue' statement can only be used within an enclosing iteration statement.": { "category": "Error", "code": 1104, - "isEarly": true + "isEarly": true }, "A 'break' statement can only be used within an enclosing iteration or switch statement.": { "category": "Error", "code": 1105, - "isEarly": true + "isEarly": true }, "Jump target cannot cross function boundary.": { "category": "Error", "code": 1107, - "isEarly": true + "isEarly": true }, "A 'return' statement can only be used within a function body.": { "category": "Error", "code": 1108, - "isEarly": true + "isEarly": true }, "Expression expected.": { "category": "Error", "code": 1109, - "isEarly": true + "isEarly": true }, "Type expected.": { "category": "Error", @@ -347,67 +347,67 @@ "A constructor implementation cannot be declared in an ambient context.": { "category": "Error", "code": 1111, - "isEarly": true + "isEarly": true }, "A class member cannot be declared optional.": { "category": "Error", "code": 1112, - "isEarly": true + "isEarly": true }, "A 'default' clause cannot appear more than once in a 'switch' statement.": { "category": "Error", "code": 1113, - "isEarly": true + "isEarly": true }, "Duplicate label '{0}'": { "category": "Error", "code": 1114, - "isEarly": true + "isEarly": true }, "A 'continue' statement can only jump to a label of an enclosing iteration statement.": { "category": "Error", "code": 1115, - "isEarly": true + "isEarly": true }, "A 'break' statement can only jump to a label of an enclosing statement.": { "category": "Error", "code": 1116, - "isEarly": true + "isEarly": true }, "An object literal cannot have multiple properties with the same name in strict mode.": { "category": "Error", "code": 1117, - "isEarly": true + "isEarly": true }, "An object literal cannot have multiple get/set accessors with the same name.": { "category": "Error", "code": 1118, - "isEarly": true + "isEarly": true }, "An object literal cannot have property and accessor with the same name.": { "category": "Error", "code": 1119, - "isEarly": true + "isEarly": true }, "An export assignment cannot have modifiers.": { "category": "Error", "code": 1120, - "isEarly": true + "isEarly": true }, "Octal literals are not allowed in strict mode.": { "category": "Error", "code": 1121, - "isEarly": true + "isEarly": true }, "A tuple type element list cannot be empty.": { "category": "Error", "code": 1122, - "isEarly": true + "isEarly": true }, "Variable declaration list cannot be empty.": { "category": "Error", "code": 1123, - "isEarly": true + "isEarly": true }, "Digit expected.": { "category": "Error", @@ -456,7 +456,7 @@ "Argument expression expected.": { "category": "Error", "code": 1135, - "isEarly": true + "isEarly": true }, "Property assignment expected.": { "category": "Error", @@ -481,7 +481,7 @@ "String literal expected.": { "category": "Error", "code": 1141, - "isEarly": true + "isEarly": true }, "Line break not permitted here.": { "category": "Error", @@ -495,7 +495,7 @@ "Modifiers not permitted on index signature members.": { "category": "Error", "code": 1145, - "isEarly": true + "isEarly": true }, "Declaration expected.": { "category": "Error", @@ -504,7 +504,7 @@ "Import declarations in an internal module cannot reference an external module.": { "category": "Error", "code": 1147, - "isEarly": true + "isEarly": true }, "Cannot compile external modules unless the '--module' flag is provided.": { "category": "Error", @@ -517,7 +517,7 @@ "'new T[]' cannot be used to create an array. Use 'new Array()' instead.": { "category": "Error", "code": 1150, - "isEarly": true + "isEarly": true }, "'var', 'let' or 'const' expected.": { "category": "Error", @@ -526,32 +526,32 @@ "'let' declarations are only available when targeting ECMAScript 6 and higher.": { "category": "Error", "code": 1153, - "isEarly": true + "isEarly": true }, "'const' declarations are only available when targeting ECMAScript 6 and higher.": { "category": "Error", "code": 1154, - "isEarly": true + "isEarly": true }, "'const' declarations must be initialized": { "category": "Error", "code": 1155, - "isEarly": true + "isEarly": true }, "'const' declarations can only be declared inside a block.": { "category": "Error", "code": 1156, - "isEarly": true + "isEarly": true }, "'let' declarations can only be declared inside a block.": { "category": "Error", "code": 1157, - "isEarly": true + "isEarly": true }, "Tagged templates are only available when targeting ECMAScript 6 and higher.": { "category": "Error", "code": 1159, - "isEarly": true + "isEarly": true }, "Unterminated template literal.": { "category": "Error", @@ -564,48 +564,47 @@ "An object member cannot be declared optional.": { "category": "Error", "code": 1162, - "isEarly": true + "isEarly": true }, - "'yield' expression must be contained_within a generator declaration." - : { + "'yield' expression must be contained_within a generator declaration.": { "category": "Error", "code": 1163, - "isEarly": true + "isEarly": true }, "Computed property names are not allowed in enums.": { "category": "Error", "code": 1164, - "isEarly": true + "isEarly": true }, "Computed property names are not allowed in an ambient context.": { "category": "Error", "code": 1165, - "isEarly": true + "isEarly": true }, "Computed property names are not allowed in class property declarations.": { "category": "Error", "code": 1166, - "isEarly": true + "isEarly": true }, "Computed property names are only available when targeting ECMAScript 6 and higher.": { "category": "Error", "code": 1167, - "isEarly": true + "isEarly": true }, "Computed property names are not allowed in method overloads.": { "category": "Error", "code": 1168, - "isEarly": true + "isEarly": true }, "Computed property names are not allowed in interfaces.": { "category": "Error", "code": 1169, - "isEarly": true + "isEarly": true }, "Computed property names are not allowed in type literals.": { "category": "Error", "code": 1170, - "isEarly": true + "isEarly": true }, "A comma expression is not allowed in a computed property name.": { "category": "Error", @@ -614,27 +613,27 @@ "'extends' clause already seen.": { "category": "Error", "code": 1172, - "isEarly": true + "isEarly": true }, "'extends' clause must precede 'implements' clause.": { "category": "Error", "code": 1173, - "isEarly": true + "isEarly": true }, "Classes can only extend a single class.": { "category": "Error", "code": 1174, - "isEarly": true + "isEarly": true }, "'implements' clause already seen.": { "category": "Error", "code": 1175, - "isEarly": true + "isEarly": true }, "Interface declaration cannot have 'implements' clause.": { "category": "Error", "code": 1176, - "isEarly": true + "isEarly": true }, "Binary digit expected.": { "category": "Error", @@ -659,11 +658,16 @@ "A destructuring declaration must have an initializer.": { "category": "Error", "code": 1182, - "isEarly": true + "isEarly": true }, "Destructuring declarations are not allowed in ambient contexts.": { "category": "Error", "code": 1183, + "isEarly": true + }, + "An implementation cannot be declared in ambient contexts.": { + "category": "Error", + "code": 1184, "isEarly": true }, diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 49871d790a2..ebd1d9d31c4 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -4625,7 +4625,7 @@ module ts { function checkNode(node: Node, nodeKind: SyntaxKind): boolean { // Now do node specific checks. - switch (nodeKind) { + //switch (nodeKind) { //case SyntaxKind.BreakStatement: //case SyntaxKind.ContinueStatement: //return checkBreakOrContinueStatement(node); @@ -4667,7 +4667,7 @@ module ts { //return checkProperty(node); //case SyntaxKind.ReturnStatement: return checkReturnStatement(node); //case SyntaxKind.SetAccessor: return checkSetAccessor(node); - case SyntaxKind.SourceFile: return checkSourceFile(node); + //case SyntaxKind.SourceFile: return checkSourceFile(node); //case SyntaxKind.ShorthandPropertyAssignment: return checkShorthandPropertyAssignment(node); //case SyntaxKind.SwitchStatement: return checkSwitchStatement(node); //case SyntaxKind.TaggedTemplateExpression: return checkTaggedTemplateExpression(node); @@ -4677,7 +4677,9 @@ module ts { //case SyntaxKind.VariableStatement: return checkVariableStatement(node); //case SyntaxKind.WithStatement: return checkWithStatement(node); //case SyntaxKind.YieldExpression: return checkYieldExpression(node); - } + //return false + //} + return false; } function scanToken(pos: number) { @@ -4721,23 +4723,23 @@ module ts { function checkForStatementInAmbientContext(node: Node, kind: SyntaxKind): boolean { switch (kind) { - case SyntaxKind.Block: + //case SyntaxKind.Block: case SyntaxKind.EmptyStatement: - case SyntaxKind.IfStatement: - case SyntaxKind.DoStatement: - case SyntaxKind.WhileStatement: - case SyntaxKind.ForStatement: - case SyntaxKind.ForInStatement: - case SyntaxKind.ContinueStatement: - case SyntaxKind.BreakStatement: - case SyntaxKind.ReturnStatement: - case SyntaxKind.WithStatement: - case SyntaxKind.SwitchStatement: - case SyntaxKind.ThrowStatement: - case SyntaxKind.TryStatement: + //case SyntaxKind.IfStatement: + //case SyntaxKind.DoStatement: + //case SyntaxKind.WhileStatement: + //case SyntaxKind.ForStatement: + //case SyntaxKind.ForInStatement: + //case SyntaxKind.ContinueStatement: + //case SyntaxKind.BreakStatement: + //case SyntaxKind.ReturnStatement: + //case SyntaxKind.WithStatement: + //case SyntaxKind.SwitchStatement: + //case SyntaxKind.ThrowStatement: + //case SyntaxKind.TryStatement: case SyntaxKind.DebuggerStatement: - case SyntaxKind.LabeledStatement: - case SyntaxKind.ExpressionStatement: + //case SyntaxKind.LabeledStatement: + //case SyntaxKind.ExpressionStatement: return grammarErrorOnFirstToken(node, Diagnostics.Statements_are_not_allowed_in_ambient_contexts); } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 19baf052406..a733adefce9 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1214,6 +1214,7 @@ module ts { isVisible?: boolean; // Is this node visible localModuleName?: string; // Local name for module instance assignmentChecks?: Map; // Cache of assignment checks + hasReportedStatementInAmbientContext?: boolean; // Cache boolean if we report statements in ambient context } export const enum TypeFlags { diff --git a/tests/baselines/reference/ambientErrors.errors.txt b/tests/baselines/reference/ambientErrors.errors.txt index 834c35e541c..609229bf2f0 100644 --- a/tests/baselines/reference/ambientErrors.errors.txt +++ b/tests/baselines/reference/ambientErrors.errors.txt @@ -1,22 +1,24 @@ tests/cases/conformance/ambient/ambientErrors.ts(2,15): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/ambient/ambientErrors.ts(6,1): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/ambient/ambientErrors.ts(17,22): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/ambient/ambientErrors.ts(20,24): error TS1036: Statements are not allowed in ambient contexts. tests/cases/conformance/ambient/ambientErrors.ts(20,24): error TS1037: A function implementation cannot be declared in an ambient context. +tests/cases/conformance/ambient/ambientErrors.ts(24,5): error TS1066: Ambient enum elements can only have integer literal initializers. +tests/cases/conformance/ambient/ambientErrors.ts(29,5): error TS1066: Ambient enum elements can only have integer literal initializers. tests/cases/conformance/ambient/ambientErrors.ts(34,11): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/ambient/ambientErrors.ts(35,19): error TS1036: Statements are not allowed in ambient contexts. tests/cases/conformance/ambient/ambientErrors.ts(35,19): error TS1037: A function implementation cannot be declared in an ambient context. tests/cases/conformance/ambient/ambientErrors.ts(37,20): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/conformance/ambient/ambientErrors.ts(38,13): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/conformance/ambient/ambientErrors.ts(39,23): error TS1111: A constructor implementation cannot be declared in an ambient context. tests/cases/conformance/ambient/ambientErrors.ts(40,14): error TS1037: A function implementation cannot be declared in an ambient context. tests/cases/conformance/ambient/ambientErrors.ts(41,22): error TS1037: A function implementation cannot be declared in an ambient context. -tests/cases/conformance/ambient/ambientErrors.ts(6,1): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/conformance/ambient/ambientErrors.ts(17,22): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/ambient/ambientErrors.ts(24,5): error TS1066: Ambient enum elements can only have integer literal initializers. -tests/cases/conformance/ambient/ambientErrors.ts(29,5): error TS1066: Ambient enum elements can only have integer literal initializers. tests/cases/conformance/ambient/ambientErrors.ts(47,20): error TS2435: Ambient external modules cannot be nested in other modules. tests/cases/conformance/ambient/ambientErrors.ts(51,16): error TS2436: Ambient external module declaration cannot specify relative module name. tests/cases/conformance/ambient/ambientErrors.ts(57,5): error TS2309: An export assignment cannot be used in a module with other exported elements. -==== tests/cases/conformance/ambient/ambientErrors.ts (16 errors) ==== +==== tests/cases/conformance/ambient/ambientErrors.ts (18 errors) ==== // Ambient variable with an initializer declare var x = 4; ~ @@ -44,6 +46,8 @@ tests/cases/conformance/ambient/ambientErrors.ts(57,5): error TS2309: An export // Ambient function with function body declare function fn4() { }; ~ +!!! error TS1036: Statements are not allowed in ambient contexts. + ~ !!! error TS1037: A function implementation cannot be declared in an ambient context. // Ambient enum with non - integer literal constant member @@ -67,6 +71,8 @@ tests/cases/conformance/ambient/ambientErrors.ts(57,5): error TS2309: An export !!! error TS1039: Initializers are not allowed in ambient contexts. function fn() { } ~ +!!! error TS1036: Statements are not allowed in ambient contexts. + ~ !!! error TS1037: A function implementation cannot be declared in an ambient context. class C { static x = 3;