Move property initializer checks to the grammar checker.

This commit is contained in:
Cyrus Najmabadi 2014-11-19 18:06:01 -08:00
parent 76f88392a4
commit 9751dca809
3 changed files with 63 additions and 17 deletions

View file

@ -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(<ParameterDeclaration>node);
case SyntaxKind.PostfixOperator: return visitPostfixOperator(<UnaryExpression>node);
case SyntaxKind.PrefixOperator: return visitPrefixOperator(<UnaryExpression>node);
case SyntaxKind.Property: return visitProperty(<PropertyDeclaration>node);
case SyntaxKind.PropertyAssignment: return visitPropertyAssignment(<PropertyDeclaration>node);
case SyntaxKind.ReturnStatement: return visitReturnStatement(<ReturnStatement>node);
case SyntaxKind.SetAccessor: return visitSetAccessor(<MethodDeclaration>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);
}

View file

@ -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.
}
}

View file

@ -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.
}