diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 2fe60edd4c7..12712fcf805 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2994,9 +2994,6 @@ module ts { property.initializer = parseInitializer(/*inParameter*/ false); parseSemicolon(); - if (inAmbientContext && property.initializer && errorCountBeforePropertyDeclaration === file.parseDiagnostics.length) { - grammarErrorAtPos(initializerStart, initializerFirstTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); - } return finishNode(property); } } @@ -3739,6 +3736,7 @@ module ts { case SyntaxKind.Parameter: return visitParameter(node); case SyntaxKind.PostfixOperator: return visitPostfixOperator(node); case SyntaxKind.PrefixOperator: return visitPrefixOperator(node); + case SyntaxKind.Property: return visitProperty(node); case SyntaxKind.PropertyAssignment: return visitPropertyAssignment(node); case SyntaxKind.ReturnStatement: return visitReturnStatement(node); case SyntaxKind.SetAccessor: return visitSetAccessor(node); @@ -4381,6 +4379,12 @@ module ts { } } + function visitProperty(node: PropertyDeclaration) { + if (inAmbientContext && node.initializer) { + grammarErrorOnFirstToken(node.initializer, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + } + } + function visitPropertyAssignment(node: PropertyDeclaration) { checkForInvalidQuestionMark(node, Diagnostics.An_object_member_cannot_be_declared_optional); } diff --git a/tests/baselines/reference/ambientErrors.errors.txt b/tests/baselines/reference/ambientErrors.errors.txt index a1e85be7401..6fe7a25dfe9 100644 --- a/tests/baselines/reference/ambientErrors.errors.txt +++ b/tests/baselines/reference/ambientErrors.errors.txt @@ -1,5 +1,14 @@ -tests/cases/conformance/ambient/ambientErrors.ts(37,18): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/conformance/ambient/ambientErrors.ts(38,11): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/ambient/ambientErrors.ts(2,15): error TS1039: Initializers 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 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(47,20): error TS2435: Ambient external modules cannot be nested in other modules. @@ -7,9 +16,11 @@ tests/cases/conformance/ambient/ambientErrors.ts(51,16): error TS2436: Ambient e 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 (7 errors) ==== +==== tests/cases/conformance/ambient/ambientErrors.ts (16 errors) ==== // Ambient variable with an initializer declare var x = 4; + ~ +!!! error TS1039: Initializers are not allowed in ambient contexts. // Ambient functions with invalid overloads declare function fn(x: number): string; @@ -32,31 +43,47 @@ tests/cases/conformance/ambient/ambientErrors.ts(57,5): error TS2309: An export // Ambient function with function body declare function fn4() { }; + ~ +!!! error TS1037: A function implementation cannot be declared in an ambient context. // Ambient enum with non - integer literal constant member declare enum E1 { y = 4.23 + ~ +!!! error TS1066: Ambient enum elements can only have integer literal initializers. } // Ambient enum with computer member declare enum E2 { x = 'foo'.length + ~ +!!! error TS1066: Ambient enum elements can only have integer literal initializers. } // Ambient module with initializers for values, bodies for functions / classes declare module M1 { var x = 3; - function fn() { } - class C { - static x = 3; - ~ -!!! error TS1039: Initializers are not allowed in ambient contexts. - y = 4; ~ +!!! error TS1039: Initializers are not allowed in ambient contexts. + function fn() { } + ~ +!!! error TS1037: A function implementation cannot be declared in an ambient context. + class C { + static x = 3; + ~ +!!! error TS1039: Initializers are not allowed in ambient contexts. + y = 4; + ~ !!! error TS1039: Initializers are not allowed in ambient contexts. constructor() { } + ~ +!!! error TS1111: A constructor implementation cannot be declared in an ambient context. fn() { } + ~ +!!! error TS1037: A function implementation cannot be declared in an ambient context. static sfn() { } + ~ +!!! error TS1037: A function implementation cannot be declared in an ambient context. } } diff --git a/tests/baselines/reference/initializersInDeclarations.errors.txt b/tests/baselines/reference/initializersInDeclarations.errors.txt index 9e02a1cd747..99798d749c2 100644 --- a/tests/baselines/reference/initializersInDeclarations.errors.txt +++ b/tests/baselines/reference/initializersInDeclarations.errors.txt @@ -1,28 +1,43 @@ -tests/cases/conformance/externalModules/initializersInDeclarations.ts(5,7): error TS1039: Initializers are not allowed in ambient contexts. -tests/cases/conformance/externalModules/initializersInDeclarations.ts(6,14): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/externalModules/initializersInDeclarations.ts(5,9): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/externalModules/initializersInDeclarations.ts(6,16): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/externalModules/initializersInDeclarations.ts(8,3): error TS1036: Statements are not allowed in ambient contexts. +tests/cases/conformance/externalModules/initializersInDeclarations.ts(12,15): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/externalModules/initializersInDeclarations.ts(13,15): error TS1039: Initializers are not allowed in ambient contexts. +tests/cases/conformance/externalModules/initializersInDeclarations.ts(16,2): error TS1036: Statements are not allowed in ambient contexts. +tests/cases/conformance/externalModules/initializersInDeclarations.ts(18,16): error TS1039: Initializers are not allowed in ambient contexts. -==== tests/cases/conformance/externalModules/initializersInDeclarations.ts (2 errors) ==== +==== tests/cases/conformance/externalModules/initializersInDeclarations.ts (7 errors) ==== // Errors: Initializers & statements in declaration file declare class Foo { name = "test"; - ~ + ~~~~~~ !!! error TS1039: Initializers are not allowed in ambient contexts. "some prop" = 42; - ~ + ~~ !!! error TS1039: Initializers are not allowed in ambient contexts. fn(): boolean { return false; + ~~~~~~ +!!! error TS1036: Statements are not allowed in ambient contexts. } } declare var x = []; + ~ +!!! error TS1039: Initializers are not allowed in ambient contexts. declare var y = {}; + ~ +!!! error TS1039: Initializers are not allowed in ambient contexts. declare module M1 { while(true); + ~~~~~ +!!! error TS1036: Statements are not allowed in ambient contexts. export var v1 = () => false; + ~ +!!! error TS1039: Initializers are not allowed in ambient contexts. } \ No newline at end of file