Skip to content

Commit 05dc934

Browse files
Merge pull request #1349 from Microsoft/propertyAssignments
Property assignments
2 parents 1551c0a + c735ccc commit 05dc934

File tree

69 files changed

+856
-806
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+856
-806
lines changed

src/compiler/binder.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,12 @@ module ts {
411411
bindDeclaration(<Declaration>node, SymbolFlags.ConstructSignature, 0, /*isBlockScopeContainer*/ true);
412412
break;
413413
case SyntaxKind.Method:
414-
bindDeclaration(<Declaration>node, SymbolFlags.Method, SymbolFlags.MethodExcludes, /*isBlockScopeContainer*/ true);
414+
// If this is an ObjectLiteralExpression method, then it sits in the same space
415+
// as other properties in the object literal. So we use SymbolFlags.PropertyExcludes
416+
// so that it will conflict with any other object literal members with the same
417+
// name.
418+
bindDeclaration(<Declaration>node, SymbolFlags.Method,
419+
isObjectLiteralMethod(node) ? SymbolFlags.PropertyExcludes : SymbolFlags.MethodExcludes, /*isBlockScopeContainer*/ true);
415420
break;
416421
case SyntaxKind.IndexSignature:
417422
bindDeclaration(<Declaration>node, SymbolFlags.IndexSignature, 0, /*isBlockScopeContainer*/ false);

src/compiler/checker.ts

Lines changed: 120 additions & 68 deletions
Large diffs are not rendered by default.

src/compiler/emitter.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,30 @@ module ts {
22272227
emit(node.expression);
22282228
write("]");
22292229
}
2230+
2231+
function emitDownlevelMethod(node: MethodDeclaration) {
2232+
if (!isObjectLiteralMethod(node)) {
2233+
return;
2234+
}
2235+
2236+
emitLeadingComments(node);
2237+
emit(node.name);
2238+
write(": ");
2239+
write("function ");
2240+
emitSignatureAndBody(node);
2241+
emitTrailingComments(node);
2242+
}
2243+
2244+
function emitMethod(node: MethodDeclaration) {
2245+
if (!isObjectLiteralMethod(node)) {
2246+
return;
2247+
}
2248+
2249+
emitLeadingComments(node);
2250+
emit(node.name);
2251+
emitSignatureAndBody(node);
2252+
emitTrailingComments(node);
2253+
}
22302254

22312255
function emitPropertyAssignment(node: PropertyDeclaration) {
22322256
emitLeadingComments(node);
@@ -2236,7 +2260,7 @@ module ts {
22362260
emitTrailingComments(node);
22372261
}
22382262

2239-
function emitDownlevelShorthandPropertyAssignment(node: ShorthandPropertyDeclaration) {
2263+
function emitDownlevelShorthandPropertyAssignment(node: ShorthandPropertyAssignment) {
22402264
emitLeadingComments(node);
22412265
// Emit identifier as an identifier
22422266
emit(node.name);
@@ -2247,7 +2271,7 @@ module ts {
22472271
emitTrailingComments(node);
22482272
}
22492273

2250-
function emitShorthandPropertyAssignment(node: ShorthandPropertyDeclaration) {
2274+
function emitShorthandPropertyAssignment(node: ShorthandPropertyAssignment) {
22512275
// If short-hand property has a prefix, then regardless of the target version, we will emit it as normal property assignment. For example:
22522276
// module m {
22532277
// export var y;
@@ -2841,17 +2865,17 @@ module ts {
28412865
scopeEmitStart(node);
28422866
increaseIndent();
28432867

2844-
emitDetachedComments(node.body.kind === SyntaxKind.FunctionBlock ? (<Block>node.body).statements : node.body);
2868+
emitDetachedComments(node.body.kind === SyntaxKind.Block ? (<Block>node.body).statements : node.body);
28452869

28462870
var startIndex = 0;
2847-
if (node.body.kind === SyntaxKind.FunctionBlock) {
2871+
if (node.body.kind === SyntaxKind.Block) {
28482872
startIndex = emitDirectivePrologues((<Block>node.body).statements, /*startWithNewLine*/ true);
28492873
}
28502874
var outPos = writer.getTextPos();
28512875
emitCaptureThisForNodeIfNecessary(node);
28522876
emitDefaultValueAssignments(node);
28532877
emitRestParameter(node);
2854-
if (node.body.kind !== SyntaxKind.FunctionBlock && outPos === writer.getTextPos()) {
2878+
if (node.body.kind !== SyntaxKind.Block && outPos === writer.getTextPos()) {
28552879
decreaseIndent();
28562880
write(" ");
28572881
emitStart(node.body);
@@ -2864,7 +2888,7 @@ module ts {
28642888
emitEnd(node.body);
28652889
}
28662890
else {
2867-
if (node.body.kind === SyntaxKind.FunctionBlock) {
2891+
if (node.body.kind === SyntaxKind.Block) {
28682892
emitLinesStartingAt((<Block>node.body).statements, startIndex);
28692893
}
28702894
else {
@@ -2876,7 +2900,7 @@ module ts {
28762900
emitTrailingComments(node.body);
28772901
}
28782902
writeLine();
2879-
if (node.body.kind === SyntaxKind.FunctionBlock) {
2903+
if (node.body.kind === SyntaxKind.Block) {
28802904
emitLeadingCommentsOfPosition((<Block>node.body).statements.end);
28812905
decreaseIndent();
28822906
emitToken(SyntaxKind.CloseBraceToken, (<Block>node.body).statements.end);
@@ -3566,7 +3590,6 @@ module ts {
35663590
case SyntaxKind.Block:
35673591
case SyntaxKind.TryBlock:
35683592
case SyntaxKind.FinallyBlock:
3569-
case SyntaxKind.FunctionBlock:
35703593
case SyntaxKind.ModuleBlock:
35713594
return emitBlock(<Block>node);
35723595
case SyntaxKind.VariableStatement:
@@ -3628,15 +3651,19 @@ module ts {
36283651
// Emit node down-level
36293652
switch (node.kind) {
36303653
case SyntaxKind.ShorthandPropertyAssignment:
3631-
return emitDownlevelShorthandPropertyAssignment(<ShorthandPropertyDeclaration>node);
3654+
return emitDownlevelShorthandPropertyAssignment(<ShorthandPropertyAssignment>node);
3655+
case SyntaxKind.Method:
3656+
return emitDownlevelMethod(<MethodDeclaration>node);
36323657
}
36333658
}
36343659
else {
36353660
// Emit node natively
36363661
Debug.assert(compilerOptions.target >= ScriptTarget.ES6, "Invalid ScriptTarget. We should emit as ES6 or above");
36373662
switch (node.kind) {
36383663
case SyntaxKind.ShorthandPropertyAssignment:
3639-
return emitShorthandPropertyAssignment(<ShorthandPropertyDeclaration>node);
3664+
return emitShorthandPropertyAssignment(<ShorthandPropertyAssignment>node);
3665+
case SyntaxKind.Method:
3666+
return emitMethod(<MethodDeclaration>node);
36403667
}
36413668
}
36423669
}

0 commit comments

Comments
 (0)