1
- import type { Class , ClassElement , MethodDefinition , ParseResult , TSType , TSTypeName } from 'oxc-parser'
1
+ import type { Class , ClassElement , MethodDefinition , ParseResult } from 'oxc-parser'
2
2
import MagicString from 'magic-string'
3
+ import { transformTSType , transformTSTypeToUniocFormat } from './typing'
3
4
import { walk } from './walk'
4
5
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
6
function transformClassMembers ( members : ClassElement [ ] , s : MagicString ) : void {
34
7
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' )
8
+ if ( member . type === 'StaticBlock' || member . type === 'TSAbstractMethodDefinition' || member . type === 'TSIndexSignature' || member . type === 'TSAbstractPropertyDefinition' || member . type === 'TSAbstractAccessorProperty' )
44
9
continue
45
10
46
11
if ( member . type === 'PropertyDefinition' ) {
47
12
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` )
13
+ s . appendLeft ( memberDecoratorStart , `@Reflect._uniocMetadata("design:type", ${ member . typeAnnotation ?. typeAnnotation ? transformTSType ( member . typeAnnotation . typeAnnotation ) : 'Object' } )\n${ s . getIndentString ( ) } ` )
49
14
}
50
15
else if ( member . type === 'MethodDefinition' ) {
51
16
if ( member . kind === 'constructor' )
@@ -54,7 +19,12 @@ function transformClassMembers(members: ClassElement[], s: MagicString): void {
54
19
const memberDecoratorStart = member . decorators ?. [ 0 ] ?. start || member . start
55
20
const returnType = member . value . returnType ?. typeAnnotation ? transformTSType ( member . value . returnType . typeAnnotation ) : 'Object'
56
21
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` )
22
+ const paramTypesInfo = member . value . params . map ( param => param . typeAnnotation ?. typeAnnotation ? transformTSTypeToUniocFormat ( param . typeAnnotation . typeAnnotation ) : 'Object' ) . join ( ',' )
23
+
24
+ s . appendLeft ( memberDecoratorStart , `@Reflect._uniocMetadata("design:type", Function)\n` )
25
+ . appendLeft ( memberDecoratorStart , `${ s . getIndentString ( ) } @Reflect._uniocMetadata("design:returntype", ${ returnType } )\n` )
26
+ . appendLeft ( memberDecoratorStart , `${ s . getIndentString ( ) } @Reflect._uniocMetadata("design:paramtypes", [${ paramTypes } ])\n` )
27
+ . appendLeft ( memberDecoratorStart , `${ s . getIndentString ( ) } @Reflect._uniocMetadata("design:paramtypesInfo", [${ paramTypesInfo } ])\n${ s . getIndentString ( ) } ` )
58
28
}
59
29
}
60
30
}
@@ -67,7 +37,25 @@ function transformClassDeclaration(node: Class, s: MagicString): void {
67
37
. filter ( Boolean )
68
38
. map ( value => transformTSType ( value ! ) ) || [ ]
69
39
70
- s . appendLeft ( decoratorStart , `@Reflect._uniocMetadata("design:type", Function)\n@Reflect._uniocMetadata("design:paramtypes", [${ paramTypes . join ( ',' ) } ])\n` )
40
+ s . appendLeft ( decoratorStart , `\n@Reflect._uniocMetadata("design:type", Function)\n` )
41
+ . appendLeft ( decoratorStart , `@Reflect._uniocMetadata("design:paramtypes", [${ paramTypes . join ( ',' ) } ])\n` )
42
+ }
43
+
44
+ function checkHasDecorator ( node : Class ) : boolean {
45
+ let hasDecorator = false
46
+ if ( node . decorators && node . decorators . length > 0 )
47
+ hasDecorator = true
48
+
49
+ for ( const body of node . body . body ) {
50
+ if ( body . type === 'MethodDefinition' && body . decorators && body . decorators . length > 0 )
51
+ return true
52
+ if ( body . type === 'PropertyDefinition' && body . decorators && body . decorators . length > 0 )
53
+ return true
54
+ if ( body . type === 'AccessorProperty' && body . decorators && body . decorators . length > 0 )
55
+ return true
56
+ }
57
+
58
+ return hasDecorator
71
59
}
72
60
73
61
export async function transform ( ast : ParseResult , fullText : string ) : Promise < MagicString > {
@@ -78,6 +66,9 @@ export async function transform(ast: ParseResult, fullText: string): Promise<Mag
78
66
if ( node . type !== 'ClassDeclaration' )
79
67
return
80
68
69
+ if ( ! checkHasDecorator ( node ) )
70
+ return
71
+
81
72
transformClassDeclaration ( node , s )
82
73
transformClassMembers ( node . body . body , s )
83
74
} ,
0 commit comments