From 4081ce01f0dab3c626e9af62e2b8999f4751c106 Mon Sep 17 00:00:00 2001 From: Mathis Zeiher Date: Mon, 1 Apr 2019 22:14:16 +0200 Subject: [PATCH 1/2] added type annotation for get/set properties --- src/metadata/metadataVisitor.ts | 58 ++++++++++++++++++++++++--------- src/metadata/serializeType.ts | 23 +++++++++---- 2 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/metadata/metadataVisitor.ts b/src/metadata/metadataVisitor.ts index e345426..dc13f00 100644 --- a/src/metadata/metadataVisitor.ts +++ b/src/metadata/metadataVisitor.ts @@ -15,23 +15,49 @@ 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)) - ) - ] + debugger; + 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..ae8295f 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; } @@ -35,15 +38,21 @@ export function serializeType( classPath: NodePath, param: Parameter ) { + debugger; 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( From a726ec3afaadb74f24c0e23ec99a46d7697960cc Mon Sep 17 00:00:00 2001 From: Mathis Zeiher Date: Mon, 1 Apr 2019 22:17:53 +0200 Subject: [PATCH 2/2] removed debugger directives --- src/metadata/metadataVisitor.ts | 1 - src/metadata/serializeType.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/src/metadata/metadataVisitor.ts b/src/metadata/metadataVisitor.ts index dc13f00..368ed05 100644 --- a/src/metadata/metadataVisitor.ts +++ b/src/metadata/metadataVisitor.ts @@ -15,7 +15,6 @@ export function metadataVisitor( field.kind === 'constructor' ? classNode.decorators : field.decorators; if (!decorators || decorators.length === 0) return; - debugger; if(field.kind === 'get') { field.decorators!.push( t.decorator( diff --git a/src/metadata/serializeType.ts b/src/metadata/serializeType.ts index ae8295f..96a407e 100644 --- a/src/metadata/serializeType.ts +++ b/src/metadata/serializeType.ts @@ -38,7 +38,6 @@ export function serializeType( classPath: NodePath, param: Parameter ) { - debugger; const node = getTypedNode(param); if (node == null) return createVoidZero();