Skip to content

Commit

Permalink
add typealias parser
Browse files Browse the repository at this point in the history
  • Loading branch information
yoli799480165 committed Dec 25, 2024
1 parent c2403c1 commit 5905e60
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
75 changes: 75 additions & 0 deletions generators/CssInCSharp.Generator/TypeScriptConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,14 @@ private SyntaxNodeOrList GenerateCSharpAst(Ts.TsTypes.INode node, NodeContext? c

return classDeclaration;
}
case Ts.TsTypes.SyntaxKind.IntersectionType:
{
var n = node.AsType<Ts.TsTypes.IntersectionTypeNode>();
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<Ts.TsTypes.NewExpression>();
Expand Down Expand Up @@ -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<Ts.TsTypes.TypeAliasDeclaration>();
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<MemberDeclarationSyntax>());
}
}
else
{
// add base class
var r = GenerateCSharpAst(n.Type);
var baseClasses = new List<SyntaxNodeOrToken>();
if (r.IsT2)
{
baseClasses = r.AsT2;
}
else
{
baseClasses.Add(r.AsT0);
}

if (baseClasses is { Count: > 0 })
{
classDeclaration = classDeclaration.WithBaseList
(
SyntaxFactory.BaseList(SyntaxFactory.SeparatedList<BaseTypeSyntax>(baseClasses))
);
}
}
return classDeclaration;
}
case Ts.TsTypes.SyntaxKind.TypeLiteral:
{
var n = node.AsType<Ts.TsTypes.TypeLiteralNode>();
return n.Members.Select(x => GenerateCSharpAst(x).AsT0).ToList();
}
case Ts.TsTypes.SyntaxKind.TypeReference:
{
var n = node.AsType<Ts.TsTypes.TypeReferenceNode>();
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<TypeSyntax>(args))));
}
else
{
return SyntaxFactory.SimpleBaseType(
SyntaxFactory.IdentifierName(n.IdentifierStr));
}
}
case Ts.TsTypes.SyntaxKind.FirstTemplateToken:
{
var n = node.AsType<Ts.TsTypes.NoSubstitutionTemplateLiteral>();
Expand Down

0 comments on commit 5905e60

Please sign in to comment.