diff --git a/src/metadata/metadataVisitor.ts b/src/metadata/metadataVisitor.ts index e345426..368ed05 100644 --- a/src/metadata/metadataVisitor.ts +++ b/src/metadata/metadataVisitor.ts @@ -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': diff --git a/src/metadata/serializeType.ts b/src/metadata/serializeType.ts index a60cc0e..96a407e 100644 --- a/src/metadata/serializeType.ts +++ b/src/metadata/serializeType.ts @@ -3,7 +3,7 @@ import { NodePath } from '@babel/traverse'; type InferArray = T extends Array ? A : never; -type Parameter = InferArray | t.ClassProperty; +type Parameter = InferArray | t.ClassProperty | t.ClassMethod['returnType']; function createVoidZero() { return t.unaryExpression('void', t.numericLiteral(0)); @@ -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; @@ -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; } @@ -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 = (node).typeAnnotation.typeAnnotation; + const className = classPath.node.id ? classPath.node.id.name : ''; + return serializeTypeNode(className, annotation); + } } function serializeTypeReferenceNode(