diff --git a/generators/CssInCSharp.Generator/Extensions/NodeExtensions.cs b/generators/CssInCSharp.Generator/Extensions/NodeExtensions.cs index 136c887..fa36ad5 100644 --- a/generators/CssInCSharp.Generator/Extensions/NodeExtensions.cs +++ b/generators/CssInCSharp.Generator/Extensions/NodeExtensions.cs @@ -34,6 +34,7 @@ public static bool IsMember(this Ts.TsTypes.INode node) case Ts.TsTypes.SyntaxKind.FunctionDeclaration: case Ts.TsTypes.SyntaxKind.PropertySignature: case Ts.TsTypes.SyntaxKind.ExportAssignment: + case Ts.TsTypes.SyntaxKind.TypeAliasDeclaration: return true; default: return false; } diff --git a/generators/CssInCSharp.Generator/TypeScriptConverter.cs b/generators/CssInCSharp.Generator/TypeScriptConverter.cs index 28e2883..cedb3d8 100644 --- a/generators/CssInCSharp.Generator/TypeScriptConverter.cs +++ b/generators/CssInCSharp.Generator/TypeScriptConverter.cs @@ -320,6 +320,14 @@ private SyntaxNodeOrList GenerateCSharpAst(Ts.TsTypes.INode node, NodeContext? c return classDeclaration; } + case Ts.TsTypes.SyntaxKind.IntersectionType: + { + var n = node.AsType(); + var types = n.Types.Where(x => x.Kind != Ts.TsTypes.SyntaxKind.TypeLiteral) + .Select(x => (SyntaxNodeOrToken)GenerateCSharpAst(x).AsT0) + .Separate(SyntaxFactory.Token(SyntaxKind.CommaToken)).ToList(); + return types; + } case Ts.TsTypes.SyntaxKind.NewExpression: { var n = node.AsType(); @@ -529,6 +537,73 @@ private SyntaxNodeOrList GenerateCSharpAst(Ts.TsTypes.INode node, NodeContext? c return SyntaxFactory.LiteralExpression( SyntaxKind.TrueLiteralExpression); } + case Ts.TsTypes.SyntaxKind.TypeAliasDeclaration: + { + var n = node.AsType(); + var classDeclaration = SyntaxFactory.ClassDeclaration(Format(n.IdentifierStr)).AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)); + if (n.Type.Kind == Ts.TsTypes.SyntaxKind.UnionType) + { + // todo: how to handle UnionType + return default; + } + if (n.Type.Kind == Ts.TsTypes.SyntaxKind.TypeLiteral) + { + // add members + var members = GenerateCSharpAst(n.Type).AsT1; + foreach (var member in members) + { + classDeclaration = classDeclaration.AddMembers(member.AsType()); + } + } + else + { + // add base class + var r = GenerateCSharpAst(n.Type); + var baseClasses = new List(); + if (r.IsT2) + { + baseClasses = r.AsT2; + } + else + { + baseClasses.Add(r.AsT0); + } + + if (baseClasses is { Count: > 0 }) + { + classDeclaration = classDeclaration.WithBaseList + ( + SyntaxFactory.BaseList(SyntaxFactory.SeparatedList(baseClasses)) + ); + } + } + return classDeclaration; + } + case Ts.TsTypes.SyntaxKind.TypeLiteral: + { + var n = node.AsType(); + return n.Members.Select(x => GenerateCSharpAst(x).AsT0).ToList(); + } + case Ts.TsTypes.SyntaxKind.TypeReference: + { + var n = node.AsType(); + if (n.TypeArguments is { Count: > 0 }) + { + var args = n.TypeArguments + .Select(x => (SyntaxNodeOrToken)SyntaxFactory.IdentifierName(x.GetText().Purify())) + .Separate(SyntaxFactory.Token(SyntaxKind.CommaToken)); + + return SyntaxFactory.SimpleBaseType(SyntaxFactory + .GenericName(n.IdentifierStr) + .WithTypeArgumentList( + SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList(args)))); + } + else + { + return SyntaxFactory.SimpleBaseType( + SyntaxFactory.IdentifierName(n.IdentifierStr)); + } + } case Ts.TsTypes.SyntaxKind.FirstTemplateToken: { var n = node.AsType();