Skip to content

Commit 44d7a9a

Browse files
author
Josh Goldberg
committed
Handled comments after super, with tests
1 parent b250f5f commit 44d7a9a

File tree

6 files changed

+180
-47
lines changed

6 files changed

+180
-47
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ namespace ts {
15261526
)
15271527
);
15281528
insertStatementAfterCustomPrologue(statements, assignSuperExpression);
1529-
setCommentRange(assignSuperExpression, getOriginalNode(superExpression));
1529+
setCommentRange(assignSuperExpression, getOriginalNode(superExpression).parent);
15301530
}
15311531

15321532
function insertCaptureThisForNode(statements: Statement[], node: Node, initializer: Expression | undefined): void {

tests/baselines/reference/derivedClassSuperStatementPosition.errors.txt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(12,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
22
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(22,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
3-
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(45,15): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
4-
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(54,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
5-
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(66,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
6-
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(75,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
3+
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(45,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
4+
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(60,15): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
5+
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(69,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
6+
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(81,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
7+
tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts(90,13): error TS2798: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
78

89

9-
==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts (6 errors) ====
10+
==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts (7 errors) ====
1011
class DerivedBasic extends Object {
1112
prop = 1;
1213
constructor() {
@@ -42,12 +43,29 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS
4243
x: any;
4344
constructor() {
4445
// c1
45-
console.log();
46-
// c2
47-
super();
46+
console.log(); // c2
47+
// c3
48+
super(); // c4
49+
// c5
50+
this.x = null; // c6
51+
// c7
52+
}
53+
}
54+
55+
class DerivedCommentsInvalidThis extends Object {
56+
x: any;
57+
constructor() {
58+
// c0
59+
this;
60+
~~~~
61+
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
62+
// c1
63+
console.log(); // c2
4864
// c3
49-
this.x = null;
50-
// c4
65+
super(); // c4
66+
// c5
67+
this.x = null; // c6
68+
// c7
5169
}
5270
}
5371

tests/baselines/reference/derivedClassSuperStatementPosition.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,27 @@ class DerivedComments extends Object {
3030
x: any;
3131
constructor() {
3232
// c1
33-
console.log();
34-
// c2
35-
super();
33+
console.log(); // c2
34+
// c3
35+
super(); // c4
36+
// c5
37+
this.x = null; // c6
38+
// c7
39+
}
40+
}
41+
42+
class DerivedCommentsInvalidThis extends Object {
43+
x: any;
44+
constructor() {
45+
// c0
46+
this;
47+
// c1
48+
console.log(); // c2
3649
// c3
37-
this.x = null;
38-
// c4
50+
super(); // c4
51+
// c5
52+
this.x = null; // c6
53+
// c7
3954
}
4055
}
4156

@@ -138,16 +153,33 @@ var DerivedComments = /** @class */ (function (_super) {
138153
function DerivedComments() {
139154
var _this = this;
140155
// c1
141-
console.log();
142-
// c2
143-
_this = _super.call(this) || this;
156+
console.log(); // c2
144157
// c3
145-
_this.x = null;
158+
_this = _super.call(this) || this; // c4
159+
// c5
160+
_this.x = null; // c6
146161
return _this;
147-
// c4
162+
// c7
148163
}
149164
return DerivedComments;
150165
}(Object));
166+
var DerivedCommentsInvalidThis = /** @class */ (function (_super) {
167+
__extends(DerivedCommentsInvalidThis, _super);
168+
function DerivedCommentsInvalidThis() {
169+
var _this = this;
170+
// c0
171+
_this;
172+
// c1
173+
console.log(); // c2
174+
// c3
175+
_this = _super.call(this) || this; // c4
176+
// c5
177+
_this.x = null; // c6
178+
return _this;
179+
// c7
180+
}
181+
return DerivedCommentsInvalidThis;
182+
}(Object));
151183
var DerivedInConditional = /** @class */ (function (_super) {
152184
__extends(DerivedInConditional, _super);
153185
function DerivedInConditional() {

tests/baselines/reference/derivedClassSuperStatementPosition.symbols

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,63 @@ class DerivedComments extends Object {
8383

8484
constructor() {
8585
// c1
86-
console.log();
86+
console.log(); // c2
8787
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
8888
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
8989
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
9090

91-
// c2
92-
super();
91+
// c3
92+
super(); // c4
9393
>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --))
9494

95-
// c3
96-
this.x = null;
95+
// c5
96+
this.x = null; // c6
9797
>this.x : Symbol(DerivedComments.x, Decl(derivedClassSuperStatementPosition.ts, 27, 38))
9898
>this : Symbol(DerivedComments, Decl(derivedClassSuperStatementPosition.ts, 25, 1))
9999
>x : Symbol(DerivedComments.x, Decl(derivedClassSuperStatementPosition.ts, 27, 38))
100100

101-
// c4
101+
// c7
102+
}
103+
}
104+
105+
class DerivedCommentsInvalidThis extends Object {
106+
>DerivedCommentsInvalidThis : Symbol(DerivedCommentsInvalidThis, Decl(derivedClassSuperStatementPosition.ts, 38, 1))
107+
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
108+
109+
x: any;
110+
>x : Symbol(DerivedCommentsInvalidThis.x, Decl(derivedClassSuperStatementPosition.ts, 40, 49))
111+
112+
constructor() {
113+
// c0
114+
this;
115+
>this : Symbol(DerivedCommentsInvalidThis, Decl(derivedClassSuperStatementPosition.ts, 38, 1))
116+
117+
// c1
118+
console.log(); // c2
119+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
120+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
121+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
122+
123+
// c3
124+
super(); // c4
125+
>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --))
126+
127+
// c5
128+
this.x = null; // c6
129+
>this.x : Symbol(DerivedCommentsInvalidThis.x, Decl(derivedClassSuperStatementPosition.ts, 40, 49))
130+
>this : Symbol(DerivedCommentsInvalidThis, Decl(derivedClassSuperStatementPosition.ts, 38, 1))
131+
>x : Symbol(DerivedCommentsInvalidThis.x, Decl(derivedClassSuperStatementPosition.ts, 40, 49))
132+
133+
// c7
102134
}
103135
}
104136

105137
class DerivedInConditional extends Object {
106-
>DerivedInConditional : Symbol(DerivedInConditional, Decl(derivedClassSuperStatementPosition.ts, 38, 1))
138+
>DerivedInConditional : Symbol(DerivedInConditional, Decl(derivedClassSuperStatementPosition.ts, 53, 1))
107139
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
108140

109141
prop = 1;
110-
>prop : Symbol(DerivedInConditional.prop, Decl(derivedClassSuperStatementPosition.ts, 40, 43))
142+
>prop : Symbol(DerivedInConditional.prop, Decl(derivedClassSuperStatementPosition.ts, 55, 43))
111143

112144
constructor() {
113145
Math.random()
@@ -124,11 +156,11 @@ class DerivedInConditional extends Object {
124156
}
125157

126158
class DerivedInIf extends Object {
127-
>DerivedInIf : Symbol(DerivedInIf, Decl(derivedClassSuperStatementPosition.ts, 47, 1))
159+
>DerivedInIf : Symbol(DerivedInIf, Decl(derivedClassSuperStatementPosition.ts, 62, 1))
128160
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
129161

130162
prop = 1;
131-
>prop : Symbol(DerivedInIf.prop, Decl(derivedClassSuperStatementPosition.ts, 49, 34))
163+
>prop : Symbol(DerivedInIf.prop, Decl(derivedClassSuperStatementPosition.ts, 64, 34))
132164

133165
constructor() {
134166
if (Math.random()) {
@@ -147,14 +179,14 @@ class DerivedInIf extends Object {
147179
}
148180

149181
class DerivedInBlockWithProperties extends Object {
150-
>DerivedInBlockWithProperties : Symbol(DerivedInBlockWithProperties, Decl(derivedClassSuperStatementPosition.ts, 59, 1))
182+
>DerivedInBlockWithProperties : Symbol(DerivedInBlockWithProperties, Decl(derivedClassSuperStatementPosition.ts, 74, 1))
151183
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
152184

153185
prop = 1;
154-
>prop : Symbol(DerivedInBlockWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 61, 51))
186+
>prop : Symbol(DerivedInBlockWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 76, 51))
155187

156188
constructor(private paramProp = 2) {
157-
>paramProp : Symbol(DerivedInBlockWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 63, 16))
189+
>paramProp : Symbol(DerivedInBlockWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 78, 16))
158190
{
159191
super();
160192
>super : Symbol(ObjectConstructor, Decl(lib.es5.d.ts, --, --))
@@ -163,14 +195,14 @@ class DerivedInBlockWithProperties extends Object {
163195
}
164196

165197
class DerivedInConditionalWithProperties extends Object {
166-
>DerivedInConditionalWithProperties : Symbol(DerivedInConditionalWithProperties, Decl(derivedClassSuperStatementPosition.ts, 68, 1))
198+
>DerivedInConditionalWithProperties : Symbol(DerivedInConditionalWithProperties, Decl(derivedClassSuperStatementPosition.ts, 83, 1))
167199
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
168200

169201
prop = 1;
170-
>prop : Symbol(DerivedInConditionalWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 70, 57))
202+
>prop : Symbol(DerivedInConditionalWithProperties.prop, Decl(derivedClassSuperStatementPosition.ts, 85, 57))
171203

172204
constructor(private paramProp = 2) {
173-
>paramProp : Symbol(DerivedInConditionalWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 72, 16))
205+
>paramProp : Symbol(DerivedInConditionalWithProperties.paramProp, Decl(derivedClassSuperStatementPosition.ts, 87, 16))
174206

175207
if (Math.random()) {
176208
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

tests/baselines/reference/derivedClassSuperStatementPosition.types

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,62 @@ class DerivedComments extends Object {
9292

9393
constructor() {
9494
// c1
95-
console.log();
95+
console.log(); // c2
9696
>console.log() : void
9797
>console.log : (...data: any[]) => void
9898
>console : Console
9999
>log : (...data: any[]) => void
100100

101-
// c2
102-
super();
101+
// c3
102+
super(); // c4
103103
>super() : void
104104
>super : ObjectConstructor
105105

106+
// c5
107+
this.x = null; // c6
108+
>this.x = null : null
109+
>this.x : any
110+
>this : this
111+
>x : any
112+
>null : null
113+
114+
// c7
115+
}
116+
}
117+
118+
class DerivedCommentsInvalidThis extends Object {
119+
>DerivedCommentsInvalidThis : DerivedCommentsInvalidThis
120+
>Object : Object
121+
122+
x: any;
123+
>x : any
124+
125+
constructor() {
126+
// c0
127+
this;
128+
>this : this
129+
130+
// c1
131+
console.log(); // c2
132+
>console.log() : void
133+
>console.log : (...data: any[]) => void
134+
>console : Console
135+
>log : (...data: any[]) => void
136+
106137
// c3
107-
this.x = null;
138+
super(); // c4
139+
>super() : void
140+
>super : ObjectConstructor
141+
142+
// c5
143+
this.x = null; // c6
108144
>this.x = null : null
109145
>this.x : any
110146
>this : this
111147
>x : any
112148
>null : null
113149

114-
// c4
150+
// c7
115151
}
116152
}
117153

tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,27 @@ class DerivedComments extends Object {
3131
x: any;
3232
constructor() {
3333
// c1
34-
console.log();
35-
// c2
36-
super();
34+
console.log(); // c2
35+
// c3
36+
super(); // c4
37+
// c5
38+
this.x = null; // c6
39+
// c7
40+
}
41+
}
42+
43+
class DerivedCommentsInvalidThis extends Object {
44+
x: any;
45+
constructor() {
46+
// c0
47+
this;
48+
// c1
49+
console.log(); // c2
3750
// c3
38-
this.x = null;
39-
// c4
51+
super(); // c4
52+
// c5
53+
this.x = null; // c6
54+
// c7
4055
}
4156
}
4257

0 commit comments

Comments
 (0)