Skip to content

Commit

Permalink
Create RegularType and remove NamespacedType and GenericType
Browse files Browse the repository at this point in the history
  • Loading branch information
YarinOmesi committed Aug 5, 2023
1 parent 1d5ebec commit 5086cd3
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 60 deletions.
18 changes: 10 additions & 8 deletions TestsHelper.SourceGenerator/CodeBuilding/Types/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ namespace TestsHelper.SourceGenerator.CodeBuilding.Types;

public static class Extensions
{
public static NamespacedType Type(this ITypeSymbol type) => type.ContainingNamespace.IsGlobalNamespace
? new NamespacedType(string.Empty, type.Name)
: new NamespacedType(type.GetNamespace(), type.Name);
public static RegularType Type(this ITypeSymbol type) => type.ContainingNamespace.IsGlobalNamespace
? new RegularType(string.Empty, type.Name)
: new RegularType(type.GetNamespace(), type.Name);

public static string MakeString(this IType type) => type.Build().NormalizeWhitespace().ToFullString();
public static NamespacedType Type(this string @namespace, string name) => new(@namespace, name);
public static GenericType Generic(this NamespacedType type, params IType[] typeArguments) => new(type, typeArguments);
public static GenericType Generic(this NamespacedType type, params ITypeSymbol[] typeArguments) => new(type, typeArguments.Select(Type).ToArray());
public static RegularType Type(this string @namespace, string name) => new(@namespace, name);
public static RegularType Generic(this RegularType type, params IType[] typeArguments) => type with {TypedArguments = typeArguments};
public static RegularType Generic(this RegularType type, params ITypeSymbol[] typeArguments) =>type with {TypedArguments = typeArguments.Select(Type).ToArray()};
public static NullableType Nullable(this IType type) => new(type);
public static IType Type(this TypeBuilder typeBuilder) => new TypeBuilderType(typeBuilder);

private record TypeBuilderType(TypeBuilder TypeBuilder) : IType
private sealed record TypeBuilderType(TypeBuilder TypeBuilder) : IType<SimpleNameSyntax>
{
public string Namespace => TypeBuilder.ParentFileBuilder.Namespace;
public string Name => TypeBuilder.Name;
public TypeSyntax Build() => new NamespacedType(Namespace, Name).Build();
public RegularType RegularType => new(Namespace, Name);
public SimpleNameSyntax Build() => RegularType.Build();
TypeSyntax IType.Build() => Build();
}
}
26 changes: 0 additions & 26 deletions TestsHelper.SourceGenerator/CodeBuilding/Types/GenericType.cs

This file was deleted.

6 changes: 6 additions & 0 deletions TestsHelper.SourceGenerator/CodeBuilding/Types/IType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ public interface IType
public string Name { get; }

public TypeSyntax Build();
}

public interface IType<out T> : IType
where T : TypeSyntax
{
public new T Build();
}
11 changes: 0 additions & 11 deletions TestsHelper.SourceGenerator/CodeBuilding/Types/NamespacedType.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace TestsHelper.SourceGenerator.CodeBuilding.Types;

[DebuggerDisplay("{Type}?")]
public record NullableType(IType Type) : IType
public record NullableType(IType Type) : IType<NullableTypeSyntax>
{
public string Namespace => Type.Namespace;

public string Name => Type.Name;
public TypeSyntax Build() => SyntaxFactory.NullableType(Type.Build());
public NullableTypeSyntax Build() => SyntaxFactory.NullableType(Type.Build());
TypeSyntax IType.Build() => Build();
}
29 changes: 29 additions & 0 deletions TestsHelper.SourceGenerator/CodeBuilding/Types/RegularType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace TestsHelper.SourceGenerator.CodeBuilding.Types;

[DebuggerDisplay("{Namespace}.{Name}`{TypedArguments.Count}")]
public record RegularType(string Namespace, string Name, IReadOnlyList<IType> TypedArguments) : IType<SimpleNameSyntax>
{
private static readonly IReadOnlyList<IType> EmptyList = new List<IType>();

public RegularType(string Namespace, string Name) : this(Namespace, Name, EmptyList)
{
}

TypeSyntax IType.Build() => Build();

public SimpleNameSyntax Build()
{
SimpleNameSyntax nameSyntax = TypedArguments.Count switch {
0 => SyntaxFactory.IdentifierName(Name),
_ => SyntaxFactory.GenericName(Name).AddTypeArgumentListArguments(TypedArguments.Select(type => type.Build()).ToArray())
};

return nameSyntax;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Generate(TypeBuilder builder, IType dependencyTypeName, IMethodSymbo
builder.Name = $"Method_{method.Name}";
builder.Public();

GenericType moqCallbackType = method.ReturnType.SpecialType == SpecialType.System_Void
RegularType moqCallbackType = method.ReturnType.SpecialType == SpecialType.System_Void
? CommonTypes.SystemAction.Generic(dependencyTypeName)
: CommonTypes.SystemFunc.Generic(dependencyTypeName, method.ReturnType.Type());

Expand Down Expand Up @@ -45,7 +45,7 @@ public void Generate(TypeBuilder builder, IType dependencyTypeName, IMethodSymbo
string patchedExpression = Cyber_CretePatchedExpression(method, expressionField.Name, converterField.Name);

// Setup()
var setupReturnType = new GenericType(Moq.ISetup, moqCallbackType.TypedArguments);
var setupReturnType = Moq.ISetup with {TypedArguments = moqCallbackType.TypedArguments};

var setupBuilder = MethodBuilder.Create(setupReturnType, "Setup", parameters).Add(builder)
.Public();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private static IReadOnlyList<IType> FindAllTypesFromType(IType type)
{
List<IType> types = new List<IType>();
if (type is not VoidType) types.Add(type);
if (type is GenericType genericType) types.AddRange(genericType.TypedArguments.SelectMany(FindAllTypesFromType));
if (type is RegularType genericType) types.AddRange(genericType.TypedArguments.SelectMany(FindAllTypesFromType));
return types;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ namespace TestsHelper.SourceGenerator.MockFilling.PartialImplementation.Types;
public static class CommonTypes
{
public static readonly string SystemLinqExpressions = "System.Linq.Expressions";
public static readonly NamespacedType LinqExpression = SystemLinqExpressions.Type("Expression");
public static readonly RegularType LinqExpression = SystemLinqExpressions.Type("Expression");

public static readonly string MockWrappingConverters = "TestsHelper.SourceGenerator.MockWrapping.Converters";
public static readonly NamespacedType IValueConverter = MockWrappingConverters.Type("IValueConverter");
public static readonly NamespacedType MoqValueConverter = MockWrappingConverters.Type("MoqValueConverter");
public static readonly NamespacedType Value = "TestsHelper.SourceGenerator.MockWrapping".Type("Value");
public static readonly RegularType IValueConverter = MockWrappingConverters.Type("IValueConverter");
public static readonly RegularType MoqValueConverter = MockWrappingConverters.Type("MoqValueConverter");
public static readonly RegularType Value = "TestsHelper.SourceGenerator.MockWrapping".Type("Value");

public static readonly string System = "System";
public static readonly NamespacedType SystemFunc = System.Type("Func");
public static readonly NamespacedType SystemAction = System.Type("Action");

public static readonly RegularType SystemFunc = System.Type("Func");
public static readonly RegularType SystemAction = System.Type("Action");
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class Moq
{
public static readonly string Namespace = "Moq";
public static readonly string MoqLanguageFlow = "Moq.Language.Flow";
public static readonly NamespacedType Mock = Namespace.Type("Mock");
public static readonly NamespacedType Times = Namespace.Type("Times");
public static readonly NamespacedType ISetup = MoqLanguageFlow.Type("ISetup");
public static readonly RegularType Mock = Namespace.Type("Mock");
public static readonly RegularType Times = Namespace.Type("Times");
public static readonly RegularType ISetup = MoqLanguageFlow.Type("ISetup");
}

0 comments on commit 5086cd3

Please sign in to comment.