Skip to content

Commit d5ac244

Browse files
authored
Merge pull request #16496 from Microsoft/fix16417
Fix decorated class with extends in new class emit
2 parents 68e2a45 + 7cf6fdb commit d5ac244

File tree

6 files changed

+150
-21
lines changed

6 files changed

+150
-21
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3401,28 +3401,19 @@ namespace ts {
34013401
classBodyStart++;
34023402
}
34033403

3404-
// We reuse the comment and source-map positions from the original variable statement
3405-
// and class alias, while converting the function declaration for the class constructor
3406-
// into an expression.
3404+
// The next statement is the function declaration.
3405+
statements.push(funcStatements[classBodyStart]);
3406+
classBodyStart++;
3407+
3408+
// Add the class alias following the declaration.
34073409
statements.push(
3408-
updateVariableStatement(
3409-
varStatement,
3410-
/*modifiers*/ undefined,
3411-
updateVariableDeclarationList(varStatement.declarationList, [
3412-
updateVariableDeclaration(variable,
3413-
variable.name,
3414-
/*type*/ undefined,
3415-
updateBinary(aliasAssignment,
3416-
aliasAssignment.left,
3417-
convertFunctionDeclarationToExpression(
3418-
cast(funcStatements[classBodyStart], isFunctionDeclaration)
3419-
)
3420-
)
3421-
)
3422-
])
3410+
createStatement(
3411+
createAssignment(
3412+
aliasAssignment.left,
3413+
cast(variable.name, isIdentifier)
3414+
)
34233415
)
34243416
);
3425-
classBodyStart++;
34263417
}
34273418

34283419
// Find the trailing 'return' statement (4)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//// [decoratorOnClass9.ts]
2+
declare var dec: any;
3+
4+
class A {}
5+
6+
// https://github.com/Microsoft/TypeScript/issues/16417
7+
@dec
8+
class B extends A {
9+
static x = 1;
10+
static y = B.x;
11+
m() {
12+
return B.x;
13+
}
14+
}
15+
16+
//// [decoratorOnClass9.js]
17+
var __extends = (this && this.__extends) || (function () {
18+
var extendStatics = Object.setPrototypeOf ||
19+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
20+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
21+
return function (d, b) {
22+
extendStatics(d, b);
23+
function __() { this.constructor = d; }
24+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
25+
};
26+
})();
27+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
28+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
29+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
30+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
31+
return c > 3 && r && Object.defineProperty(target, key, r), r;
32+
};
33+
var A = (function () {
34+
function A() {
35+
}
36+
return A;
37+
}());
38+
// https://github.com/Microsoft/TypeScript/issues/16417
39+
var B = (function (_super) {
40+
__extends(B, _super);
41+
function B() {
42+
return _super !== null && _super.apply(this, arguments) || this;
43+
}
44+
B_1 = B;
45+
B.prototype.m = function () {
46+
return B_1.x;
47+
};
48+
B.x = 1;
49+
B.y = B_1.x;
50+
B = B_1 = __decorate([
51+
dec
52+
], B);
53+
return B;
54+
var B_1;
55+
}(A));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/conformance/decorators/class/decoratorOnClass9.ts ===
2+
declare var dec: any;
3+
>dec : Symbol(dec, Decl(decoratorOnClass9.ts, 0, 11))
4+
5+
class A {}
6+
>A : Symbol(A, Decl(decoratorOnClass9.ts, 0, 21))
7+
8+
// https://github.com/Microsoft/TypeScript/issues/16417
9+
@dec
10+
>dec : Symbol(dec, Decl(decoratorOnClass9.ts, 0, 11))
11+
12+
class B extends A {
13+
>B : Symbol(B, Decl(decoratorOnClass9.ts, 2, 10))
14+
>A : Symbol(A, Decl(decoratorOnClass9.ts, 0, 21))
15+
16+
static x = 1;
17+
>x : Symbol(B.x, Decl(decoratorOnClass9.ts, 6, 19))
18+
19+
static y = B.x;
20+
>y : Symbol(B.y, Decl(decoratorOnClass9.ts, 7, 17))
21+
>B.x : Symbol(B.x, Decl(decoratorOnClass9.ts, 6, 19))
22+
>B : Symbol(B, Decl(decoratorOnClass9.ts, 2, 10))
23+
>x : Symbol(B.x, Decl(decoratorOnClass9.ts, 6, 19))
24+
25+
m() {
26+
>m : Symbol(B.m, Decl(decoratorOnClass9.ts, 8, 19))
27+
28+
return B.x;
29+
>B.x : Symbol(B.x, Decl(decoratorOnClass9.ts, 6, 19))
30+
>B : Symbol(B, Decl(decoratorOnClass9.ts, 2, 10))
31+
>x : Symbol(B.x, Decl(decoratorOnClass9.ts, 6, 19))
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
=== tests/cases/conformance/decorators/class/decoratorOnClass9.ts ===
2+
declare var dec: any;
3+
>dec : any
4+
5+
class A {}
6+
>A : A
7+
8+
// https://github.com/Microsoft/TypeScript/issues/16417
9+
@dec
10+
>dec : any
11+
12+
class B extends A {
13+
>B : B
14+
>A : A
15+
16+
static x = 1;
17+
>x : number
18+
>1 : 1
19+
20+
static y = B.x;
21+
>y : number
22+
>B.x : number
23+
>B : typeof B
24+
>x : number
25+
26+
m() {
27+
>m : () => number
28+
29+
return B.x;
30+
>B.x : number
31+
>B : typeof B
32+
>x : number
33+
}
34+
}

tests/baselines/reference/es6modulekindWithES5Target11.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
1616
return c > 3 && r && Object.defineProperty(target, key, r), r;
1717
};
1818
var C = (function () {
19-
var C = C_1 = function C() {
19+
function C() {
2020
this.p = 1;
21-
};
21+
}
22+
C_1 = C;
2223
C.x = function () { return C_1.y; };
2324
C.prototype.method = function () { };
2425
C.y = 1;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @target:es5
2+
// @experimentaldecorators: true
3+
declare var dec: any;
4+
5+
class A {}
6+
7+
// https://github.com/Microsoft/TypeScript/issues/16417
8+
@dec
9+
class B extends A {
10+
static x = 1;
11+
static y = B.x;
12+
m() {
13+
return B.x;
14+
}
15+
}

0 commit comments

Comments
 (0)