Move grammar checking: variableDeclaration, variableStatements; there are still erros from incomplete grammar migration

This commit is contained in:
Yui T 2014-12-15 18:10:35 -08:00
parent ccff5efb73
commit e0e88adfc5
7 changed files with 97 additions and 24 deletions

View File

@ -8000,7 +8000,10 @@ module ts {
// Grammar checking
// TODO (yuisu) : Revisit this check once move all grammar checking
if (node.kind === SyntaxKind.BindingElement) {
checkGrammarBindingElement(<BindingElement>node);
checkGrammarEvalOrArgumentsInStrictMode(node, <Identifier>node.name);
}
else if (node.kind === SyntaxKind.VariableDeclaration) {
checkGrammarVariableDeclaration(<VariableDeclaration>node);
}
checkSourceElement(node.type);
@ -8059,6 +8062,9 @@ module ts {
}
function checkVariableStatement(node: VariableStatement) {
// Grammar checking
checkGrammarModifiers(node) || checkGrammarVariableDeclarations(node, node.declarations) || checkGrammarForDisallowedLetOrConstStatement(node);
forEach(node.declarations, checkSourceElement);
}
@ -8091,7 +8097,6 @@ module ts {
}
function checkForInStatement(node: ForInStatement) {
// TypeScript 1.0 spec (April 2014): 5.4
// In a 'for-in' statement of the form
// for (var VarDecl in Expr) Statement
@ -10132,12 +10137,6 @@ module ts {
checkGrammarForOmittedArgument(node, arguments);
}
function checkGrammarBindingElement(node: BindingElement) {
// It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code
// and its Identifier is eval or arguments
checkGrammarEvalOrArgumentsInStrictMode(node, <Identifier>node.name);
}
function checkGrammarHeritageClause(node: HeritageClause): boolean {
var types = node.types;
if (checkGrammarForDisallowedTrailingComma(types)) {
@ -10387,6 +10386,80 @@ module ts {
}
}
function checkGrammarVariableDeclaration(node: VariableDeclaration) {
if (isInAmbientContext(node)) {
if (isBindingPattern(node.name)) {
return grammarErrorOnNode(node, Diagnostics.Destructuring_declarations_are_not_allowed_in_ambient_contexts);
}
if (node.initializer) {
// Error on equals token which immediate precedes the initializer
return grammarErrorAtPos(getSourceFileOfNode(node), node.initializer.pos - 1, 1, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
}
}
else {
if (!node.initializer) {
if (isBindingPattern(node.name) && !isBindingPattern(node.parent)) {
return grammarErrorOnNode(node, Diagnostics.A_destructuring_declaration_must_have_an_initializer);
}
if (isConst(node)) {
return grammarErrorOnNode(node, Diagnostics.const_declarations_must_be_initialized);
}
}
}
// It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code
// and its Identifier is eval or arguments
return checkGrammarEvalOrArgumentsInStrictMode(node, <Identifier>node.name);
}
function checkGrammarVariableDeclarations(variableStatement: VariableStatement, declarations: NodeArray<VariableDeclaration>): boolean {
if (declarations) {
if (checkGrammarForDisallowedTrailingComma(declarations)) {
return true;
}
if (!declarations.length) {
return grammarErrorAtPos(getSourceFileOfNode(variableStatement), declarations.pos, declarations.end - declarations.pos, Diagnostics.Variable_declaration_list_cannot_be_empty);
}
var decl = declarations[0];
if (compilerOptions.target < ScriptTarget.ES6) {
if (isLet(decl)) {
return grammarErrorOnFirstToken(decl, Diagnostics.let_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher);
}
else if (isConst(decl)) {
return grammarErrorOnFirstToken(decl, Diagnostics.const_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher);
}
}
}
}
function allowLetAndConstDeclarations(parent: Node): boolean {
switch (parent.kind) {
case SyntaxKind.IfStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.WithStatement:
case SyntaxKind.ForStatement:
case SyntaxKind.ForInStatement:
return false;
case SyntaxKind.LabeledStatement:
return allowLetAndConstDeclarations(parent.parent);
}
return true;
}
function checkGrammarForDisallowedLetOrConstStatement(node: VariableStatement) {
if (!allowLetAndConstDeclarations(node.parent)) {
if (isLet(node)) {
return grammarErrorOnNode(node, Diagnostics.let_declarations_can_only_be_declared_inside_a_block);
}
else if (isConst(node)) {
return grammarErrorOnNode(node, Diagnostics.const_declarations_can_only_be_declared_inside_a_block);
}
}
}
function isIntegerLiteral(expression: Expression): boolean {
if (expression.kind === SyntaxKind.PrefixUnaryExpression) {
var unaryExpression = <PrefixUnaryExpression>expression;

View File

@ -4673,8 +4673,8 @@ module ts {
//case SyntaxKind.TaggedTemplateExpression: return checkTaggedTemplateExpression(<TaggedTemplateExpression>node);
//case SyntaxKind.ThrowStatement: return checkThrowStatement(<ThrowStatement>node);
//case SyntaxKind.TypeReference: return checkTypeReference(<TypeReferenceNode>node);
case SyntaxKind.VariableDeclaration: return checkVariableDeclaration(<VariableDeclaration>node);
case SyntaxKind.VariableStatement: return checkVariableStatement(<VariableStatement>node);
//case SyntaxKind.VariableDeclaration: return checkVariableDeclaration(<VariableDeclaration>node);
//case SyntaxKind.VariableStatement: return checkVariableStatement(<VariableStatement>node);
//case SyntaxKind.WithStatement: return checkWithStatement(<WithStatement>node);
//case SyntaxKind.YieldExpression: return checkYieldExpression(<YieldExpression>node);
}
@ -5333,7 +5333,7 @@ module ts {
case SyntaxKind.ModuleDeclaration:
//case SyntaxKind.EnumDeclaration:
case SyntaxKind.ExportAssignment:
case SyntaxKind.VariableStatement:
//case SyntaxKind.VariableStatement:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.TypeAliasDeclaration:
case SyntaxKind.ImportDeclaration:

View File

@ -1,9 +1,9 @@
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(7,16): error TS1037: A function implementation cannot be declared in an ambient context.
tests/cases/conformance/externalModules/initializersInDeclarations.ts(16,2): 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.

View File

@ -1,9 +1,9 @@
tests/cases/conformance/internalModules/moduleBody/invalidModuleWithVarStatements.ts(4,5): error TS1044: 'public' modifier cannot appear on a module element.
tests/cases/conformance/internalModules/moduleBody/invalidModuleWithVarStatements.ts(8,5): error TS1044: 'public' modifier cannot appear on a module element.
tests/cases/conformance/internalModules/moduleBody/invalidModuleWithVarStatements.ts(12,5): error TS1044: 'static' modifier cannot appear on a module element.
tests/cases/conformance/internalModules/moduleBody/invalidModuleWithVarStatements.ts(16,5): error TS1044: 'static' modifier cannot appear on a module element.
tests/cases/conformance/internalModules/moduleBody/invalidModuleWithVarStatements.ts(20,5): error TS1044: 'private' modifier cannot appear on a module element.
tests/cases/conformance/internalModules/moduleBody/invalidModuleWithVarStatements.ts(25,5): error TS1044: 'private' modifier cannot appear on a module element.
tests/cases/conformance/internalModules/moduleBody/invalidModuleWithVarStatements.ts(4,5): error TS1044: 'public' modifier cannot appear on a module element.
tests/cases/conformance/internalModules/moduleBody/invalidModuleWithVarStatements.ts(12,5): error TS1044: 'static' modifier cannot appear on a module element.
tests/cases/conformance/internalModules/moduleBody/invalidModuleWithVarStatements.ts(20,5): error TS1044: 'private' modifier cannot appear on a module element.
==== tests/cases/conformance/internalModules/moduleBody/invalidModuleWithVarStatements.ts (6 errors) ====

View File

@ -1,11 +1,11 @@
tests/cases/compiler/letDeclarations-es5.ts(10,9): error TS1153: 'let' declarations are only available when targeting ECMAScript 6 and higher.
tests/cases/compiler/letDeclarations-es5.ts(12,9): error TS1153: 'let' declarations are only available when targeting ECMAScript 6 and higher.
tests/cases/compiler/letDeclarations-es5.ts(2,5): error TS1153: 'let' declarations are only available when targeting ECMAScript 6 and higher.
tests/cases/compiler/letDeclarations-es5.ts(3,5): error TS1153: 'let' declarations are only available when targeting ECMAScript 6 and higher.
tests/cases/compiler/letDeclarations-es5.ts(4,5): error TS1153: 'let' declarations are only available when targeting ECMAScript 6 and higher.
tests/cases/compiler/letDeclarations-es5.ts(6,5): error TS1153: 'let' declarations are only available when targeting ECMAScript 6 and higher.
tests/cases/compiler/letDeclarations-es5.ts(7,5): error TS1153: 'let' declarations are only available when targeting ECMAScript 6 and higher.
tests/cases/compiler/letDeclarations-es5.ts(8,5): error TS1153: 'let' declarations are only available when targeting ECMAScript 6 and higher.
tests/cases/compiler/letDeclarations-es5.ts(10,9): error TS1153: 'let' declarations are only available when targeting ECMAScript 6 and higher.
tests/cases/compiler/letDeclarations-es5.ts(12,9): error TS1153: 'let' declarations are only available when targeting ECMAScript 6 and higher.
==== tests/cases/compiler/letDeclarations-es5.ts (8 errors) ====

View File

@ -1,4 +1,6 @@
tests/cases/compiler/varBlock.ts(8,16): error TS1039: Initializers are not allowed in ambient contexts.
tests/cases/compiler/varBlock.ts(11,22): error TS2369: A parameter property is only allowed in a constructor implementation.
tests/cases/compiler/varBlock.ts(11,22): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
tests/cases/compiler/varBlock.ts(15,15): error TS1039: Initializers are not allowed in ambient contexts.
tests/cases/compiler/varBlock.ts(21,16): error TS1039: Initializers are not allowed in ambient contexts.
tests/cases/compiler/varBlock.ts(22,20): error TS1039: Initializers are not allowed in ambient contexts.
@ -14,10 +16,8 @@ tests/cases/compiler/varBlock.ts(33,25): error TS1039: Initializers are not allo
tests/cases/compiler/varBlock.ts(34,19): error TS1039: Initializers are not allowed in ambient contexts.
tests/cases/compiler/varBlock.ts(35,25): error TS1039: Initializers are not allowed in ambient contexts.
tests/cases/compiler/varBlock.ts(35,35): error TS1039: Initializers are not allowed in ambient contexts.
tests/cases/compiler/varBlock.ts(39,15): error TS1039: Initializers are not allowed in ambient contexts.
tests/cases/compiler/varBlock.ts(11,22): error TS2369: A parameter property is only allowed in a constructor implementation.
tests/cases/compiler/varBlock.ts(11,22): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
tests/cases/compiler/varBlock.ts(39,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'c' must be of type 'any', but here has type 'number'.
tests/cases/compiler/varBlock.ts(39,15): error TS1039: Initializers are not allowed in ambient contexts.
==== tests/cases/compiler/varBlock.ts (20 errors) ====
@ -96,7 +96,7 @@ tests/cases/compiler/varBlock.ts(39,13): error TS2403: Subsequent variable decla
declare var c;
declare var c = 10;
~
!!! error TS1039: Initializers are not allowed in ambient contexts.
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'c' must be of type 'any', but here has type 'number'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'c' must be of type 'any', but here has type 'number'.
~
!!! error TS1039: Initializers are not allowed in ambient contexts.

View File

@ -1,5 +1,5 @@
tests/cases/compiler/variableDeclarationInStrictMode1.ts(2,5): error TS1100: Invalid use of 'eval' in strict mode.
lib.d.ts(29,18): error TS2300: Duplicate identifier 'eval'.
tests/cases/compiler/variableDeclarationInStrictMode1.ts(2,5): error TS1100: Invalid use of 'eval' in strict mode.
tests/cases/compiler/variableDeclarationInStrictMode1.ts(2,5): error TS2300: Duplicate identifier 'eval'.