Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile decorators properly when googmodule is false and transformTypesToClosure is true #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions demo/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function classDecorator(constructor: Function) {
Object.seal(constructor);
Object.seal(constructor.prototype);
}

export function methodDecorator(value: boolean) {
return function (target: any, propertyKey: string, descriptor?: PropertyDescriptor) {
if (descriptor) {
descriptor.enumerable = value;
}
};
}

export function accessorDecorator(value: boolean,) {
return function (target: any, propertyKey: string, descriptor?: PropertyDescriptor) {
if (descriptor) {
descriptor.configurable = value;
}
};
}

1 change: 1 addition & 0 deletions demo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"strict": true,
"moduleResolution": "Bundler",
"esModuleInterop": true,
"experimentalDecorators": true,
},
"exclude": [
"test.ts"
Expand Down
28 changes: 28 additions & 0 deletions demo/using_decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { accessorDecorator, classDecorator, methodDecorator } from "./decorators";

@classDecorator
export class Person {
private name_: string;
constructor(name: string) {
this.name_ = name;
}

@accessorDecorator(true)
get name() {
return this.name_;
}

@methodDecorator(true)
greet() {
console.log(`Hello, my name is ${this.name}.`);
}
}

const p = new Person("Ron");
p.greet();


export class Employee {
constructor(private age_: number) {}
get age() { return this.age_; }
}
13 changes: 13 additions & 0 deletions src/decorator_downlevel_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,19 @@ export function decoratorDownlevelTransformer(
if (decoratedProperties.size) {
newMembers.push(createPropDecoratorsClassProperty(diagnostics, decoratedProperties));
}
/**
* I'm pretty sure this is where the class ends up turning from
*
* @classDecorator
* class Person { ... }
*
* into
*
* let Person = class Person { ... }
* Person = __decorate([
* classDecorator
* ], Person);
*/
return ts.factory.updateClassDeclaration(
classDecl, modifiersToKeep.length ? modifiersToKeep : undefined,
classDecl.name, classDecl.typeParameters, classDecl.heritageClauses,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"strict": true,
"outDir": "out",
"paths": {"tsickle": ["./src/tsickle.ts"]},
"esModuleInterop": true
"esModuleInterop": true,
"experimentalDecorators": true
},
"include": [
"src/*.ts",
Expand Down