From 60b382d1b33d19a5506d81c13f9b44efae89aa93 Mon Sep 17 00:00:00 2001 From: Yui T Date: Wed, 7 Sep 2016 13:54:52 -0700 Subject: [PATCH 1/2] Correct emit comment for decorated class declaration --- src/compiler/transformers/ts.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index efb247ba663..00441c279c7 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -694,19 +694,21 @@ namespace ts { // let ${name} = ${classExpression} where name is either declaredName if the class doesn't contain self-reference // or decoratedClassAlias if the class contain self-reference. + const transformedClassExpression = createVariableStatement( + /*modifiers*/ undefined, + createLetDeclarationList([ + createVariableDeclaration( + classAlias || declaredName, + /*type*/ undefined, + classExpression + ) + ]), + /*location*/ location + ); + setCommentRange(transformedClassExpression, node); statements.push( setOriginalNode( - createVariableStatement( - /*modifiers*/ undefined, - createLetDeclarationList([ - createVariableDeclaration( - classAlias || declaredName, - /*type*/ undefined, - classExpression - ) - ]), - /*location*/ location - ), + /*node*/ transformedClassExpression, /*original*/ node ) ); From 7d91ac808b71e2a5dc7fd4912d7ede960b866a55 Mon Sep 17 00:00:00 2001 From: Yui T Date: Wed, 7 Sep 2016 13:55:02 -0700 Subject: [PATCH 2/2] Add tests and baselines --- .../commentOnDecoratedClassDeclaration.js | 47 +++++++++++++++++++ ...commentOnDecoratedClassDeclaration.symbols | 26 ++++++++++ .../commentOnDecoratedClassDeclaration.types | 30 ++++++++++++ .../commentOnDecoratedClassDeclaration.ts | 17 +++++++ 4 files changed, 120 insertions(+) create mode 100644 tests/baselines/reference/commentOnDecoratedClassDeclaration.js create mode 100644 tests/baselines/reference/commentOnDecoratedClassDeclaration.symbols create mode 100644 tests/baselines/reference/commentOnDecoratedClassDeclaration.types create mode 100644 tests/cases/compiler/commentOnDecoratedClassDeclaration.ts diff --git a/tests/baselines/reference/commentOnDecoratedClassDeclaration.js b/tests/baselines/reference/commentOnDecoratedClassDeclaration.js new file mode 100644 index 00000000000..44a7047a2ac --- /dev/null +++ b/tests/baselines/reference/commentOnDecoratedClassDeclaration.js @@ -0,0 +1,47 @@ +//// [commentOnDecoratedClassDeclaration.ts] +declare function decorator(x: string): any; + +/** + * Leading trivia + */ +@decorator("hello") +class Remote { } + +/** + * Floating Comment + */ + +@decorator("hi") +class AnotherRomote { + constructor() {} +} + +//// [commentOnDecoratedClassDeclaration.js] +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + 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; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +/** + * Leading trivia + */ +var Remote = (function () { + function Remote() { + } + return Remote; +}()); +Remote = __decorate([ + decorator("hello") +], Remote); +/** + * Floating Comment + */ +var AnotherRomote = (function () { + function AnotherRomote() { + } + return AnotherRomote; +}()); +AnotherRomote = __decorate([ + decorator("hi") +], AnotherRomote); diff --git a/tests/baselines/reference/commentOnDecoratedClassDeclaration.symbols b/tests/baselines/reference/commentOnDecoratedClassDeclaration.symbols new file mode 100644 index 00000000000..92a21ce23ba --- /dev/null +++ b/tests/baselines/reference/commentOnDecoratedClassDeclaration.symbols @@ -0,0 +1,26 @@ +=== tests/cases/compiler/commentOnDecoratedClassDeclaration.ts === +declare function decorator(x: string): any; +>decorator : Symbol(decorator, Decl(commentOnDecoratedClassDeclaration.ts, 0, 0)) +>x : Symbol(x, Decl(commentOnDecoratedClassDeclaration.ts, 0, 27)) + +/** + * Leading trivia + */ +@decorator("hello") +>decorator : Symbol(decorator, Decl(commentOnDecoratedClassDeclaration.ts, 0, 0)) + +class Remote { } +>Remote : Symbol(Remote, Decl(commentOnDecoratedClassDeclaration.ts, 0, 43)) + +/** + * Floating Comment + */ + +@decorator("hi") +>decorator : Symbol(decorator, Decl(commentOnDecoratedClassDeclaration.ts, 0, 0)) + +class AnotherRomote { +>AnotherRomote : Symbol(AnotherRomote, Decl(commentOnDecoratedClassDeclaration.ts, 6, 16)) + + constructor() {} +} diff --git a/tests/baselines/reference/commentOnDecoratedClassDeclaration.types b/tests/baselines/reference/commentOnDecoratedClassDeclaration.types new file mode 100644 index 00000000000..f4850a0b7b7 --- /dev/null +++ b/tests/baselines/reference/commentOnDecoratedClassDeclaration.types @@ -0,0 +1,30 @@ +=== tests/cases/compiler/commentOnDecoratedClassDeclaration.ts === +declare function decorator(x: string): any; +>decorator : (x: string) => any +>x : string + +/** + * Leading trivia + */ +@decorator("hello") +>decorator("hello") : any +>decorator : (x: string) => any +>"hello" : string + +class Remote { } +>Remote : Remote + +/** + * Floating Comment + */ + +@decorator("hi") +>decorator("hi") : any +>decorator : (x: string) => any +>"hi" : string + +class AnotherRomote { +>AnotherRomote : AnotherRomote + + constructor() {} +} diff --git a/tests/cases/compiler/commentOnDecoratedClassDeclaration.ts b/tests/cases/compiler/commentOnDecoratedClassDeclaration.ts new file mode 100644 index 00000000000..aadf63bf757 --- /dev/null +++ b/tests/cases/compiler/commentOnDecoratedClassDeclaration.ts @@ -0,0 +1,17 @@ +// @experimentalDecorators: true +declare function decorator(x: string): any; + +/** + * Leading trivia + */ +@decorator("hello") +class Remote { } + +/** + * Floating Comment + */ + +@decorator("hi") +class AnotherRomote { + constructor() {} +} \ No newline at end of file