@@ -8,12 +8,15 @@ namespace CssInCSharp.Generator
88{
99 public class CSharpOptions
1010 {
11- public List < string > Usings { get ; set ; } = [ "System" ] ;
11+ public List < string > Usings { get ; set ; } = [ "System" , "CssInCSharp" ] ;
1212 public string Namespace { get ; set ; } = "CssInCSharp" ;
1313 public string DefaultReturnType { get ; set ; } = "object" ;
1414 public string DefaultParameterType { get ; set ; } = "object" ;
1515 public string DefaultFieldType { get ; set ; } = "object" ;
16- public string ? DefaultClassName { get ; set ; }
16+ public string DefaultClassName { get ; set ; } = "GeneratedStyle" ;
17+ public bool UsePartialClass { get ; set ; } = false ;
18+ public bool UseStaticMethod { get ; set ; } = false ;
19+ public bool UsePascalCase { get ; set ; } = false ;
1720 }
1821
1922 public class TypeScriptConverter : IConverter
@@ -27,21 +30,27 @@ public TypeScriptConverter(CSharpOptions options = null)
2730
2831 public string Convert ( string content , string fileName )
2932 {
30- var usings = GenerateUsings ( ) ;
31- var ast = new Ts . TypeScriptAST ( content , fileName ) ;
32- var member = GenerateMemberDeclaration ( ast . RootNode ) ;
33- return SyntaxFactory . CompilationUnit ( )
34- . WithUsings ( usings )
35- . AddMembers ( member )
36- . NormalizeWhitespace ( )
37- . ToFullString ( ) ;
33+ var tsAst = new Ts . TypeScriptAST ( content , fileName ) ;
34+ var csAst = Generate ( tsAst . RootNode ) ;
35+ return csAst . NormalizeWhitespace ( ) . ToFullString ( ) ;
3836 }
3937
40- private SyntaxList < UsingDirectiveSyntax > GenerateUsings ( )
38+ private CompilationUnitSyntax Generate ( Ts . TsTypes . INode node )
4139 {
42- var usings = _options . Usings . Select ( x => SyntaxFactory . UsingDirective (
43- SyntaxFactory . ParseName ( x ) ) ) ;
44- return SyntaxFactory . List < UsingDirectiveSyntax > ( usings ) ;
40+ // usings
41+ var usings = SyntaxFactory . List < UsingDirectiveSyntax > ( _options . Usings . Select ( x => SyntaxFactory . UsingDirective (
42+ SyntaxFactory . ParseName ( x ) ) ) ) ;
43+
44+ // namespace
45+ var @namespace = SyntaxFactory . NamespaceDeclaration ( SyntaxFactory . ParseName ( _options . Namespace ) ) ;
46+
47+ // members
48+ var members = GenerateMemberDeclaration ( node ) ;
49+ @namespace = @namespace . AddMembers ( members ) ;
50+
51+ return SyntaxFactory . CompilationUnit ( )
52+ . WithUsings ( usings )
53+ . AddMembers ( @namespace ) ;
4554 }
4655
4756 private MemberDeclarationSyntax ? GenerateMemberDeclaration ( Ts . TsTypes . INode node )
@@ -154,9 +163,12 @@ private SyntaxNodeOrList GenerateCSharpAst(Ts.TsTypes.INode node, NodeContext? c
154163 }
155164 else
156165 {
157- var methodDeclaration = SyntaxFactory . MethodDeclaration (
158- SyntaxFactory . ParseTypeName ( returnType ) , funcName )
159- . AddModifiers ( SyntaxFactory . Token ( SyntaxKind . PublicKeyword ) ) ;
166+ SyntaxToken [ ] tokens = _options . UseStaticMethod
167+ ? [ SyntaxFactory . Token ( SyntaxKind . PublicKeyword ) , SyntaxFactory . Token ( SyntaxKind . StaticKeyword ) ]
168+ : [ SyntaxFactory . Token ( SyntaxKind . PublicKeyword ) ] ;
169+ var methodDeclaration = SyntaxFactory
170+ . MethodDeclaration ( SyntaxFactory . ParseTypeName ( returnType ) , Format ( funcName ) )
171+ . AddModifiers ( tokens ) ;
160172 methodDeclaration = methodDeclaration . AddParameterListParameters ( parameters ) ;
161173 return methodDeclaration . WithBody ( SyntaxFactory . Block ( statements ) ) ;
162174 }
@@ -420,7 +432,7 @@ private SyntaxNodeOrList GenerateCSharpAst(Ts.TsTypes.INode node, NodeContext? c
420432 {
421433 var n = node . AsType < Ts . TsTypes . PropertySignature > ( ) ;
422434 return SyntaxFactory
423- . PropertyDeclaration ( SyntaxFactory . ParseTypeName ( "string" ) , n . IdentifierStr )
435+ . PropertyDeclaration ( GetType ( n . Type ) , Format ( n . IdentifierStr ) )
424436 . AddModifiers ( SyntaxFactory . Token ( SyntaxKind . PublicKeyword ) )
425437 . AddAccessorListAccessors
426438 (
@@ -462,7 +474,10 @@ private SyntaxNodeOrList GenerateCSharpAst(Ts.TsTypes.INode node, NodeContext? c
462474 }
463475 case Ts . TsTypes . SyntaxKind . SourceFile :
464476 {
465- return SyntaxFactory . NamespaceDeclaration ( SyntaxFactory . ParseName ( _options . Namespace ) ) ;
477+ SyntaxToken [ ] tokens = _options . UsePartialClass
478+ ? [ SyntaxFactory . Token ( SyntaxKind . PublicKeyword ) , SyntaxFactory . Token ( SyntaxKind . PartialKeyword ) ]
479+ : [ SyntaxFactory . Token ( SyntaxKind . PublicKeyword ) ] ;
480+ return SyntaxFactory . ClassDeclaration ( Format ( _options . DefaultClassName ) ) . AddModifiers ( tokens ) ;
466481 }
467482 case Ts . TsTypes . SyntaxKind . StringLiteral :
468483 {
@@ -580,7 +595,30 @@ private SyntaxKind GenerateOperatorToken(Ts.TsTypes.INode node)
580595 }
581596 }
582597
583- static int GetLineNumber ( string text , int index )
598+ private TypeSyntax GetType ( Ts . TsTypes . INode node )
599+ {
600+ switch ( node . Kind )
601+ {
602+ case Ts . TsTypes . SyntaxKind . NumberKeyword :
603+ return SyntaxFactory . ParseTypeName ( "double" ) ;
604+ case Ts . TsTypes . SyntaxKind . StringKeyword :
605+ return SyntaxFactory . ParseTypeName ( "double" ) ;
606+ default :
607+ return SyntaxFactory . ParseTypeName ( "object" ) ;
608+ }
609+ }
610+
611+ private string Format ( string text )
612+ {
613+ if ( _options . UsePascalCase )
614+ {
615+ return text . ToPascalCase ( ) ;
616+ }
617+
618+ return text ;
619+ }
620+
621+ private static int GetLineNumber ( string text , int index )
584622 {
585623 if ( index < 0 || index > text . Length )
586624 throw new ArgumentOutOfRangeException ( nameof ( index ) , "Index is out of the range of the text." ) ;
0 commit comments