Move variable grammar checks to the grammar checker.
This commit is contained in:
parent
78af519f5f
commit
5a7929aec7
|
@ -1005,7 +1005,6 @@ module ts {
|
|||
grammarErrorOnNode(node, Diagnostics.Invalid_use_of_0_in_strict_mode, name);
|
||||
}
|
||||
|
||||
|
||||
function grammarErrorAtPos(start: number, length: number, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): void {
|
||||
file._parserDiagnostics.push(createFileDiagnostic(file, start, length, message, arg0, arg1, arg2));
|
||||
}
|
||||
|
@ -3292,7 +3291,6 @@ module ts {
|
|||
function parseVariableDeclaration(flags: NodeFlags, noIn?: boolean): VariableDeclaration {
|
||||
var node = <VariableDeclaration>createNode(SyntaxKind.VariableDeclaration);
|
||||
node.flags = flags;
|
||||
var errorCountBeforeVariableDeclaration = file._parserDiagnostics.length;
|
||||
node.name = parseIdentifier();
|
||||
node.type = parseTypeAnnotation();
|
||||
|
||||
|
@ -3301,17 +3299,6 @@ module ts {
|
|||
var initializerFirstTokenLength = scanner.getTextPos() - initializerStart;
|
||||
node.initializer = parseInitializer(/*inParameter*/ false, noIn);
|
||||
|
||||
if (inAmbientContext && node.initializer && errorCountBeforeVariableDeclaration === file._parserDiagnostics.length) {
|
||||
grammarErrorAtPos(initializerStart, initializerFirstTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
|
||||
}
|
||||
if (!inAmbientContext && !node.initializer && flags & NodeFlags.Const) {
|
||||
grammarErrorOnNode(node, Diagnostics.const_declarations_must_be_initialized);
|
||||
}
|
||||
if (isInStrictMode && isEvalOrArgumentsIdentifier(node.name)) {
|
||||
// It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code
|
||||
// and its Identifier is eval or arguments
|
||||
reportInvalidUseInStrictMode(node.name);
|
||||
}
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
|
@ -4147,6 +4134,7 @@ module ts {
|
|||
case SyntaxKind.PrefixOperator: return visitPrefixOperator(<UnaryExpression>node);
|
||||
case SyntaxKind.SetAccessor: return visitSetAccessor(<MethodDeclaration>node);
|
||||
case SyntaxKind.TaggedTemplateExpression: return visitTaggedTemplateExpression(<TaggedTemplateExpression>node);
|
||||
case SyntaxKind.VariableDeclaration: return visitVariableDeclaration(<VariableDeclaration>node);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4159,6 +4147,11 @@ module ts {
|
|||
return true;
|
||||
}
|
||||
|
||||
function grammarErrorAtPos(start: number, length: number, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): boolean {
|
||||
file._syntacticDiagnostics.push(createFileDiagnostic(file, start, length, message, arg0, arg1, arg2));
|
||||
return true;
|
||||
}
|
||||
|
||||
function reportInvalidUseInStrictMode(node: Identifier): boolean {
|
||||
// declarationNameToString cannot be used here since it uses a backreference to 'parent' that is not yet set
|
||||
var name = sourceText.substring(skipTrivia(sourceText, node.pos), node.end);
|
||||
|
@ -4479,6 +4472,21 @@ module ts {
|
|||
grammarErrorOnNode(node, Diagnostics.Tagged_templates_are_only_available_when_targeting_ECMAScript_6_and_higher);
|
||||
}
|
||||
}
|
||||
|
||||
function visitVariableDeclaration(node: VariableDeclaration) {
|
||||
if (inAmbientContext && node.initializer) {
|
||||
var equalsPos = node.type ? skipTrivia(sourceText, node.type.end) : skipTrivia(sourceText, node.name.end);
|
||||
grammarErrorAtPos(equalsPos, "=".length, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
|
||||
}
|
||||
if (!inAmbientContext && !node.initializer && node.flags & NodeFlags.Const) {
|
||||
grammarErrorOnNode(node, Diagnostics.const_declarations_must_be_initialized);
|
||||
}
|
||||
if (node.flags & NodeFlags.ParsedInStrictMode && isEvalOrArgumentsIdentifier(node.name)) {
|
||||
// It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code
|
||||
// and its Identifier is eval or arguments
|
||||
reportInvalidUseInStrictMode(node.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program {
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
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(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,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.
|
||||
|
@ -14,11 +12,9 @@ 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 (14 errors) ====
|
||||
==== tests/cases/conformance/ambient/ambientErrors.ts (12 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;
|
||||
|
@ -57,8 +53,6 @@ tests/cases/conformance/ambient/ambientErrors.ts(57,5): error TS2309: An export
|
|||
// Ambient module with initializers for values, bodies for functions / classes
|
||||
declare module M1 {
|
||||
var x = 3;
|
||||
~
|
||||
!!! error TS1039: Initializers are not allowed in ambient contexts.
|
||||
function fn() { }
|
||||
~
|
||||
!!! error TS1037: A function implementation cannot be declared in an ambient context.
|
||||
|
|
7
tests/baselines/reference/ambientErrors1.errors.txt
Normal file
7
tests/baselines/reference/ambientErrors1.errors.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
tests/cases/compiler/ambientErrors1.ts(1,15): error TS1039: Initializers are not allowed in ambient contexts.
|
||||
|
||||
|
||||
==== tests/cases/compiler/ambientErrors1.ts (1 errors) ====
|
||||
declare var x = 4;
|
||||
~
|
||||
!!! error TS1039: Initializers are not allowed in ambient contexts.
|
|
@ -1,13 +1,10 @@
|
|||
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(7,16): error TS1037: A function implementation cannot be declared in an ambient context.
|
||||
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 (7 errors) ====
|
||||
==== tests/cases/conformance/externalModules/initializersInDeclarations.ts (4 errors) ====
|
||||
|
||||
// Errors: Initializers & statements in declaration file
|
||||
|
||||
|
@ -26,11 +23,7 @@ tests/cases/conformance/externalModules/initializersInDeclarations.ts(18,16): er
|
|||
}
|
||||
|
||||
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);
|
||||
|
@ -38,6 +31,4 @@ tests/cases/conformance/externalModules/initializersInDeclarations.ts(18,16): er
|
|||
!!! error TS1036: Statements are not allowed in ambient contexts.
|
||||
|
||||
export var v1 = () => false;
|
||||
~
|
||||
!!! error TS1039: Initializers are not allowed in ambient contexts.
|
||||
}
|
|
@ -1,10 +1,7 @@
|
|||
tests/cases/compiler/missingRequiredDeclare.d.ts(1,1): error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file.
|
||||
tests/cases/compiler/missingRequiredDeclare.d.ts(1,7): error TS1039: Initializers are not allowed in ambient contexts.
|
||||
|
||||
|
||||
==== tests/cases/compiler/missingRequiredDeclare.d.ts (2 errors) ====
|
||||
==== tests/cases/compiler/missingRequiredDeclare.d.ts (1 errors) ====
|
||||
var x = 1;
|
||||
~~~
|
||||
!!! error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file.
|
||||
~
|
||||
!!! error TS1039: Initializers are not allowed in ambient contexts.
|
||||
!!! error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file.
|
|
@ -1,10 +1,7 @@
|
|||
tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts(1,1): error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file.
|
||||
tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts(1,7): error TS1039: Initializers are not allowed in ambient contexts.
|
||||
|
||||
|
||||
==== tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts (2 errors) ====
|
||||
==== tests/cases/conformance/parser/ecmascript5/Statements/parserVariableStatement1.d.ts (1 errors) ====
|
||||
var v = 1;
|
||||
~~~
|
||||
!!! error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file.
|
||||
~
|
||||
!!! error TS1039: Initializers are not allowed in ambient contexts.
|
||||
!!! error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file.
|
1
tests/cases/compiler/ambientErrors1.ts
Normal file
1
tests/cases/compiler/ambientErrors1.ts
Normal file
|
@ -0,0 +1 @@
|
|||
declare var x = 4;
|
Loading…
Reference in a new issue