Skip to content

Commit fcdc07e

Browse files
authored
Merge pull request #10760 from Microsoft/fix10731_commentDecoratedClass
Fix10731: Correctly emit comment for decorated class declaration
2 parents 2ac86d7 + 7d91ac8 commit fcdc07e

File tree

5 files changed

+133
-11
lines changed

5 files changed

+133
-11
lines changed

src/compiler/transformers/ts.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -694,19 +694,21 @@ namespace ts {
694694

695695
// let ${name} = ${classExpression} where name is either declaredName if the class doesn't contain self-reference
696696
// or decoratedClassAlias if the class contain self-reference.
697+
const transformedClassExpression = createVariableStatement(
698+
/*modifiers*/ undefined,
699+
createLetDeclarationList([
700+
createVariableDeclaration(
701+
classAlias || declaredName,
702+
/*type*/ undefined,
703+
classExpression
704+
)
705+
]),
706+
/*location*/ location
707+
);
708+
setCommentRange(transformedClassExpression, node);
697709
statements.push(
698710
setOriginalNode(
699-
createVariableStatement(
700-
/*modifiers*/ undefined,
701-
createLetDeclarationList([
702-
createVariableDeclaration(
703-
classAlias || declaredName,
704-
/*type*/ undefined,
705-
classExpression
706-
)
707-
]),
708-
/*location*/ location
709-
),
711+
/*node*/ transformedClassExpression,
710712
/*original*/ node
711713
)
712714
);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [commentOnDecoratedClassDeclaration.ts]
2+
declare function decorator(x: string): any;
3+
4+
/**
5+
* Leading trivia
6+
*/
7+
@decorator("hello")
8+
class Remote { }
9+
10+
/**
11+
* Floating Comment
12+
*/
13+
14+
@decorator("hi")
15+
class AnotherRomote {
16+
constructor() {}
17+
}
18+
19+
//// [commentOnDecoratedClassDeclaration.js]
20+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
21+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
22+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
23+
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;
24+
return c > 3 && r && Object.defineProperty(target, key, r), r;
25+
};
26+
/**
27+
* Leading trivia
28+
*/
29+
var Remote = (function () {
30+
function Remote() {
31+
}
32+
return Remote;
33+
}());
34+
Remote = __decorate([
35+
decorator("hello")
36+
], Remote);
37+
/**
38+
* Floating Comment
39+
*/
40+
var AnotherRomote = (function () {
41+
function AnotherRomote() {
42+
}
43+
return AnotherRomote;
44+
}());
45+
AnotherRomote = __decorate([
46+
decorator("hi")
47+
], AnotherRomote);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/commentOnDecoratedClassDeclaration.ts ===
2+
declare function decorator(x: string): any;
3+
>decorator : Symbol(decorator, Decl(commentOnDecoratedClassDeclaration.ts, 0, 0))
4+
>x : Symbol(x, Decl(commentOnDecoratedClassDeclaration.ts, 0, 27))
5+
6+
/**
7+
* Leading trivia
8+
*/
9+
@decorator("hello")
10+
>decorator : Symbol(decorator, Decl(commentOnDecoratedClassDeclaration.ts, 0, 0))
11+
12+
class Remote { }
13+
>Remote : Symbol(Remote, Decl(commentOnDecoratedClassDeclaration.ts, 0, 43))
14+
15+
/**
16+
* Floating Comment
17+
*/
18+
19+
@decorator("hi")
20+
>decorator : Symbol(decorator, Decl(commentOnDecoratedClassDeclaration.ts, 0, 0))
21+
22+
class AnotherRomote {
23+
>AnotherRomote : Symbol(AnotherRomote, Decl(commentOnDecoratedClassDeclaration.ts, 6, 16))
24+
25+
constructor() {}
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/commentOnDecoratedClassDeclaration.ts ===
2+
declare function decorator(x: string): any;
3+
>decorator : (x: string) => any
4+
>x : string
5+
6+
/**
7+
* Leading trivia
8+
*/
9+
@decorator("hello")
10+
>decorator("hello") : any
11+
>decorator : (x: string) => any
12+
>"hello" : string
13+
14+
class Remote { }
15+
>Remote : Remote
16+
17+
/**
18+
* Floating Comment
19+
*/
20+
21+
@decorator("hi")
22+
>decorator("hi") : any
23+
>decorator : (x: string) => any
24+
>"hi" : string
25+
26+
class AnotherRomote {
27+
>AnotherRomote : AnotherRomote
28+
29+
constructor() {}
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @experimentalDecorators: true
2+
declare function decorator(x: string): any;
3+
4+
/**
5+
* Leading trivia
6+
*/
7+
@decorator("hello")
8+
class Remote { }
9+
10+
/**
11+
* Floating Comment
12+
*/
13+
14+
@decorator("hi")
15+
class AnotherRomote {
16+
constructor() {}
17+
}

0 commit comments

Comments
 (0)