diff --git a/src/class_decorator_downlevel_transformer.ts b/src/class_decorator_downlevel_transformer.ts index cc626cce4..321776187 100644 --- a/src/class_decorator_downlevel_transformer.ts +++ b/src/class_decorator_downlevel_transformer.ts @@ -100,7 +100,7 @@ function insertBeforeDecoratorProperties( let insertionPoint = classMembers.findIndex( m => isNameEqual(m, 'ctorParameters') || isNameEqual(m, 'propDecorators')); if (insertionPoint === -1) { - insertionPoint = classMembers.length - 1; + insertionPoint = classMembers.length; // Insert at end of list } const members = [ ...classMembers.slice(0, insertionPoint), decoratorMetadata, @@ -149,4 +149,4 @@ export function classDecoratorDownlevelTransformer( return (sf: ts.SourceFile) => visitor(sf) as ts.SourceFile; }; -} \ No newline at end of file +} diff --git a/src/decorator-annotator.ts b/src/decorator-annotator.ts index ea944f2db..2d52e5be2 100644 --- a/src/decorator-annotator.ts +++ b/src/decorator-annotator.ts @@ -64,11 +64,11 @@ export function shouldLower(decorator: ts.Decorator, typeChecker: ts.TypeChecker // separately for each class we encounter. export class DecoratorClassVisitor { /** Decorators on the class itself. */ - decorators: ts.Decorator[]; + private decorators: ts.Decorator[]; /** The constructor parameter list and decorators on each param. */ private ctorParameters: ConstructorParameter[]; /** Per-method decorators. */ - propDecorators: Map; + private propDecorators: Map; constructor( private typeChecker: ts.TypeChecker, private rewriter: Rewriter, @@ -249,8 +249,12 @@ export class DecoratorClassVisitor { */ emitMetadataAsStaticProperties() { const decoratorInvocations = '{type: Function, args?: any[]}[]'; + if (this.decorators || this.ctorParameters) { this.rewriter.emit(`/** @nocollapse */\n`); + } + + if (this.ctorParameters) { // ctorParameters may contain forward references in the type: field, so wrap in a function // closure this.rewriter.emit( diff --git a/test/decorator-annotator_test.ts b/test/decorator-annotator_test.ts index fcac2c97e..2a9b70c90 100644 --- a/test/decorator-annotator_test.ts +++ b/test/decorator-annotator_test.ts @@ -120,14 +120,6 @@ class Foo { { type: Test1 }, { type: Test2, args: [param,] }, ]; - /** @nocollapse */ - static ctorParameters: () => ({ - type: any; - decorators?: { - type: Function; - args?: any[]; - }[]; - } | null)[] = () => []; } `); }); @@ -148,14 +140,6 @@ class Foo { }[] = [ { type: Test }, ]; - /** @nocollapse */ - static ctorParameters: () => ({ - type: any; - decorators?: { - type: Function; - args?: any[]; - }[]; - } | null)[] = () => []; } `); }); @@ -177,14 +161,6 @@ class Foo { }[] = [ { type: Test }, ]; - /** @nocollapse */ - static ctorParameters: () => ({ - type: any; - decorators?: { - type: Function; - args?: any[]; - }[]; - } | null)[] = () => []; } `); }); @@ -219,14 +195,6 @@ class Foo { { type: Test3 }, { type: Test4, args: [param,] }, ]; - /** @nocollapse */ - static ctorParameters: () => ({ - type: any; - decorators?: { - type: Function; - args?: any[]; - }[]; - } | null)[] = () => []; } `); }); @@ -246,14 +214,6 @@ export class Foo { }[] = [ { type: Test1 }, ]; - /** @nocollapse */ - static ctorParameters: () => ({ - type: any; - decorators?: { - type: Function; - args?: any[]; - }[]; - } | null)[] = () => []; } `); }); @@ -282,14 +242,6 @@ export class Foo { }[] = [ { type: Test2 }, ]; - /** @nocollapse */ - static ctorParameters: () => ({ - type: any; - decorators?: { - type: Function; - args?: any[]; - }[]; - } | null)[] = () => []; } } static decorators: { @@ -298,14 +250,6 @@ export class Foo { }[] = [ { type: Test1 }, ]; - /** @nocollapse */ - static ctorParameters: () => ({ - type: any; - decorators?: { - type: Function; - args?: any[]; - }[]; - } | null)[] = () => []; } `); }); @@ -321,6 +265,37 @@ class Foo { `); }); + it('transforms empty ctors', () => { + expectTranslated(` +import {FakeDecorator} from 'bar'; +/** @Annotation */ let Test1: FakeDecorator; +@Test1() +class Foo { + constructor() { + } +}`).to.equal(`import { FakeDecorator } from 'bar'; +/** @Annotation */ let Test1: FakeDecorator; +class Foo { + constructor() { + } + static decorators: { + type: Function; + args?: any[]; + }[] = [ + { type: Test1 }, + ]; + /** @nocollapse */ + static ctorParameters: () => ({ + type: any; + decorators?: { + type: Function; + args?: any[]; + }[]; + } | null)[] = () => []; +} +`); + }); + it('transforms injected ctors', () => { expectTranslated(` /** @Annotation */ let Inject: Function;