Skip to content

Commit 88933d5

Browse files
author
Yui T
committed
Address code review
1 parent e573461 commit 88933d5

8 files changed

+77
-79
lines changed

src/compiler/emitter.ts

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,7 +2630,6 @@ module ts {
26302630
}
26312631

26322632
function emitSuper(node: Node) {
2633-
debugger;
26342633
if (languageVersion >= ScriptTarget.ES6) {
26352634
write("super");
26362635
}
@@ -2639,9 +2638,6 @@ module ts {
26392638
if (flags & NodeCheckFlags.SuperInstance) {
26402639
write("_super.prototype");
26412640
}
2642-
else if ((flags & NodeCheckFlags.SuperStatic) || (node.parent.kind === SyntaxKind.Constructor)) {
2643-
write("_super");
2644-
}
26452641
else {
26462642
write("_super");
26472643
}
@@ -4403,23 +4399,7 @@ module ts {
44034399
emitComputedPropertyName(<ComputedPropertyName>memberName);
44044400
}
44054401
else {
4406-
// For ES6 and above, we want to emit memberName by itself without prefix ".",
4407-
// For ES5 and below, we want to prefix memberName with ".". For example,
4408-
// Typescript:
4409-
// class C {
4410-
// x = 10;
4411-
// foo () {}
4412-
// }
4413-
// Javascript:
4414-
// var C = (function () {
4415-
// function C() {
4416-
// this.x = 10; // Property "x" need to be prefixed with "."
4417-
// }
4418-
// C.prototype.foo = function() {}; // Similarly property "foo" need to be prefixed with "."
4419-
// }
4420-
if (languageVersion < ScriptTarget.ES6 || memberName.parent.kind === SyntaxKind.PropertyDeclaration) {
4421-
write(".");
4422-
}
4402+
write(".");
44234403
emitNodeWithoutSourceMap(memberName);
44244404
}
44254405
}
@@ -4458,14 +4438,18 @@ module ts {
44584438
writeLine();
44594439
emitLeadingComments(member);
44604440
emitStart(member);
4441+
emitStart((<MethodDeclaration>member).name);
44614442
emitDeclarationName(node);
44624443
if (!(member.flags & NodeFlags.Static)) {
44634444
write(".prototype");
44644445
}
44654446
emitMemberAccessForPropertyName((<MethodDeclaration>member).name);
4447+
emitEnd((<MethodDeclaration>member).name);
44664448
write(" = ");
4449+
emitStart(member);
44674450
emitFunctionDeclaration(<MethodDeclaration>member);
44684451
emitEnd(member);
4452+
emitEnd(member);
44694453
write(";");
44704454
emitTrailingComments(member);
44714455
}
@@ -4475,12 +4459,14 @@ module ts {
44754459
writeLine();
44764460
emitStart(member);
44774461
write("Object.defineProperty(");
4462+
emitStart((<AccessorDeclaration>member).name);
44784463
emitDeclarationName(node);
44794464
if (!(member.flags & NodeFlags.Static)) {
44804465
write(".prototype");
44814466
}
44824467
write(", ");
44834468
emitExpressionForPropertyName((<AccessorDeclaration>member).name);
4469+
emitEnd((<AccessorDeclaration>member).name);
44844470
write(", {");
44854471
increaseIndent();
44864472
if (accessors.getAccessor) {
@@ -4531,14 +4517,15 @@ module ts {
45314517
if (member.flags & NodeFlags.Static) {
45324518
write("static ");
45334519
}
4534-
emitMemberAccessForPropertyName((<MethodDeclaration>member).name);
4520+
emit((<MethodDeclaration>member).name);
45354521
emitSignatureAndBody(<MethodDeclaration>member);
45364522
emitEnd(member);
45374523
emitTrailingComments(member);
45384524
}
45394525
else if (member.kind === SyntaxKind.GetAccessor || member.kind === SyntaxKind.SetAccessor) {
45404526
var accessors = getAllAccessorDeclarations(node.members, <AccessorDeclaration>member);
45414527
if (member === accessors.firstAccessor) {
4528+
writeLine();
45424529
if (accessors.getAccessor) {
45434530
writeLine();
45444531
emitLeadingComments(accessors.getAccessor);
@@ -4547,7 +4534,7 @@ module ts {
45474534
write("static ");
45484535
}
45494536
write("get ");
4550-
emitMemberAccessForPropertyName((<MethodDeclaration>member).name);
4537+
emit((<MethodDeclaration>member).name);
45514538
emitSignatureAndBody(accessors.getAccessor);
45524539
emitEnd(accessors.getAccessor);
45534540
emitTrailingComments(accessors.getAccessor);
@@ -4561,7 +4548,7 @@ module ts {
45614548
write("static ");
45624549
}
45634550
write("set ");
4564-
emitMemberAccessForPropertyName((<MethodDeclaration>member).name);
4551+
emit((<MethodDeclaration>member).name);
45654552
emitSignatureAndBody(accessors.setAccessor);
45664553
emitEnd(accessors.setAccessor);
45674554
emitTrailingComments(accessors.setAccessor);;
@@ -4572,42 +4559,42 @@ module ts {
45724559
}
45734560

45744561
function emitConstructor(node: ClassDeclaration, baseTypeNode: TypeReferenceNode) {
4575-
debugger;
4576-
var saveTempCount = tempCount;
4577-
var saveTempVariables = tempVariables;
4578-
var saveTempParameters = tempParameters;
4562+
let saveTempCount = tempCount;
4563+
let saveTempVariables = tempVariables;
4564+
let saveTempParameters = tempParameters;
45794565
tempCount = 0;
45804566
tempVariables = undefined;
45814567
tempParameters = undefined;
45824568

4583-
var popFrame = enterNameScope();
4569+
let popFrame = enterNameScope();
45844570
// Check if we have property assignment inside class declaration.
45854571
// If there is property assignment, we need to emit constructor whether users define it or not
45864572
// If there is no property assignment, we can omit constructor if users do not define it
4587-
var hasPropertyAssignment = false;
4573+
let hasInstancePropertyWithInitializer = false;
45884574

45894575
// Emit the constructor overload pinned comments
45904576
forEach(node.members, member => {
45914577
if (member.kind === SyntaxKind.Constructor && !(<ConstructorDeclaration>member).body) {
45924578
emitPinnedOrTripleSlashComments(member);
45934579
}
4594-
if (member.kind === SyntaxKind.PropertyDeclaration && (<PropertyDeclaration>member).initializer) {
4595-
hasPropertyAssignment = true;
4580+
// Check if there is any non-static property assignment
4581+
if (member.kind === SyntaxKind.PropertyDeclaration && (<PropertyDeclaration>member).initializer && (member.flags & NodeFlags.Static) === 0) {
4582+
hasInstancePropertyWithInitializer = true;
45964583
}
45974584
});
45984585

4599-
var ctor = getFirstConstructorWithBody(node);
4586+
let ctor = getFirstConstructorWithBody(node);
46004587

46014588
// For target ES6 and above, if there is no user-defined constructor and there is no property assignment
46024589
// do not emit constructor in class declaration.
4603-
if (languageVersion >= ScriptTarget.ES6 && !ctor && !hasPropertyAssignment) {
4590+
if (languageVersion >= ScriptTarget.ES6 && !ctor && !hasInstancePropertyWithInitializer) {
46044591
return;
46054592
}
46064593

46074594
if (ctor) {
46084595
emitLeadingComments(ctor);
46094596
}
4610-
emitStart(<Node>ctor || node);
4597+
emitStart(ctor || node);
46114598

46124599
if (languageVersion < ScriptTarget.ES6) {
46134600
write("function ");
@@ -4639,7 +4626,7 @@ module ts {
46394626
scopeEmitStart(node, "constructor");
46404627
increaseIndent();
46414628
if (ctor) {
4642-
emitDetachedComments((<Block>ctor.body).statements);
4629+
emitDetachedComments(ctor.body.statements);
46434630
}
46444631
emitCaptureThisForNodeIfNecessary(node);
46454632
if (ctor) {
@@ -4662,10 +4649,12 @@ module ts {
46624649
emitEnd(baseTypeNode);
46634650
}
46644651
}
4665-
emitMemberAssignments(node, /*nonstatic*/0);
4652+
emitMemberAssignments(node, /*staticFlag*/0);
46664653
if (ctor) {
46674654
var statements: Node[] = (<Block>ctor.body).statements;
4668-
if (superCall) statements = statements.slice(1);
4655+
if (superCall) {
4656+
statements = statements.slice(1);
4657+
}
46694658
emitLines(statements);
46704659
}
46714660
emitTempDeclarations(/*newLine*/ true);
@@ -4696,6 +4685,7 @@ module ts {
46964685
write("default ");
46974686
}
46984687
}
4688+
46994689
write("class ");
47004690
emitDeclarationName(node);
47014691
var baseTypeNode = getClassBaseTypeNode(node);

src/compiler/parser.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4518,8 +4518,8 @@ module ts {
45184518

45194519
function parseClassDeclaration(fullStart: number, modifiers: ModifiersArray): ClassDeclaration {
45204520
// In ES6 specification, All parts of a ClassDeclaration or a ClassExpression are strict mode code
4521+
let savedStrictModeContext = inStrictModeContext();
45214522
if (languageVersion >= ScriptTarget.ES6) {
4522-
var savedStrictModeContext = inStrictModeContext();
45234523
setStrictModeContext(true);
45244524
}
45254525

@@ -4545,13 +4545,8 @@ module ts {
45454545
}
45464546

45474547
var finishedNode = finishNode(node);
4548-
if (languageVersion >= ScriptTarget.ES6) {
4549-
setStrictModeContext(savedStrictModeContext);
4550-
return finishedNode;
4551-
}
4552-
else {
4553-
return finishedNode;
4554-
}
4548+
setStrictModeContext(savedStrictModeContext);
4549+
return finishedNode;
45554550
}
45564551

45574552
function parseHeritageClauses(isClassHeritageClause: boolean): NodeArray<HeritageClause> {

tests/baselines/reference/computedPropertyNamesWithStaticProperty.errors.txt

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/es6/computedProperties/computedPropertyNamesWithStaticProperty.ts ===
2+
class C {
3+
>C : C
4+
5+
static staticProp = 10;
6+
>staticProp : number
7+
8+
get [C.staticProp]() {
9+
>C.staticProp : number
10+
>C : typeof C
11+
>staticProp : number
12+
13+
return "hello";
14+
}
15+
set [C.staticProp](x: string) {
16+
>C.staticProp : number
17+
>C : typeof C
18+
>staticProp : number
19+
>x : string
20+
21+
var y = x;
22+
>y : string
23+
>x : string
24+
}
25+
[C.staticProp]() { }
26+
>C.staticProp : number
27+
>C : typeof C
28+
>staticProp : number
29+
}

tests/baselines/reference/properties.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/properties.sourcemap.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ sourceFile:properties.ts
4343
---
4444
>>> Object.defineProperty(MyClass.prototype, "Count", {
4545
1->^^^^
46-
2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
2 > ^^^^^^^^^^^^^^^^^^^^^^
47+
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^
4748
1->
48-
2 > Count
49-
1->Emitted(4, 5) Source(4, 16) + SourceIndex(0) name (MyClass)
50-
2 >Emitted(4, 53) Source(4, 21) + SourceIndex(0) name (MyClass)
49+
2 > public get
50+
3 > Count
51+
1->Emitted(4, 5) Source(4, 5) + SourceIndex(0) name (MyClass)
52+
2 >Emitted(4, 27) Source(4, 16) + SourceIndex(0) name (MyClass)
53+
3 >Emitted(4, 53) Source(4, 21) + SourceIndex(0) name (MyClass)
5154
---
5255
>>> get: function () {
5356
1 >^^^^^^^^^^^^^

tests/baselines/reference/sourceMapValidationClass.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/sourceMapValidationClass.sourcemap.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,15 @@ sourceFile:sourceMapValidationClass.ts
223223
---
224224
>>> Object.defineProperty(Greeter.prototype, "greetings", {
225225
1->^^^^
226-
2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
226+
2 > ^^^^^^^^^^^^^^^^^^^^^^
227+
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
227228
1->
228-
> get
229-
2 > greetings
230-
1->Emitted(16, 5) Source(12, 9) + SourceIndex(0) name (Greeter)
231-
2 >Emitted(16, 57) Source(12, 18) + SourceIndex(0) name (Greeter)
229+
>
230+
2 > get
231+
3 > greetings
232+
1->Emitted(16, 5) Source(12, 5) + SourceIndex(0) name (Greeter)
233+
2 >Emitted(16, 27) Source(12, 9) + SourceIndex(0) name (Greeter)
234+
3 >Emitted(16, 57) Source(12, 18) + SourceIndex(0) name (Greeter)
232235
---
233236
>>> get: function () {
234237
1 >^^^^^^^^^^^^^

0 commit comments

Comments
 (0)