Skip to content

Commit

Permalink
add regex replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
yoli799480165 committed Dec 25, 2024
1 parent 5905e60 commit c5481e1
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 25 deletions.
74 changes: 74 additions & 0 deletions generators/CssInCSharp.Generator/CSharpOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Text.RegularExpressions;

namespace CssInCSharp.Generator;

public class CSharpOptions
{
public List<string> Usings { get; set; } = ["System", "CssInCSharp"];
public string Namespace { get; set; } = "CssInCSharp";
public string DefaultReturnType { get; set; } = "object";
public string DefaultParameterType { get; set; } = "object";
public string DefaultFieldType { get; set; } = "object";
public string DefaultClassName { get; set; } = "GeneratedStyle";
public string DefaultExportMethodName { get; set; } = "Default";
public bool UsePartialClass { get; set; } = false;
public bool UseStaticMethod { get; set; } = false;
public bool UsePascalCase { get; set; } = true;
public List<Replacement> Replacements { get; set; } = [];

public string Replace(string input)
{
if (Replacements.Count <= 0)
{
return input;
}
var context = new Dictionary<string, string>
{
{ nameof(Namespace), Namespace },
{ nameof(DefaultReturnType), DefaultReturnType },
{ nameof(DefaultParameterType), DefaultParameterType },
{ nameof(DefaultFieldType), DefaultFieldType },
{ nameof(DefaultClassName), DefaultClassName },
{ nameof(DefaultExportMethodName), DefaultExportMethodName },
};

foreach (var item in Replacements)
{
input = item.Replace(input, context);
}

return input;
}
}

public class Replacement
{
public string Pattern { get; set; } = default!;

public string Value { get; set; } = default!;

public Replacement()
{
}

public Replacement(string pattern, string value)
{
Pattern = pattern;
Value = value;
}

public string Replace(string input, Dictionary<string, string> context)
{
var match = Regex.Match(Value, @"\w*(\{(\w+)\})\w*");
if (match is { Success: true, Groups.Count: >= 3 })
{
var replace = match.Groups[1].ToString();
var key = match.Groups[2].ToString();
if (context.TryGetValue(key, out var value))
{
Value = Value.Replace(replace, value);
}
}
return Regex.Replace(input, Pattern, Value);
}
}
46 changes: 21 additions & 25 deletions generators/CssInCSharp.Generator/TypeScriptConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@

namespace CssInCSharp.Generator
{
public class CSharpOptions
{
public List<string> Usings { get; set; } = ["System", "CssInCSharp"];
public string Namespace { get; set; } = "CssInCSharp";
public string DefaultReturnType { get; set; } = "object";
public string DefaultParameterType { get; set; } = "object";
public string DefaultFieldType { get; set; } = "object";
public string DefaultClassName { get; set; } = "GeneratedStyle";
public string DefaultExportMethodName { get; set; } = "Default";
public bool UsePartialClass { get; set; } = false;
public bool UseStaticMethod { get; set; } = false;
public bool UsePascalCase { get; set; } = false;
}

public class TypeScriptConverter : IConverter
{
private readonly CSharpOptions _options;
Expand All @@ -33,7 +19,8 @@ public string Convert(string content, string fileName)
{
var tsAst = new Ts.TypeScriptAST(content, fileName);
var csAst = Generate(tsAst.RootNode);
return csAst.NormalizeWhitespace().ToFullString();
var code = csAst.NormalizeWhitespace().ToFullString();
return _options.Replace(code);
}

private CompilationUnitSyntax Generate(Ts.TsTypes.INode node)
Expand Down Expand Up @@ -356,7 +343,7 @@ private SyntaxNodeOrList GenerateCSharpAst(Ts.TsTypes.INode node, NodeContext? c
{
var item = (el as Ts.TsTypes.BindingElement)!;
var name = item.Name.GetText();
var property = item.PropertyName?.GetText() ?? name;
var property = Format(item.PropertyName?.GetText() ?? name);
var variable = SyntaxFactory
.VariableDeclaration(SyntaxFactory.ParseTypeName("var"))
.AddVariables
Expand Down Expand Up @@ -419,28 +406,37 @@ private SyntaxNodeOrList GenerateCSharpAst(Ts.TsTypes.INode node, NodeContext? c
return SyntaxFactory.ConditionalAccessExpression(
SyntaxFactory.IdentifierName(context.ConditionalToken),
SyntaxFactory.MemberBindingExpression(
SyntaxFactory.IdentifierName(n.Name.GetText())));
SyntaxFactory.IdentifierName(Format(n.Name.GetText()))));
}
return SyntaxFactory.MemberAccessExpression
(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName(n.Expression.GetText()),
SyntaxFactory.IdentifierName(n.Name.GetText())
SyntaxFactory.IdentifierName(Format(n.Name.GetText()))
);
}
case Ts.TsTypes.SyntaxKind.PropertyAssignment:
{
var n = node.AsType<Ts.TsTypes.PropertyAssignment>();
var initializer = n.Initializer;
var left = GenerateCSharpAst(n.Name, context).AsType<ExpressionSyntax>();
if (n.Name.IsIndexerProperty())
ExpressionSyntax left;
if (n.Name.Kind == Ts.TsTypes.SyntaxKind.Identifier)
{
left = SyntaxFactory.ElementAccessExpression(SyntaxFactory.IdentifierName(""))
.WithArgumentList(
SyntaxFactory.BracketedArgumentList(
SyntaxFactory.SingletonSeparatedList(
SyntaxFactory.Argument(left))));
left = SyntaxFactory.IdentifierName(Format(n.Name.GetText()));
}
else
{
left = GenerateCSharpAst(n.Name, context).AsType<ExpressionSyntax>();
if (n.Name.IsIndexerProperty())
{
left = SyntaxFactory.ElementAccessExpression(SyntaxFactory.IdentifierName(""))
.WithArgumentList(
SyntaxFactory.BracketedArgumentList(
SyntaxFactory.SingletonSeparatedList(
SyntaxFactory.Argument(left))));
}
}

var right = GenerateCSharpAst(initializer, context).AsType<ExpressionSyntax>();
return SyntaxFactory.AssignmentExpression
(
Expand Down

0 comments on commit c5481e1

Please sign in to comment.