Skip to content

Commit 1b9a9d8

Browse files
author
Patrick Szmucer
committed
Fix transformed constructor code when there is code between prologue statements and super call
1 parent 6894f91 commit 1b9a9d8

5 files changed

+140
-1
lines changed

src/compiler/transformers/es2015.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ namespace ts {
11461146
[
11471147
...existingPrologue,
11481148
...prologue,
1149-
...(superStatementIndex <= existingPrologue.length ? emptyArray : visitNodes(constructor.body.statements, visitor, isStatement, existingPrologue.length, superStatementIndex)),
1149+
...(superStatementIndex <= existingPrologue.length ? emptyArray : visitNodes(constructor.body.statements, visitor, isStatement, existingPrologue.length, superStatementIndex - existingPrologue.length)),
11501150
...statements
11511151
]
11521152
),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//// [constructorWithSuperAndPrologue.es5.ts]
2+
// https://github.com/microsoft/TypeScript/issues/48761
3+
"use strict";
4+
5+
class A {
6+
public constructor() {
7+
console.log("A")
8+
}
9+
}
10+
11+
class B extends A {
12+
constructor() {
13+
"ngInject";
14+
console.log("B")
15+
super();
16+
}
17+
}
18+
19+
20+
//// [constructorWithSuperAndPrologue.es5.js]
21+
// https://github.com/microsoft/TypeScript/issues/48761
22+
"use strict";
23+
var __extends = (this && this.__extends) || (function () {
24+
var extendStatics = function (d, b) {
25+
extendStatics = Object.setPrototypeOf ||
26+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
27+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
28+
return extendStatics(d, b);
29+
};
30+
return function (d, b) {
31+
if (typeof b !== "function" && b !== null)
32+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
33+
extendStatics(d, b);
34+
function __() { this.constructor = d; }
35+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
36+
};
37+
})();
38+
var A = /** @class */ (function () {
39+
function A() {
40+
console.log("A");
41+
}
42+
return A;
43+
}());
44+
var B = /** @class */ (function (_super) {
45+
__extends(B, _super);
46+
function B() {
47+
"ngInject";
48+
console.log("B");
49+
return _super.call(this) || this;
50+
}
51+
return B;
52+
}(A));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/compiler/constructorWithSuperAndPrologue.es5.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/48761
3+
"use strict";
4+
5+
class A {
6+
>A : Symbol(A, Decl(constructorWithSuperAndPrologue.es5.ts, 1, 13))
7+
8+
public constructor() {
9+
console.log("A")
10+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
11+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
12+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
13+
}
14+
}
15+
16+
class B extends A {
17+
>B : Symbol(B, Decl(constructorWithSuperAndPrologue.es5.ts, 7, 1))
18+
>A : Symbol(A, Decl(constructorWithSuperAndPrologue.es5.ts, 1, 13))
19+
20+
constructor() {
21+
"ngInject";
22+
console.log("B")
23+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
24+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
25+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
26+
27+
super();
28+
>super : Symbol(A, Decl(constructorWithSuperAndPrologue.es5.ts, 1, 13))
29+
}
30+
}
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== tests/cases/compiler/constructorWithSuperAndPrologue.es5.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/48761
3+
"use strict";
4+
>"use strict" : "use strict"
5+
6+
class A {
7+
>A : A
8+
9+
public constructor() {
10+
console.log("A")
11+
>console.log("A") : void
12+
>console.log : (...data: any[]) => void
13+
>console : Console
14+
>log : (...data: any[]) => void
15+
>"A" : "A"
16+
}
17+
}
18+
19+
class B extends A {
20+
>B : B
21+
>A : A
22+
23+
constructor() {
24+
"ngInject";
25+
>"ngInject" : "ngInject"
26+
27+
console.log("B")
28+
>console.log("B") : void
29+
>console.log : (...data: any[]) => void
30+
>console : Console
31+
>log : (...data: any[]) => void
32+
>"B" : "B"
33+
34+
super();
35+
>super() : void
36+
>super : typeof A
37+
}
38+
}
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @target: es5
2+
// https://github.com/microsoft/TypeScript/issues/48761
3+
"use strict";
4+
5+
class A {
6+
public constructor() {
7+
console.log("A")
8+
}
9+
}
10+
11+
class B extends A {
12+
constructor() {
13+
"ngInject";
14+
console.log("B")
15+
super();
16+
}
17+
}

0 commit comments

Comments
 (0)