Skip to content
Open
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
33 changes: 33 additions & 0 deletions src/metadata/serializeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ function serializeTypeReferenceNode(
);
}

function serializeTypeImportNode(
className: string,
node: t.TSImportType
) {
/**
* We need to save references to this type since it is going
* to be used as a Value (and not just as a Type) here.
*
* This is resolved in main plugin method, calling
* `path.scope.crawl()` which updates the bindings.
*/
const reference = serializeReference(node.qualifier!);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your effort! I'd like to know here when qualifier may be null (maybe handling it with a simple return t.identifier('Object'); in this case): do you have more info about the TSImportType?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont know when the qualifier can be null, I just followed the TypeScript declaration for TSImportType

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be when the dynamic import not found


/**
* We don't know if type is just a type (interface, etc.) or a concrete
* value (class, etc.).
* `typeof` operator allows us to use the expression even if it is not
* defined, fallback is just `Object`.
*/
return t.conditionalExpression(
t.binaryExpression(
'===',
t.unaryExpression('typeof', reference),
t.stringLiteral('undefined')
),
t.identifier('Object'),
t.clone(reference)
);
}

/**
* Checks if node (this should be the result of `serializeReference`) member
* expression or identifier is a reference to self (class name).
Expand Down Expand Up @@ -193,6 +223,9 @@ function serializeTypeNode(className: string, node: t.TSType): SerializedType {
case 'TSConditionalType':
return serializeTypeList(className, [node.trueType, node.falseType]);

case 'TSImportType':
return serializeTypeImportNode(className, node);

case 'TSTypeQuery':
case 'TSTypeOperator':
case 'TSIndexedAccessType':
Expand Down
3 changes: 3 additions & 0 deletions test/__fixtures__/nest-injection/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export class AppController {
@Inject()
private appService2: AppService;

@Inject()
private appService3: import('./app.service').AppService;

@Get()
getHello(): string {
return this.appService.getHello();
Expand Down
13 changes: 10 additions & 3 deletions test/__fixtures__/nest-injection/output.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _class, _class2, _descriptor, _descriptor2, _temp;
var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class, _class2, _descriptor, _descriptor2, _descriptor3, _temp;

function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }

Expand All @@ -7,13 +7,15 @@ 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 set to use loose mode. ' + 'To use proposal-class-properties in spec mode with decorators, wait for ' + 'the next major version of decorators in stage 2.'); }

import { AppService } from './app.service';
export let AppController = (_dec = Controller(), _dec2 = Reflect.metadata("design:paramtypes", [typeof AppService === "undefined" ? Object : AppService]), _dec3 = Inject(), _dec4 = Reflect.metadata("design:type", typeof AppService === "undefined" ? Object : AppService), _dec5 = Inject(), _dec6 = Reflect.metadata("design:type", typeof AppService === "undefined" ? Object : AppService), _dec7 = Get(), _dec8 = Reflect.metadata("design:paramtypes", []), _dec(_class = _dec2(_class = (_class2 = (_temp = class AppController {
export let AppController = (_dec = Controller(), _dec2 = Reflect.metadata("design:paramtypes", [typeof AppService === "undefined" ? Object : AppService]), _dec3 = Inject(), _dec4 = Reflect.metadata("design:type", typeof AppService === "undefined" ? Object : AppService), _dec5 = Inject(), _dec6 = Reflect.metadata("design:type", typeof AppService === "undefined" ? Object : AppService), _dec7 = Inject(), _dec8 = Reflect.metadata("design:type", typeof AppService === "undefined" ? Object : AppService), _dec9 = Get(), _dec10 = Reflect.metadata("design:paramtypes", []), _dec(_class = _dec2(_class = (_class2 = (_temp = class AppController {
constructor(appService) {
this.appService = appService;

_initializerDefineProperty(this, "appService", _descriptor, this);

_initializerDefineProperty(this, "appService2", _descriptor2, this);

_initializerDefineProperty(this, "appService3", _descriptor3, this);
}

getHello() {
Expand All @@ -30,4 +32,9 @@ export let AppController = (_dec = Controller(), _dec2 = Reflect.metadata("desig
enumerable: true,
writable: true,
initializer: null
}), _applyDecoratedDescriptor(_class2.prototype, "getHello", [_dec7, _dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "getHello"), _class2.prototype)), _class2)) || _class) || _class);
}), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "appService3", [_dec7, _dec8], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
}), _applyDecoratedDescriptor(_class2.prototype, "getHello", [_dec9, _dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "getHello"), _class2.prototype)), _class2)) || _class) || _class);