1- import { Class , Interface , Method , Namespace , Property , Type } from "./types"
1+ import { Class , Interface , Namespace } from "./types"
2+ import { createNamespace } from "./export/namespace" ;
23import * as fs from 'fs' ;
34import * as path from 'path' ;
45
@@ -51,93 +52,3 @@ const assignToNamespace = (s: Class | Interface, ns: Namespace, nsParts: string[
5152 }
5253 }
5354}
54-
55- const filterDupliciteStaticProperties = ( c : Class , ns : Namespace ) => c . properties . filter ( p => {
56- const sameNamespace = ns . namespaces . find ( cns => cns . name === c . name ) ;
57-
58- return ! sameNamespace || ( ! sameNamespace . classes . find ( cc => cc . name === p . name ) && ! sameNamespace . namespaces . find ( cns => cns . name === p . name ) ) ;
59- } )
60-
61- const propertyAccessMap = { 'normal' : '' , 'static' : 'static ' , 'constant' : 'static readonly ' } ;
62-
63- const createNamespace = ( ns : Namespace ) => ( ns . parent ? ( ns . parent . parent ? '' : 'declare ' ) + 'namespace ' + ns . name + ' {\n' : '' )
64- + createStructures ( ns . classes , ns )
65- + ( ns . interfaces . length > 0 ? '\n\n' + createStructures ( ns . interfaces , ns ) : '' )
66- + ( ns . classes . length !== 0 && ns . namespaces . length !== 0 ? '\n\n' : '' )
67- + ns . namespaces . map ( createNamespace ) . join ( '\n\n' )
68- + ( ns . parent ? '}' : '' + '\n' ) ;
69-
70- const createStructures = ( ss : ( Class | Interface ) [ ] , ns : Namespace ) => ss . map ( ( s ) => createStructure ( s , ns ) ) . join ( '\n\n' ) ;
71-
72- const createStructure = ( s : Class | Interface , ns : Namespace ) => createComment ( s )
73- + ( ns . parent ? '' : 'declare ' )
74- + s . type + ' ' + s . name + ' ' + ( s . type === 'class' ? createClass ( s , ns ) : createInterface ( s , ns ) ) ;
75-
76- const createClass = ( c : Class , ns : Namespace ) => ( c . extends ? ' extends ' + c . extends : '' )
77- + ( c ?. implements . length > 0 ? ' implements ' + c . implements . join ( ', ' ) : '' )
78- + ' {\n' + createProperties ( c , ns ) + createMethods ( c ) + '}' ;
79-
80- const createInterface = ( i : Interface , ns : Namespace ) => ( i . extends . length > 0 ? ' extends ' + i . extends . join ( ', ' ) : '' )
81- + ' {\n' + createMethods ( i ) + '}' ;
82-
83- const createProperties = ( c : Class , ns : Namespace ) => c . properties . length === 0 ? '' : (
84- filterDupliciteStaticProperties ( c , ns )
85- . map ( p => createComment ( p ) + propertyAccessMap [ p . access ] + p . name + ';\n' )
86- . join ( '\n' ) + '\n'
87- ) ;
88-
89- const createMethods = ( s : Class | Interface ) => s . methods . map ( m => createComment ( m )
90- + ( m . static ? 'static ' : '' )
91- + m . name + '(' + createArguments ( m ) + ')'
92- + ( m . type ? ': ' + createType ( m . type ) : '' ) + ';\n' ) . join ( '\n' ) ;
93-
94- const createComment = ( o : Method | Class | Interface | Property ) => {
95- const commentSections = [ ] ;
96-
97- if ( o . comment ) {
98- commentSections . push ( o . comment ) ;
99- }
100- if ( 'events' in o ) {
101- o . events . forEach ( e => {
102- commentSections . push ( '@fires ' + e . name + ( e . comment ? ' ' + e . comment : '' ) ) ;
103- } )
104- }
105- if ( o . url ) {
106- commentSections . push ( '@see ' + o . url ) ;
107- }
108- if ( 'arguments' in o ) {
109- o . arguments . forEach ( a => {
110- const createName = ( ) => {
111- if ( a . optional ) {
112- if ( a . defaultValue ) {
113- if ( a . type === 'string' ) {
114- return '[' + a . name + '="' + a . defaultValue + '"]' ;
115- } else {
116- return '[' + a . name + '=' + a . defaultValue + ']' ;
117- }
118- } else {
119- return '[' + a . name + ']' ;
120- }
121- } else {
122- return a . name ;
123- }
124- }
125-
126- commentSections . push ( '@param ' + ( a . type ? '{' + createType ( a . type ) + '} ' : '' ) + createName ( ) + ( a . comment ? ' ' + a . comment : '' ) ) ;
127- } )
128-
129- if ( o . type && o . type !== 'void' ) {
130- commentSections . push ( '@returns ' + ( '{' + createType ( o . type ) + '}' ) + ( o . returnComment ? ' ' + o . returnComment : '' ) ) ;
131- }
132- }
133-
134- if ( commentSections . length === 0 ) {
135- return '' ;
136- }
137-
138- return '/**\n * ' + commentSections . join ( '\n * ' ) + '\n */\n' ;
139- }
140-
141- const createArguments = ( m : Method ) => m . arguments . map ( a => a . name + ( a . optional ? '?' : '' ) + ( a . type ? ': ' + createType ( a . type ) : '' ) ) . join ( ', ' ) ;
142-
143- const createType = ( type : Type | Type [ ] ) => typeof type === 'string' ? type : type . join ( ' | ' ) ;
0 commit comments