Basic typechecking and emitting for short hand property assignment

Conflicts:
	src/compiler/diagnosticInformationMap.generated.ts
	src/compiler/emitter.ts
This commit is contained in:
Yui T
2014-11-04 14:43:43 -08:00
parent 150e8d30d7
commit 8a779e1e85
11 changed files with 105 additions and 3 deletions

View File

@@ -350,6 +350,7 @@ module ts {
break;
case SyntaxKind.Property:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.ShortHandPropertyAssignment:
bindDeclaration(<Declaration>node, SymbolFlags.Property, SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false);
break;
case SyntaxKind.EnumMember:

View File

@@ -4968,7 +4968,14 @@ module ts {
if (hasProperty(members, id)) {
var member = members[id];
if (member.flags & SymbolFlags.Property) {
var type = checkExpression((<PropertyDeclaration>member.declarations[0]).initializer, contextualMapper);
var memberDecl = <PropertyDeclaration>member.declarations[0];
var type: Type;
if (memberDecl.kind === SyntaxKind.PropertyAssignment) {
type = checkExpression(memberDecl.initializer, contextualMapper);
}
else {
type = checkExpression(memberDecl.name, contextualMapper);
}
var prop = <TransientSymbol>createSymbol(SymbolFlags.Property | SymbolFlags.Transient | member.flags, member.name);
prop.declarations = member.declarations;
prop.parent = member.parent;

View File

@@ -122,6 +122,7 @@ module ts {
let_declarations_can_only_be_declared_inside_a_block: { code: 1157, category: DiagnosticCategory.Error, key: "'let' declarations can only be declared inside a block." },
Invalid_template_literal_expected: { code: 1158, category: DiagnosticCategory.Error, key: "Invalid template literal; expected '}'" },
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." },
A_object_member_cannot_be_declared_optional: { code: 1159, category: DiagnosticCategory.Error, key: "A object member cannot be declared optional." },
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

@@ -480,6 +480,11 @@
"code": 1159
},
"A object member cannot be declared optional.": {
"category": "Error",
"code": 1159
},
"Duplicate identifier '{0}'.": {
"category": "Error",
"code": 2300

View File

@@ -1033,6 +1033,16 @@ module ts {
emitTrailingComments(node);
}
function emitShortHandPropertyAssignment(node: ShortHandPropertyDeclaration) {
emitLeadingComments(node);
emit(node.name);
if (compilerOptions.target !== ScriptTarget.ES6) {
write(": ");
emit(node.name);
}
emitTrailingComments(node);
}
function tryEmitConstantValue(node: PropertyAccess | IndexedAccess): boolean {
var constantValue = resolver.getConstantValue(node);
if (constantValue !== undefined) {
@@ -2250,6 +2260,8 @@ module ts {
return emitObjectLiteral(<ObjectLiteral>node);
case SyntaxKind.PropertyAssignment:
return emitPropertyAssignment(<PropertyDeclaration>node);
case SyntaxKind.ShortHandPropertyAssignment:
return emitShortHandPropertyAssignment(<ShortHandPropertyDeclaration>node);
case SyntaxKind.PropertyAccess:
return emitPropertyAccess(<PropertyAccess>node);
case SyntaxKind.IndexedAccess:

View File

@@ -198,6 +198,7 @@ module ts {
child((<ParameterDeclaration>node).initializer);
case SyntaxKind.Property:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.ShortHandPropertyAssignment:
return child((<PropertyDeclaration>node).name) ||
child((<PropertyDeclaration>node).type) ||
child((<PropertyDeclaration>node).initializer);
@@ -2669,9 +2670,11 @@ module ts {
function parsePropertyAssignment(): PropertyDeclaration {
var nodePos = scanner.getStartPos();
var nameToken = token;
var propertyName = parsePropertyName();
var node: PropertyDeclaration;
if (token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) {
var node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment, nodePos);
node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment, nodePos);
node.name = propertyName;
var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false);
var body = parseBody(/* ignoreMissingOpenBrace */ false);
@@ -2683,8 +2686,19 @@ module ts {
node.initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, undefined, sig, body);
return finishNode(node);
}
if (token === SyntaxKind.QuestionToken) {
var questionStart = scanner.getTokenPos();
errorAtPos(questionStart, scanner.getStartPos() - questionStart, Diagnostics.A_object_member_cannot_be_declared_optional);
nextToken();
}
// Parse to check if it is short-hand property assignment or normal property assignment
if (token !== SyntaxKind.ColonToken && nameToken === SyntaxKind.Identifier) {
node = <ShortHandPropertyDeclaration>createNode(SyntaxKind.ShortHandPropertyAssignment, nodePos);
node.name = propertyName;
}
else {
var node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment, nodePos);
node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment, nodePos);
node.name = propertyName;
parseExpected(SyntaxKind.ColonToken);
node.initializer = parseAssignmentExpression(false);
@@ -2737,6 +2751,9 @@ module ts {
if (p.kind === SyntaxKind.PropertyAssignment) {
currentKind = Property;
}
else if (p.kind === SyntaxKind.ShortHandPropertyAssignment) {
currentKind = Property;
}
else if (p.kind === SyntaxKind.GetAccessor) {
currentKind = GetAccessor;
}

View File

@@ -165,6 +165,7 @@ module ts {
ArrayLiteral,
ObjectLiteral,
PropertyAssignment,
ShortHandPropertyAssignment,
PropertyAccess,
IndexedAccess,
CallExpression,
@@ -329,6 +330,10 @@ module ts {
initializer?: Expression;
}
export interface ShortHandPropertyDeclaration extends PropertyDeclaration {
name: Identifier;
}
export interface ParameterDeclaration extends VariableDeclaration { }
/**