@@ -22,42 +22,32 @@ public static string WriteTypeScript(IEnumerable<IntellisenseObject> objects)
2222
2323 foreach ( var ns in objects . GroupBy ( o => o . Namespace ) )
2424 {
25- if ( ! Options . GlobalScope )
25+ if ( Options . DeclareModule )
2626 {
2727 sb . AppendFormat ( "declare module {0} {{\r \n " , ns . Key ) ;
2828 }
2929
30+ string export = ! Options . DeclareModule ? "export " : string . Empty ;
31+ string prefixModule = Options . DeclareModule ? "\t " : String . Empty ;
32+
3033 foreach ( IntellisenseObject io in ns )
3134 {
3235 if ( ! string . IsNullOrEmpty ( io . Summary ) )
33- sb . AppendLine ( "\t /** " + _whitespaceTrimmer . Replace ( io . Summary , "" ) + " */" ) ;
36+ sb . Append ( prefixModule ) . AppendLine ( "/** " + _whitespaceTrimmer . Replace ( io . Summary , "" ) + " */" ) ;
3437
3538 if ( io . IsEnum )
3639 {
37- string export = Options . Export ? "export " : string . Empty ;
38- sb . AppendLine ( " \t " + export + "const enum " + Utility . CamelCaseClassName ( io . Name ) + " { ") ;
40+ string type = "enum " ;
41+ sb . Append ( prefixModule ) . Append ( export ) . Append ( type ) . Append ( Utility . CamelCaseClassName ( io . Name ) ) . Append ( " ") ;
3942
40- foreach ( var p in io . Properties )
41- {
42- WriteTypeScriptComment ( p , sb ) ;
43-
44- if ( p . InitExpression != null )
45- {
46- sb . AppendLine ( "\t \t " + Utility . CamelCaseEnumValue ( p . Name ) + " = " + CleanEnumInitValue ( p . InitExpression ) + "," ) ;
47- }
48- else
49- {
50- sb . AppendLine ( "\t \t " + Utility . CamelCaseEnumValue ( p . Name ) + "," ) ;
51- }
52- }
53-
54- sb . AppendLine ( "\t }" ) ;
43+ sb . AppendLine ( "{" ) ;
44+ WriteTSEnumDefinition ( sb , prefixModule + "\t " , io . Properties ) ;
45+ sb . Append ( prefixModule ) . AppendLine ( "}" ) ;
5546 }
5647 else
5748 {
5849 string type = Options . ClassInsteadOfInterface ? "class " : "interface " ;
59- string export = Options . Export ? "export " : string . Empty ;
60- sb . Append ( "\t " ) . Append ( export ) . Append ( type ) . Append ( Utility . CamelCaseClassName ( io . Name ) ) . Append ( " " ) ;
50+ sb . Append ( prefixModule ) . Append ( export ) . Append ( type ) . Append ( Utility . CamelCaseClassName ( io . Name ) ) . Append ( " " ) ;
6151
6252 if ( ! string . IsNullOrEmpty ( io . BaseName ) )
6353 {
@@ -69,12 +59,13 @@ public static string WriteTypeScript(IEnumerable<IntellisenseObject> objects)
6959 sb . Append ( Utility . CamelCaseClassName ( io . BaseName ) ) . Append ( " " ) ;
7060 }
7161
72- WriteTSInterfaceDefinition ( sb , "\t " , io . Properties ) ;
73- sb . AppendLine ( ) ;
62+ sb . AppendLine ( "{" ) ;
63+ WriteTSInterfaceDefinition ( sb , prefixModule + "\t " , io . Properties ) ;
64+ sb . Append ( prefixModule ) . AppendLine ( "}" ) ;
7465 }
7566 }
7667
77- if ( ! Options . GlobalScope )
68+ if ( Options . DeclareModule )
7869 {
7970 sb . AppendLine ( "}" ) ;
8071 }
@@ -91,36 +82,48 @@ private static string CleanEnumInitValue(string value)
9182 if ( trimedValue . Length > 0 ) return trimedValue ;
9283 return "0" ;
9384 }
94-
95-
96- private static void WriteTypeScriptComment ( IntellisenseProperty p , StringBuilder sb )
85+
86+ private static void WriteTypeScriptComment ( IntellisenseProperty p , StringBuilder sb , string prefix )
9787 {
9888 if ( string . IsNullOrEmpty ( p . Summary ) ) return ;
99- sb . AppendLine ( "\t \t /** " + _whitespaceTrimmer . Replace ( p . Summary , "" ) + " */" ) ;
89+ sb . Append ( prefix ) . AppendLine ( "/** " + _whitespaceTrimmer . Replace ( p . Summary , "" ) + " */" ) ;
10090 }
10191
102- private static void WriteTSInterfaceDefinition ( StringBuilder sb , string prefix ,
103- IEnumerable < IntellisenseProperty > props )
92+ private static void WriteTSEnumDefinition ( StringBuilder sb , string prefix , IEnumerable < IntellisenseProperty > props )
10493 {
105- sb . AppendLine ( "{" ) ;
94+ foreach ( var p in props )
95+ {
96+ WriteTypeScriptComment ( p , sb , prefix ) ;
10697
98+ if ( p . InitExpression != null )
99+ {
100+ sb . AppendLine ( prefix + Utility . CamelCaseEnumValue ( p . Name ) + " = " + CleanEnumInitValue ( p . InitExpression ) + "," ) ;
101+ }
102+ else
103+ {
104+ sb . AppendLine ( prefix + Utility . CamelCaseEnumValue ( p . Name ) + "," ) ;
105+ }
106+ }
107+
108+ }
109+
110+ private static void WriteTSInterfaceDefinition ( StringBuilder sb , string prefix , IEnumerable < IntellisenseProperty > props )
111+ {
107112 foreach ( var p in props )
108113 {
109- WriteTypeScriptComment ( p , sb ) ;
110- sb . AppendFormat ( "{0}\t {1}: " , prefix , Utility . CamelCasePropertyName ( p . NameWithOption ) ) ;
114+ WriteTypeScriptComment ( p , sb , prefix ) ;
115+ sb . AppendFormat ( "{0}{1}: " , prefix , Utility . CamelCasePropertyName ( p . NameWithOption ) ) ;
111116
112117 if ( p . Type . IsKnownType ) sb . Append ( p . Type . TypeScriptName ) ;
113118 else
114119 {
115120 if ( p . Type . Shape == null ) sb . Append ( "any" ) ;
116- else WriteTSInterfaceDefinition ( sb , prefix + " \t " , p . Type . Shape ) ;
121+ else WriteTSInterfaceDefinition ( sb , prefix , p . Type . Shape ) ;
117122 }
118123 if ( p . Type . IsArray ) sb . Append ( "[]" ) ;
119124
120125 sb . AppendLine ( ";" ) ;
121126 }
122-
123- sb . Append ( prefix ) . Append ( "}" ) ;
124127 }
125128 }
126129}
0 commit comments