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
57 changes: 41 additions & 16 deletions src/metadata/metadataVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,48 @@ export function metadataVisitor(
field.kind === 'constructor' ? classNode.decorators : field.decorators;

if (!decorators || decorators.length === 0) return;

decorators!.push(
t.decorator(
t.callExpression(
t.memberExpression(
t.identifier('Reflect'),
t.identifier('metadata')
),
[
t.stringLiteral('design:paramtypes'),
t.arrayExpression(
field.params.map(param => serializeType(classPath, param))
)
]
if(field.kind === 'get') {
field.decorators!.push(
t.decorator(
t.callExpression(
t.memberExpression(
t.identifier('Reflect'),
t.identifier('metadata')
),
[t.stringLiteral('design:type'), serializeType(classPath, field.returnType)]
)
)
)
);
);
} else if(field.kind === 'set') {
field.decorators!.push(
t.decorator(
t.callExpression(
t.memberExpression(
t.identifier('Reflect'),
t.identifier('metadata')
),
[t.stringLiteral('design:type'), field.params.map(param => serializeType(classPath, param)).reduce(value => value)]
)
)
);
} else {
decorators!.push(
t.decorator(
t.callExpression(
t.memberExpression(
t.identifier('Reflect'),
t.identifier('metadata')
),
[
t.stringLiteral('design:paramtypes'),
t.arrayExpression(
field.params.map(param => serializeType(classPath, param))
)
]
)
)
);
}
break;

case 'ClassProperty':
Expand Down
22 changes: 15 additions & 7 deletions src/metadata/serializeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NodePath } from '@babel/traverse';

type InferArray<T> = T extends Array<infer A> ? A : never;

type Parameter = InferArray<t.ClassMethod['params']> | t.ClassProperty;
type Parameter = InferArray<t.ClassMethod['params']> | t.ClassProperty | t.ClassMethod['returnType'];

function createVoidZero() {
return t.unaryExpression('void', t.numericLiteral(0));
Expand All @@ -16,7 +16,7 @@ function createVoidZero() {
* @todo Array and Objects spread are not supported.
* @todo Rest parameters are not supported.
*/
function getTypedNode(param: Parameter): t.Identifier | t.ClassProperty | null {
function getTypedNode(param: Parameter): t.Identifier | t.ClassProperty | t.TSTypeAnnotation | null {
if (param == null) return null;

if (param.type === 'ClassProperty') return param;
Expand All @@ -28,6 +28,9 @@ function getTypedNode(param: Parameter): t.Identifier | t.ClassProperty | null {
if (param.type === 'TSParameterProperty')
return getTypedNode(param.parameter);

if (param.type === 'TSTypeAnnotation')
return param;

return null;
}

Expand All @@ -38,12 +41,17 @@ export function serializeType(
const node = getTypedNode(param);
if (node == null) return createVoidZero();

if (!node.typeAnnotation || node.typeAnnotation.type !== 'TSTypeAnnotation')
if (!node.typeAnnotation || (node.typeAnnotation.type !== 'TSTypeAnnotation' && node.type !== 'TSTypeAnnotation'))
return createVoidZero();

const annotation = node.typeAnnotation.typeAnnotation;
const className = classPath.node.id ? classPath.node.id.name : '';
return serializeTypeNode(className, annotation);
if(node.type === 'TSTypeAnnotation') {
const annotation = node.typeAnnotation;
const className = classPath.node.id ? classPath.node.id.name : '';
return serializeTypeNode(className, annotation);
} else {
const annotation = (<any>node).typeAnnotation.typeAnnotation;
const className = classPath.node.id ? classPath.node.id.name : '';
return serializeTypeNode(className, annotation);
}
}

function serializeTypeReferenceNode(
Expand Down