|
| 1 | +import type { Class, ClassElement, MethodDefinition, ParseResult, TSType, TSTypeName } from 'oxc-parser' |
| 2 | +import MagicString from 'magic-string' |
| 3 | +import { walk } from './walk' |
| 4 | + |
| 5 | +type ConstructorType = `Object` | `Array` | `String` | `Number` | `Boolean` | `Function` | (string & {}) |
| 6 | + |
| 7 | +function transformTSTypeName(typeName: TSTypeName): ConstructorType { |
| 8 | + if (typeName.type === 'Identifier') { |
| 9 | + return typeName.typeAnnotation ? transformTSType(typeName.typeAnnotation) : `(() => { try { return ${typeName.name} } catch (e) { return Object } })()` |
| 10 | + } |
| 11 | + return transformTSTypeName(typeName.left) |
| 12 | +} |
| 13 | + |
| 14 | +function transformTSType(type: TSType): ConstructorType { |
| 15 | + switch (type.type) { |
| 16 | + case 'TSStringKeyword': |
| 17 | + return 'String' |
| 18 | + case 'TSNumberKeyword': |
| 19 | + return 'Number' |
| 20 | + case 'TSBooleanKeyword': |
| 21 | + return 'Boolean' |
| 22 | + case 'TSFunctionType': |
| 23 | + return 'Function' |
| 24 | + case 'TSArrayType': |
| 25 | + return 'Array' |
| 26 | + case 'TSTypeReference': |
| 27 | + return transformTSTypeName(type.typeName) |
| 28 | + default: |
| 29 | + return 'Object' |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function transformClassMembers(members: ClassElement[], s: MagicString): void { |
| 34 | + for (const member of members) { |
| 35 | + if (member.type === 'StaticBlock') |
| 36 | + continue |
| 37 | + if (member.type === 'TSAbstractMethodDefinition') |
| 38 | + continue |
| 39 | + if (member.type === 'TSIndexSignature') |
| 40 | + continue |
| 41 | + if (member.type === 'TSAbstractPropertyDefinition') |
| 42 | + continue |
| 43 | + if (member.type === 'TSAbstractAccessorProperty') |
| 44 | + continue |
| 45 | + |
| 46 | + if (member.type === 'PropertyDefinition') { |
| 47 | + const memberDecoratorStart = member.decorators?.[0]?.start || member.start |
| 48 | + s.appendLeft(memberDecoratorStart, `@Reflect._uniocMetadata("design:type", ${member.typeAnnotation?.typeAnnotation ? transformTSType(member.typeAnnotation.typeAnnotation) : 'Object'})\n`) |
| 49 | + } |
| 50 | + else if (member.type === 'MethodDefinition') { |
| 51 | + if (member.kind === 'constructor') |
| 52 | + continue |
| 53 | + |
| 54 | + const memberDecoratorStart = member.decorators?.[0]?.start || member.start |
| 55 | + const returnType = member.value.returnType?.typeAnnotation ? transformTSType(member.value.returnType.typeAnnotation) : 'Object' |
| 56 | + const paramTypes = member.value.params.map(param => param.typeAnnotation?.typeAnnotation ? transformTSType(param.typeAnnotation.typeAnnotation) : 'Object').join(',') |
| 57 | + s.appendLeft(memberDecoratorStart, `@Reflect._uniocMetadata("design:type", Function)\n@Reflect._uniocMetadata("design:returntype", ${returnType})\n@Reflect._uniocMetadata("design:paramtypes", [${paramTypes}])\n`) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function transformClassDeclaration(node: Class, s: MagicString): void { |
| 63 | + const decoratorStart = node.decorators?.[0]?.start || node.start |
| 64 | + const constructorMethod = node.body.body.find(v => v.type === 'MethodDefinition' && v.kind === 'constructor') as MethodDefinition | undefined |
| 65 | + const paramTypes = constructorMethod?.value.params |
| 66 | + .map(param => param.typeAnnotation?.typeAnnotation) |
| 67 | + .filter(Boolean) |
| 68 | + .map(value => transformTSType(value!)) || [] |
| 69 | + |
| 70 | + s.appendLeft(decoratorStart, `@Reflect._uniocMetadata("design:type", Function)\n@Reflect._uniocMetadata("design:paramtypes", [${paramTypes.join(',')}])\n`) |
| 71 | +} |
| 72 | + |
| 73 | +export async function transform(ast: ParseResult, fullText: string): Promise<MagicString> { |
| 74 | + const s = new MagicString(fullText) |
| 75 | + |
| 76 | + walk(ast.program, { |
| 77 | + all(node) { |
| 78 | + if (node.type !== 'ClassDeclaration') |
| 79 | + return |
| 80 | + |
| 81 | + transformClassDeclaration(node, s) |
| 82 | + transformClassMembers(node.body.body, s) |
| 83 | + }, |
| 84 | + }) |
| 85 | + |
| 86 | + return s |
| 87 | +} |
0 commit comments