@@ -2,11 +2,34 @@ const JUMP_LINE = '\n';
2
2
const FORMAT_CHAR = '\t' ;
3
3
4
4
class Function {
5
- constructor ( ) {
6
- this . name = '' ;
7
- this . type = '' ;
8
- this . parameters = [ ] ;
9
- this . privacy = '' ;
5
+ constructor ( {
6
+ name = '' ,
7
+ type = '' ,
8
+ parameters = [ ] ,
9
+ privacy = ''
10
+ } ) {
11
+ this . name = name ;
12
+ const typeParts = type . split ( '.' ) ;
13
+ this . type = typeParts [ typeParts . length - 1 ] ;
14
+
15
+ this . parameters = parameters ;
16
+ this . dependencies = new Set ( [ typeParts [ 0 ] ] . concat ( this . parameters . map ( parameter => parameter . split ( '.' ) [ 0 ] ) ) )
17
+ this . privacy = privacy ;
18
+ }
19
+
20
+ buildParameters ( ) {
21
+ return this . parameters
22
+ . map ( parameter => `${ parameter . name } : ${ parameter . type } ` )
23
+ . join ( ', ' ) ;
24
+ }
25
+
26
+ build ( ) {
27
+ let builder = '' ;
28
+ const privacyIfExists = this . privacy ? `${ this . privacy } ` : '' ;
29
+ builder += `${ FORMAT_CHAR } ${ privacyIfExists } ${ this . name } (${ this . buildParameters ( ) } ): ${ this . type } {${ JUMP_LINE } ` ;
30
+ builder += `${ FORMAT_CHAR } ${ FORMAT_CHAR } //TODO Implement${ JUMP_LINE } ` ;
31
+ builder += `${ FORMAT_CHAR } }${ JUMP_LINE } ` ;
32
+ return builder ;
10
33
}
11
34
}
12
35
@@ -16,22 +39,48 @@ class Propertie {
16
39
}
17
40
}
18
41
42
+ class MetaImport {
43
+ constructor ( fullObjectPath ) {
44
+ this . imports = { } ;
45
+ }
46
+
47
+ addImport ( fullObjectPath ) {
48
+ const fullObjectPathParts = fullObjectPath . split ( '.' ) ;
49
+ const fromObj = fullObjectPathParts [ 0 ] ;
50
+ const importObj = fullObjectPathParts [ 1 ] ;
51
+
52
+ const allImportsFromObject = this . imports [ fromObj ] || [ ] ;
53
+ allImportsFromObject . push ( importObj ) ;
54
+ this . imports [ fromObj ] = allImportsFromObject
55
+ }
56
+
57
+ build ( ) {
58
+ return Object
59
+ . keys ( this . imports )
60
+ . map ( ( from => `import { ${ this . imports [ from ] . join ( ', ' ) } } from ${ from } ` ) )
61
+ . join ( JUMP_LINE )
62
+ }
63
+ }
64
+
19
65
class MetaClass {
20
66
constructor ( ) {
21
67
this . properties = [ ] ;
22
68
this . functions = [ ] ;
23
69
this . className = '' ;
70
+ this . imports = new MetaImports ( ) ;
24
71
}
25
72
26
73
buildImports ( ) {
27
74
let builder = '' ;
28
- return builder ;
75
+ // return this.functions
76
+ // .map(func => func.dependencies)
77
+ // .reduce((prev, cur) => new Set([...prev, ...cur]), new Set())
78
+ // .map(import;
29
79
}
30
80
31
81
buildClass ( ) {
32
82
let builder = '' ;
33
- builder += `class ${ this . className } {'` ;
34
- builder += JUMP_LINE ;
83
+ builder += `class ${ this . className } {` ;
35
84
return builder ;
36
85
}
37
86
@@ -46,24 +95,15 @@ class MetaClass {
46
95
}
47
96
48
97
buildFunctions ( ) {
49
- let builder = '' ;
50
- this . functions . forEach ( func => {
51
- let parametersBuilder = ''
52
- if ( func . parameters ) {
53
- func . parameters . forEach ( parameter => {
54
- parametersBuilder += `${ parameter . name } : ${ parameter . type } , ` ;
55
- } ) ;
56
- parametersBuilder = parametersBuilder . substring ( 0 , parametersBuilder . length - 2 )
57
- }
58
- metaClass += `${ FORMAT_CHAR } static ${ func . name } (${ parametersBuilder } ): ${ func . type } {${ JUMP_LINE } ` ;
59
- metaClass += `${ FORMAT_CHAR } ${ FORMAT_CHAR } //TODO Implement${ JUMP_LINE } ` ;
60
- metaClass += `${ FORMAT_CHAR } }${ JUMP_LINE } ` ;
61
- } )
62
- return builder ;
98
+ return this . functions
99
+ . map ( func => func . build ( ) )
100
+ . reduce ( ( prev , cur ) => {
101
+ return `${ prev } ${ JUMP_LINE } ${ cur } ` ;
102
+ } , '' ) ;
63
103
}
64
104
65
105
buildEndClass ( ) {
66
- let builder = '' ;
106
+ let builder = JUMP_LINE ;
67
107
builder += '};'
68
108
builder += JUMP_LINE ;
69
109
builder += `export default ${ this . className } ;` ;
@@ -72,13 +112,17 @@ class MetaClass {
72
112
73
113
build ( ) {
74
114
let metaClass = '' ;
75
- metaClass += buildImports ( ) ;
76
- metaClass += buildClass ( ) ;
77
- metaClass += buildProperties ( ) ;
78
- metaClass += buildFunctions ( ) ;
79
- metaClass += buildEndClass ( ) ;
115
+ metaClass += this . buildImports ( ) ;
116
+ metaClass += this . buildClass ( ) ;
117
+ // metaClass += this. buildProperties();
118
+ metaClass += this . buildFunctions ( ) ;
119
+ metaClass += this . buildEndClass ( ) ;
80
120
return metaClass ;
81
121
}
82
122
}
83
123
84
- export default MetaClass ;
124
+ module . exports = {
125
+ MetaClass,
126
+ Function,
127
+ Propertie
128
+ }
0 commit comments