diff --git a/src/metadata/metadataVisitor.ts b/src/metadata/metadataVisitor.ts index 1ca02c9..01c0256 100644 --- a/src/metadata/metadataVisitor.ts +++ b/src/metadata/metadataVisitor.ts @@ -4,18 +4,26 @@ import { serializeType } from './serializeType'; function createMetadataDesignDecorator( design: 'design:type' | 'design:paramtypes' | 'design:returntype' | 'design:typeinfo', - typeArg: t.Expression | t.SpreadElement | t.JSXNamespacedName | t.ArgumentPlaceholder + typeArg: t.Expression | t.SpreadElement | t.JSXNamespacedName ): t.Decorator { return t.decorator( - t.callExpression( - t.memberExpression( - t.identifier('Reflect'), - t.identifier('metadata') + t.logicalExpression( + '||', + t.optionalCallExpression( + t.memberExpression( + t.identifier('Reflect'), + t.identifier('metadata') + ), + [ + t.stringLiteral(design), + typeArg + ], + true ), - [ - t.stringLiteral(design), - typeArg - ] + t.arrowFunctionExpression( + [t.identifier('t')], + t.identifier('t') + ) ) ) } diff --git a/test/__fixtures__/generics/output.js b/test/__fixtures__/generics/output.js index 44c0403..bf589e4 100644 --- a/test/__fixtures__/generics/output.js +++ b/test/__fixtures__/generics/output.js @@ -2,9 +2,9 @@ var _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2; function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } -let MyClass = (_dec = Reflect.metadata("design:type", Function), _dec2 = Reflect.metadata("design:paramtypes", [typeof Generic === "undefined" ? Object : Generic, typeof Generic === "undefined" ? Object : Generic]), _dec3 = function (target, key) { +let MyClass = (_dec = Reflect.metadata?.("design:type", Function) || (t => t), _dec2 = Reflect.metadata?.("design:paramtypes", [typeof Generic === "undefined" ? Object : Generic, typeof Generic === "undefined" ? Object : Generic]) || (t => t), _dec3 = function (target, key) { return Arg()(target, key, 1); -}, _dec4 = Reflect.metadata("design:type", Function), _dec5 = Reflect.metadata("design:paramtypes", [typeof Inter === "undefined" ? Object : Inter, typeof InterGen === "undefined" ? Object : InterGen]), Decorate(_class = _dec(_class = _dec2(_class = (_class2 = class MyClass { +}, _dec4 = Reflect.metadata?.("design:type", Function) || (t => t), _dec5 = Reflect.metadata?.("design:paramtypes", [typeof Inter === "undefined" ? Object : Inter, typeof InterGen === "undefined" ? Object : InterGen]) || (t => t), Decorate(_class = _dec(_class = _dec2(_class = (_class2 = class MyClass { constructor(generic, generic2) { this.generic = generic; } diff --git a/test/__fixtures__/nest-injection/output.js b/test/__fixtures__/nest-injection/output.js index bb1c81e..2d7c4c0 100644 --- a/test/__fixtures__/nest-injection/output.js +++ b/test/__fixtures__/nest-injection/output.js @@ -7,7 +7,7 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } import { AppService } from './app.service'; -export let AppController = (_dec = Controller(), _dec2 = Reflect.metadata("design:type", Function), _dec3 = Reflect.metadata("design:paramtypes", [typeof AppService === "undefined" ? Object : AppService]), _dec4 = Inject(), _dec5 = Reflect.metadata("design:type", typeof AppService === "undefined" ? Object : AppService), _dec6 = Inject(), _dec7 = Reflect.metadata("design:type", typeof AppService === "undefined" ? Object : AppService), _dec8 = Get(), _dec9 = Reflect.metadata("design:type", Function), _dec10 = Reflect.metadata("design:paramtypes", []), _dec(_class = _dec2(_class = _dec3(_class = (_class2 = class AppController { +export let AppController = (_dec = Controller(), _dec2 = Reflect.metadata?.("design:type", Function) || (t => t), _dec3 = Reflect.metadata?.("design:paramtypes", [typeof AppService === "undefined" ? Object : AppService]) || (t => t), _dec4 = Inject(), _dec5 = Reflect.metadata?.("design:type", typeof AppService === "undefined" ? Object : AppService) || (t => t), _dec6 = Inject(), _dec7 = Reflect.metadata?.("design:type", typeof AppService === "undefined" ? Object : AppService) || (t => t), _dec8 = Get(), _dec9 = Reflect.metadata?.("design:type", Function) || (t => t), _dec10 = Reflect.metadata?.("design:paramtypes", []) || (t => t), _dec(_class = _dec2(_class = _dec3(_class = (_class2 = class AppController { constructor(appService) { this.appService = appService; diff --git a/test/__fixtures__/parameter-decorator-typed/output.js b/test/__fixtures__/parameter-decorator-typed/output.js index 26383e2..b97e689 100644 --- a/test/__fixtures__/parameter-decorator-typed/output.js +++ b/test/__fixtures__/parameter-decorator-typed/output.js @@ -6,7 +6,7 @@ class Injected {} let MyClass = (_dec = function (target, key) { return inject()(target, undefined, 0); -}, _dec2 = Reflect.metadata("design:type", Function), _dec3 = Reflect.metadata("design:paramtypes", [typeof Injected === "undefined" ? Object : Injected]), _dec(_class = _dec2(_class = _dec3(_class = class MyClass { +}, _dec2 = Reflect.metadata?.("design:type", Function) || (t => t), _dec3 = Reflect.metadata?.("design:paramtypes", [typeof Injected === "undefined" ? Object : Injected]) || (t => t), _dec(_class = _dec2(_class = _dec3(_class = class MyClass { constructor(parameter) {} }) || _class) || _class) || _class); @@ -14,15 +14,15 @@ let MyOtherClass = (_dec4 = function (target, key) { return inject()(target, undefined, 0); }, _dec5 = function (target, key) { return inject('KIND')(target, undefined, 1); -}, _dec6 = Reflect.metadata("design:type", Function), _dec7 = Reflect.metadata("design:paramtypes", [typeof Injected === "undefined" ? Object : Injected, typeof Injected === "undefined" ? Object : Injected]), _dec8 = function (target, key) { +}, _dec6 = Reflect.metadata?.("design:type", Function) || (t => t), _dec7 = Reflect.metadata?.("design:paramtypes", [typeof Injected === "undefined" ? Object : Injected, typeof Injected === "undefined" ? Object : Injected]) || (t => t), _dec8 = function (target, key) { return demo()(target, key, 0); -}, _dec9 = Reflect.metadata("design:type", Function), _dec10 = Reflect.metadata("design:paramtypes", [String, void 0]), _dec11 = decorate('named'), _dec12 = function (target, key) { +}, _dec9 = Reflect.metadata?.("design:type", Function) || (t => t), _dec10 = Reflect.metadata?.("design:paramtypes", [String, void 0]) || (t => t), _dec11 = decorate('named'), _dec12 = function (target, key) { return inject()(target, key, 0); }, _dec13 = function (target, key) { return arg()(target, key, 1); -}, _dec14 = Reflect.metadata("design:type", Function), _dec15 = Reflect.metadata("design:paramtypes", [typeof Injected === "undefined" ? Object : Injected, typeof Schema === "undefined" ? Object : Schema]), _dec16 = function (target, key) { +}, _dec14 = Reflect.metadata?.("design:type", Function) || (t => t), _dec15 = Reflect.metadata?.("design:paramtypes", [typeof Injected === "undefined" ? Object : Injected, typeof Schema === "undefined" ? Object : Schema]) || (t => t), _dec16 = function (target, key) { return argObjectSpread()(target, key, 0); -}, _dec17 = Reflect.metadata("design:type", Function), _dec18 = Reflect.metadata("design:paramtypes", [typeof SchemaObjectSpread === "undefined" ? Object : SchemaObjectSpread]), _dec4(_class2 = _dec5(_class2 = _dec6(_class2 = _dec7(_class2 = (_class3 = class MyOtherClass { +}, _dec17 = Reflect.metadata?.("design:type", Function) || (t => t), _dec18 = Reflect.metadata?.("design:paramtypes", [typeof SchemaObjectSpread === "undefined" ? Object : SchemaObjectSpread]) || (t => t), _dec4(_class2 = _dec5(_class2 = _dec6(_class2 = _dec7(_class2 = (_class3 = class MyOtherClass { constructor(parameter, otherParam) { this.parameter = parameter; } @@ -40,9 +40,9 @@ let DecoratedClass = (_dec19 = function (target, key) { return inject()(target, undefined, 0); }, _dec20 = function (target, key) { return inject()(target, undefined, 1); -}, _dec21 = Reflect.metadata("design:type", Function), _dec22 = Reflect.metadata("design:paramtypes", [typeof Injected === "undefined" ? Object : Injected, typeof Injected === "undefined" ? Object : Injected]), _dec23 = decorate('example'), _dec24 = function (target, key) { +}, _dec21 = Reflect.metadata?.("design:type", Function) || (t => t), _dec22 = Reflect.metadata?.("design:paramtypes", [typeof Injected === "undefined" ? Object : Injected, typeof Injected === "undefined" ? Object : Injected]) || (t => t), _dec23 = decorate('example'), _dec24 = function (target, key) { return inject()(target, key, 0); -}, _dec25 = Reflect.metadata("design:type", Function), _dec26 = Reflect.metadata("design:paramtypes", [String]), Decorate(_class4 = _dec19(_class4 = _dec20(_class4 = _dec21(_class4 = _dec22(_class4 = (_class5 = class DecoratedClass { +}, _dec25 = Reflect.metadata?.("design:type", Function) || (t => t), _dec26 = Reflect.metadata?.("design:paramtypes", [String]) || (t => t), Decorate(_class4 = _dec19(_class4 = _dec20(_class4 = _dec21(_class4 = _dec22(_class4 = (_class5 = class DecoratedClass { constructor(module, otherModule) { this.module = module; } diff --git a/test/__fixtures__/type-serialization/output.js b/test/__fixtures__/type-serialization/output.js index 7506971..84837af 100644 --- a/test/__fixtures__/type-serialization/output.js +++ b/test/__fixtures__/type-serialization/output.js @@ -4,9 +4,9 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con import { Decorate } from './Decorate'; const sym = Symbol(); -let Sample = (_dec = Decorate(), _dec2 = Reflect.metadata("design:type", Function), _dec3 = Reflect.metadata("design:paramtypes", [typeof String === "undefined" ? Object : String, typeof Number === "undefined" ? Object : Number, Number, String, Boolean, String, Number, typeof Object === "undefined" ? Object : Object, Function, String, Object, typeof Function === "undefined" ? Object : Function, void 0, void 0, Object, Function, Boolean, Boolean, String]), _dec4 = function (target, key) { +let Sample = (_dec = Decorate(), _dec2 = Reflect.metadata?.("design:type", Function) || (t => t), _dec3 = Reflect.metadata?.("design:paramtypes", [typeof String === "undefined" ? Object : String, typeof Number === "undefined" ? Object : Number, Number, String, Boolean, String, Number, typeof Object === "undefined" ? Object : Object, Function, String, Object, typeof Function === "undefined" ? Object : Function, void 0, void 0, Object, Function, Boolean, Boolean, String]) || (t => t), _dec4 = function (target, key) { return Arg()(target, key, 0); -}, _dec5 = Reflect.metadata("design:type", Function), _dec6 = Reflect.metadata("design:paramtypes", [typeof Symbol === "undefined" ? Object : Symbol, Object, String, void 0, String, String, typeof Maybe === "undefined" ? Object : Maybe, Object, Object, Array, Array, void 0, Boolean, void 0, String, typeof Object === "undefined" ? Object : Object, Object, Number]), _dec7 = Decorate(), _dec8 = Reflect.metadata("design:type", Function), _dec9 = Reflect.metadata("design:paramtypes", [typeof Decorate.Name === "undefined" ? Object : Decorate.Name, typeof Decorate.Name === "undefined" ? Object : Decorate.Name]), _dec10 = Decorate(), _dec11 = Reflect.metadata("design:type", Function), _dec12 = Reflect.metadata("design:paramtypes", [String]), _dec(_class = _dec2(_class = _dec3(_class = (_class2 = class Sample { +}, _dec5 = Reflect.metadata?.("design:type", Function) || (t => t), _dec6 = Reflect.metadata?.("design:paramtypes", [typeof Symbol === "undefined" ? Object : Symbol, Object, String, void 0, String, String, typeof Maybe === "undefined" ? Object : Maybe, Object, Object, Array, Array, void 0, Boolean, void 0, String, typeof Object === "undefined" ? Object : Object, Object, Number]) || (t => t), _dec7 = Decorate(), _dec8 = Reflect.metadata?.("design:type", Function) || (t => t), _dec9 = Reflect.metadata?.("design:paramtypes", [typeof Decorate.Name === "undefined" ? Object : Decorate.Name, typeof Decorate.Name === "undefined" ? Object : Decorate.Name]) || (t => t), _dec10 = Decorate(), _dec11 = Reflect.metadata?.("design:type", Function) || (t => t), _dec12 = Reflect.metadata?.("design:paramtypes", [String]) || (t => t), _dec(_class = _dec2(_class = _dec3(_class = (_class2 = class Sample { constructor(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18 = 'abc') { this.p0 = p0; } diff --git a/test/__modules__/member-expression/output.js b/test/__modules__/member-expression/output.js index 494c5ca..c3652c5 100644 --- a/test/__modules__/member-expression/output.js +++ b/test/__modules__/member-expression/output.js @@ -13,7 +13,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de let SomeService = (_dec = Injectable(), _dec2 = function (target, key) { return Inject('aws.s3')(target, undefined, 0); -}, _dec3 = Reflect.metadata("design:type", Function), _dec4 = Reflect.metadata("design:paramtypes", [typeof _awsSdk.default.S3 === "undefined" ? Object : _awsSdk.default.S3]), _dec(_class = _dec2(_class = _dec3(_class = _dec4(_class = class SomeService { +}, _dec3 = Reflect.metadata?.("design:type", Function) || (t => t), _dec4 = Reflect.metadata?.("design:paramtypes", [typeof _awsSdk.default.S3 === "undefined" ? Object : _awsSdk.default.S3]) || (t => t), _dec(_class = _dec2(_class = _dec3(_class = _dec4(_class = class SomeService { constructor(s3client) { this.s3client = s3client; } diff --git a/test/__node__/function-param-decorator/output.js b/test/__node__/function-param-decorator/output.js index f22df36..29a0a0f 100644 --- a/test/__node__/function-param-decorator/output.js +++ b/test/__node__/function-param-decorator/output.js @@ -20,13 +20,13 @@ let Named = (_dec = function (target, key) { return (0, _decorator.default)(_some.Some)(target, undefined, 0); }, _dec2 = function (target, key) { return (0, _decorator.default)(target, undefined, 1); -}, _dec3 = Reflect.metadata("design:type", Function), _dec4 = Reflect.metadata("design:paramtypes", [typeof _some.Some === "undefined" ? Object : _some.Some, typeof _some.Some === "undefined" ? Object : _some.Some]), _dec5 = (0, _based.default)(), _dec6 = function (target, key) { +}, _dec3 = Reflect.metadata?.("design:type", Function) || (t => t), _dec4 = Reflect.metadata?.("design:paramtypes", [typeof _some.Some === "undefined" ? Object : _some.Some, typeof _some.Some === "undefined" ? Object : _some.Some]) || (t => t), _dec5 = (0, _based.default)(), _dec6 = function (target, key) { return (0, _graphql.Args)()(target, key, 0); }, _dec7 = function (target, key) { return (0, _graphql.Context)()(target, key, 1); }, _dec8 = function (target, key) { return (0, _decorator.default)(_xyz.Xyz)(target, key, 2); -}, _dec9 = Reflect.metadata("design:type", Function), _dec10 = Reflect.metadata("design:paramtypes", [typeof _graphql.Args === "undefined" ? Object : _graphql.Args, typeof _graphql.Context === "undefined" ? Object : _graphql.Context, Object]), (0, _based.default)(_class = _dec(_class = _dec2(_class = _dec3(_class = _dec4(_class = (_class2 = class Named { +}, _dec9 = Reflect.metadata?.("design:type", Function) || (t => t), _dec10 = Reflect.metadata?.("design:paramtypes", [typeof _graphql.Args === "undefined" ? Object : _graphql.Args, typeof _graphql.Context === "undefined" ? Object : _graphql.Context, Object]) || (t => t), (0, _based.default)(_class = _dec(_class = _dec2(_class = _dec3(_class = _dec4(_class = (_class2 = class Named { constructor(param, param2) { this.param = param; this.param2 = param2; diff --git a/test/__node__/self-reference/output.js b/test/__node__/self-reference/output.js index 402b895..705c943 100644 --- a/test/__node__/self-reference/output.js +++ b/test/__node__/self-reference/output.js @@ -10,7 +10,7 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); } -let Self = (_dec = injectable(), _dec2 = (0, _lib.inject)(), _dec3 = Reflect.metadata("design:type", Object), _dec(_class = (_class2 = class Self { +let Self = (_dec = injectable(), _dec2 = (0, _lib.inject)(), _dec3 = Reflect.metadata?.("design:type", Object) || (t => t), _dec(_class = (_class2 = class Self { constructor() { _initializerDefineProperty(this, "child", _descriptor, this); }