Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/class_decorator_downlevel_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -149,4 +149,4 @@ export function classDecoratorDownlevelTransformer(

return (sf: ts.SourceFile) => visitor(sf) as ts.SourceFile;
};
}
}
8 changes: 6 additions & 2 deletions src/decorator-annotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ts.Decorator[]>;
private propDecorators: Map<string, ts.Decorator[]>;

constructor(
private typeChecker: ts.TypeChecker, private rewriter: Rewriter,
Expand Down Expand Up @@ -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(
Expand Down
87 changes: 31 additions & 56 deletions test/decorator-annotator_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,6 @@ class Foo {
{ type: Test1 },
{ type: Test2, args: [param,] },
];
/** @nocollapse */
static ctorParameters: () => ({
type: any;
decorators?: {
type: Function;
args?: any[];
}[];
} | null)[] = () => [];
}
`);
});
Expand All @@ -148,14 +140,6 @@ class Foo {
}[] = [
{ type: Test },
];
/** @nocollapse */
static ctorParameters: () => ({
type: any;
decorators?: {
type: Function;
args?: any[];
}[];
} | null)[] = () => [];
}
`);
});
Expand All @@ -177,14 +161,6 @@ class Foo {
}[] = [
{ type: Test },
];
/** @nocollapse */
static ctorParameters: () => ({
type: any;
decorators?: {
type: Function;
args?: any[];
}[];
} | null)[] = () => [];
}
`);
});
Expand Down Expand Up @@ -219,14 +195,6 @@ class Foo {
{ type: Test3 },
{ type: Test4, args: [param,] },
];
/** @nocollapse */
static ctorParameters: () => ({
type: any;
decorators?: {
type: Function;
args?: any[];
}[];
} | null)[] = () => [];
}
`);
});
Expand All @@ -246,14 +214,6 @@ export class Foo {
}[] = [
{ type: Test1 },
];
/** @nocollapse */
static ctorParameters: () => ({
type: any;
decorators?: {
type: Function;
args?: any[];
}[];
} | null)[] = () => [];
}
`);
});
Expand Down Expand Up @@ -282,14 +242,6 @@ export class Foo {
}[] = [
{ type: Test2 },
];
/** @nocollapse */
static ctorParameters: () => ({
type: any;
decorators?: {
type: Function;
args?: any[];
}[];
} | null)[] = () => [];
}
}
static decorators: {
Expand All @@ -298,14 +250,6 @@ export class Foo {
}[] = [
{ type: Test1 },
];
/** @nocollapse */
static ctorParameters: () => ({
type: any;
decorators?: {
type: Function;
args?: any[];
}[];
} | null)[] = () => [];
}
`);
});
Expand All @@ -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;
Expand Down