Parser errors for destructuring declarations

This commit is contained in:
Anders Hejlsberg 2014-11-11 09:24:49 -08:00
parent 3cda261558
commit bd65f16e06
4 changed files with 63 additions and 20 deletions

View file

@ -124,6 +124,8 @@ module ts {
Tagged_templates_are_only_available_when_targeting_ECMAScript_6_and_higher: { code: 1159, category: DiagnosticCategory.Error, key: "Tagged templates are only available when targeting ECMAScript 6 and higher." },
Property_destructuring_pattern_expected: { code: 1160, category: DiagnosticCategory.Error, key: "Property destructuring pattern expected." },
Array_element_destructuring_pattern_expected: { code: 1161, category: DiagnosticCategory.Error, key: "Array element destructuring pattern expected." },
A_destructuring_declaration_must_have_an_initializer: { code: 1162, category: DiagnosticCategory.Error, key: "A destructuring declaration must have an initializer." },
Destructuring_declarations_are_not_allowed_in_ambient_contexts: { code: 1163, category: DiagnosticCategory.Error, key: "Destructuring declarations are not allowed in ambient contexts." },
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." },

View file

@ -487,6 +487,14 @@
"category": "Error",
"code": 1161
},
"A destructuring declaration must have an initializer.": {
"category": "Error",
"code": 1162
},
"Destructuring declarations are not allowed in ambient contexts.": {
"category": "Error",
"code": 1163
},
"Duplicate identifier '{0}'.": {
"category": "Error",

View file

@ -3466,11 +3466,25 @@ module ts {
var initializerFirstTokenLength = scanner.getTextPos() - initializerStart;
node.initializer = parseInitializer(/*inParameter*/ false, noIn);
if (inAmbientContext && node.initializer && errorCountBeforeVariableDeclaration === file.syntacticErrors.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 (errorCountBeforeVariableDeclaration === file.syntacticErrors.length) {
if (inAmbientContext) {
if (isBindingPattern(node.name)) {
grammarErrorOnNode(node, Diagnostics.Destructuring_declarations_are_not_allowed_in_ambient_contexts);
}
else if (node.initializer) {
grammarErrorAtPos(initializerStart, initializerFirstTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
}
}
else {
if (!node.initializer) {
if (isBindingPattern(node.name)) {
grammarErrorOnNode(node, Diagnostics.A_destructuring_declaration_must_have_an_initializer);
}
else if (flags & NodeFlags.Const) {
grammarErrorOnNode(node, Diagnostics.const_declarations_must_be_initialized);
}
}
}
}
return finishNode(node);
}

View file

@ -38,26 +38,45 @@ module ts.NavigationBar {
return indent;
}
function getChildNodes(nodes: Node[]): Node[] {
var childNodes: Node[] = [];
var childNodes: Node[] = [];
for (var i = 0, n = nodes.length; i < n; i++) {
var node = nodes[i];
if (node.kind === SyntaxKind.ClassDeclaration ||
node.kind === SyntaxKind.EnumDeclaration ||
node.kind === SyntaxKind.InterfaceDeclaration ||
node.kind === SyntaxKind.ModuleDeclaration ||
node.kind === SyntaxKind.FunctionDeclaration) {
childNodes.push(node);
}
else if (node.kind === SyntaxKind.VariableStatement) {
childNodes.push.apply(childNodes, (<VariableStatement>node).declarations);
function visit(node: Node) {
switch (node.kind) {
case SyntaxKind.VariableStatement:
forEach((<VariableStatement>node).declarations, visit);
break;
case SyntaxKind.VariableDeclaration:
if (isBindingPattern(node)) {
forEach((<BindingPattern>(<VariableDeclaration>node).name).elements, visit);
break;
}
case SyntaxKind.ClassDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.FunctionDeclaration:
childNodes.push(node);
}
}
//for (var i = 0, n = nodes.length; i < n; i++) {
// var node = nodes[i];
// if (node.kind === SyntaxKind.ClassDeclaration ||
// node.kind === SyntaxKind.EnumDeclaration ||
// node.kind === SyntaxKind.InterfaceDeclaration ||
// node.kind === SyntaxKind.ModuleDeclaration ||
// node.kind === SyntaxKind.FunctionDeclaration) {
// childNodes.push(node);
// }
// else if (node.kind === SyntaxKind.VariableStatement) {
// childNodes.push.apply(childNodes, (<VariableStatement>node).declarations);
// }
//}
forEach(nodes, visit);
return sortNodes(childNodes);
}