Skip to content

Commit 8a779e1

Browse files
author
Yui T
committed
Basic typechecking and emitting for short hand property assignment
Conflicts: src/compiler/diagnosticInformationMap.generated.ts src/compiler/emitter.ts
1 parent 150e8d3 commit 8a779e1

11 files changed

+105
-3
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ module ts {
350350
break;
351351
case SyntaxKind.Property:
352352
case SyntaxKind.PropertyAssignment:
353+
case SyntaxKind.ShortHandPropertyAssignment:
353354
bindDeclaration(<Declaration>node, SymbolFlags.Property, SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false);
354355
break;
355356
case SyntaxKind.EnumMember:

src/compiler/checker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4968,7 +4968,14 @@ module ts {
49684968
if (hasProperty(members, id)) {
49694969
var member = members[id];
49704970
if (member.flags & SymbolFlags.Property) {
4971-
var type = checkExpression((<PropertyDeclaration>member.declarations[0]).initializer, contextualMapper);
4971+
var memberDecl = <PropertyDeclaration>member.declarations[0];
4972+
var type: Type;
4973+
if (memberDecl.kind === SyntaxKind.PropertyAssignment) {
4974+
type = checkExpression(memberDecl.initializer, contextualMapper);
4975+
}
4976+
else {
4977+
type = checkExpression(memberDecl.name, contextualMapper);
4978+
}
49724979
var prop = <TransientSymbol>createSymbol(SymbolFlags.Property | SymbolFlags.Transient | member.flags, member.name);
49734980
prop.declarations = member.declarations;
49744981
prop.parent = member.parent;

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ module ts {
122122
let_declarations_can_only_be_declared_inside_a_block: { code: 1157, category: DiagnosticCategory.Error, key: "'let' declarations can only be declared inside a block." },
123123
Invalid_template_literal_expected: { code: 1158, category: DiagnosticCategory.Error, key: "Invalid template literal; expected '}'" },
124124
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." },
125+
A_object_member_cannot_be_declared_optional: { code: 1159, category: DiagnosticCategory.Error, key: "A object member cannot be declared optional." },
125126
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
126127
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." },
127128
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,11 @@
480480
"code": 1159
481481
},
482482

483+
"A object member cannot be declared optional.": {
484+
"category": "Error",
485+
"code": 1159
486+
},
487+
483488
"Duplicate identifier '{0}'.": {
484489
"category": "Error",
485490
"code": 2300

src/compiler/emitter.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,16 @@ module ts {
10331033
emitTrailingComments(node);
10341034
}
10351035

1036+
function emitShortHandPropertyAssignment(node: ShortHandPropertyDeclaration) {
1037+
emitLeadingComments(node);
1038+
emit(node.name);
1039+
if (compilerOptions.target !== ScriptTarget.ES6) {
1040+
write(": ");
1041+
emit(node.name);
1042+
}
1043+
emitTrailingComments(node);
1044+
}
1045+
10361046
function tryEmitConstantValue(node: PropertyAccess | IndexedAccess): boolean {
10371047
var constantValue = resolver.getConstantValue(node);
10381048
if (constantValue !== undefined) {
@@ -2250,6 +2260,8 @@ module ts {
22502260
return emitObjectLiteral(<ObjectLiteral>node);
22512261
case SyntaxKind.PropertyAssignment:
22522262
return emitPropertyAssignment(<PropertyDeclaration>node);
2263+
case SyntaxKind.ShortHandPropertyAssignment:
2264+
return emitShortHandPropertyAssignment(<ShortHandPropertyDeclaration>node);
22532265
case SyntaxKind.PropertyAccess:
22542266
return emitPropertyAccess(<PropertyAccess>node);
22552267
case SyntaxKind.IndexedAccess:

src/compiler/parser.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ module ts {
198198
child((<ParameterDeclaration>node).initializer);
199199
case SyntaxKind.Property:
200200
case SyntaxKind.PropertyAssignment:
201+
case SyntaxKind.ShortHandPropertyAssignment:
201202
return child((<PropertyDeclaration>node).name) ||
202203
child((<PropertyDeclaration>node).type) ||
203204
child((<PropertyDeclaration>node).initializer);
@@ -2669,9 +2670,11 @@ module ts {
26692670

26702671
function parsePropertyAssignment(): PropertyDeclaration {
26712672
var nodePos = scanner.getStartPos();
2673+
var nameToken = token;
26722674
var propertyName = parsePropertyName();
2675+
var node: PropertyDeclaration;
26732676
if (token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) {
2674-
var node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment, nodePos);
2677+
node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment, nodePos);
26752678
node.name = propertyName;
26762679
var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false);
26772680
var body = parseBody(/* ignoreMissingOpenBrace */ false);
@@ -2683,8 +2686,19 @@ module ts {
26832686
node.initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, undefined, sig, body);
26842687
return finishNode(node);
26852688
}
2689+
if (token === SyntaxKind.QuestionToken) {
2690+
var questionStart = scanner.getTokenPos();
2691+
errorAtPos(questionStart, scanner.getStartPos() - questionStart, Diagnostics.A_object_member_cannot_be_declared_optional);
2692+
nextToken();
2693+
}
2694+
2695+
// Parse to check if it is short-hand property assignment or normal property assignment
2696+
if (token !== SyntaxKind.ColonToken && nameToken === SyntaxKind.Identifier) {
2697+
node = <ShortHandPropertyDeclaration>createNode(SyntaxKind.ShortHandPropertyAssignment, nodePos);
2698+
node.name = propertyName;
2699+
}
26862700
else {
2687-
var node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment, nodePos);
2701+
node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment, nodePos);
26882702
node.name = propertyName;
26892703
parseExpected(SyntaxKind.ColonToken);
26902704
node.initializer = parseAssignmentExpression(false);
@@ -2737,6 +2751,9 @@ module ts {
27372751
if (p.kind === SyntaxKind.PropertyAssignment) {
27382752
currentKind = Property;
27392753
}
2754+
else if (p.kind === SyntaxKind.ShortHandPropertyAssignment) {
2755+
currentKind = Property;
2756+
}
27402757
else if (p.kind === SyntaxKind.GetAccessor) {
27412758
currentKind = GetAccessor;
27422759
}

src/compiler/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ module ts {
165165
ArrayLiteral,
166166
ObjectLiteral,
167167
PropertyAssignment,
168+
ShortHandPropertyAssignment,
168169
PropertyAccess,
169170
IndexedAccess,
170171
CallExpression,
@@ -329,6 +330,10 @@ module ts {
329330
initializer?: Expression;
330331
}
331332

333+
export interface ShortHandPropertyDeclaration extends PropertyDeclaration {
334+
name: Identifier;
335+
}
336+
332337
export interface ParameterDeclaration extends VariableDeclaration { }
333338

334339
/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var a, b, c;
2+
3+
var x1 = {
4+
a
5+
};
6+
7+
var x2 = {
8+
a,
9+
}
10+
11+
var x3 = {
12+
a: 0,
13+
b,
14+
c,
15+
d() { },
16+
x3,
17+
parent: x3
18+
};
19+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// errors
2+
var y = {
3+
"stringLiteral",
4+
42,
5+
get e,
6+
set f,
7+
this,
8+
super,
9+
var,
10+
class,
11+
typeof
12+
};
13+
14+
var x = {
15+
a.b,
16+
a["ss"],
17+
a[1],
18+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// module export
2+
3+
module m {
4+
export var x;
5+
}
6+
7+
module m {
8+
var z = x;
9+
var y = {
10+
a: x,
11+
x
12+
};
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var x = {
2+
x, // OK
3+
undefinedVariable // Error
4+
}

0 commit comments

Comments
 (0)