Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Dec 20, 2024
1 parent 5fed627 commit 0188a9d
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 200 deletions.
2 changes: 1 addition & 1 deletion Immutype.Tests/Integration/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static CSharpCompilation CreateCompilation() =>
MetadataReference.CreateFromFile(typeof(Uri).Assembly.Location),
MetadataReference.CreateFromFile(typeof(SourceBuilder).Assembly.Location));

public static IReadOnlyList<string> Run(this string setupCode, out string generatedCode, RunOptions? options = default)
public static IReadOnlyList<string> Run(this string setupCode, out string generatedCode, RunOptions? options = null)
{
var curOptions = options ?? new RunOptions();
var parseOptions = CSharpParseOptions.Default.WithLanguageVersion(curOptions.LanguageVersion);
Expand Down
2 changes: 1 addition & 1 deletion Immutype.UsageScenarios.Tests/ExplicitConstructorChoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal readonly struct Person
public Person(
string name,
int age = 0,
IImmutableList<Person>? friends = default)
IImmutableList<Person>? friends = null)
{
Name = name;
Age = age;
Expand Down
2 changes: 1 addition & 1 deletion Immutype.UsageScenarios.Tests/GenericTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Immutype.UsageScenarios.Tests.GenericTypes
// $header=It is possible to use generic types including any generic constraints.
// {
[Immutype.Target]
internal record Person<TAge>(string Name, TAge Age = default, IEnumerable<Person<TAge>>? Friends = default)
internal record Person<TAge>(string Name, TAge Age = default, IEnumerable<Person<TAge>>? Friends = null)
where TAge : struct;

public class GenericTypes
Expand Down
2 changes: 1 addition & 1 deletion Immutype.UsageScenarios.Tests/ImmutableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal readonly struct Person
public Person(
string name,
int age = 0,
IImmutableList<Person>? friends = default)
IImmutableList<Person>? friends = null)
{
Name = name;
Age = age;
Expand Down
4 changes: 2 additions & 2 deletions Immutype.UsageScenarios.Tests/NullableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ namespace Immutype.UsageScenarios.Tests.NullableCollection
[Immutype.Target]
internal record Person(
string Name,
int? Age = default,
ICollection<Person>? Friends = default);
int? Age = null,
ICollection<Person>? Friends = null);

public class NullableCollection
{
Expand Down
4 changes: 2 additions & 2 deletions Immutype.UsageScenarios.Tests/RecordWithConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ internal record Person
{
public Person(
string name,
int? age = default,
ICollection<Person>? friends = default)
int? age = null,
ICollection<Person>? friends = null)
{
Name = name;
Age = age;
Expand Down
2 changes: 1 addition & 1 deletion Immutype.UsageScenarios.Tests/Set.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Immutype.UsageScenarios.Tests.Set
internal record Person(
string Name,
int Age = 0,
ISet<Person>? Friends = default);
ISet<Person>? Friends = null);

public class Set
{
Expand Down
15 changes: 10 additions & 5 deletions Immutype/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ namespace Immutype;

using Core;
using Pure.DI;
using static Pure.DI.Lifetime;
using static Pure.DI.Tag;

internal partial class Composition
{
private static void Setup() => DI.Setup()
.Root<(ISourceBuilder SourceBuilder, IComponentsBuilder ComponentsBuilder, ITypeSyntaxFilter SyntaxFilter)>(nameof(Root))
.DefaultLifetime(Lifetime.PerBlock)
.Bind().To((TT[] arr) => ImmutableArray.Create(arr))
.Root<(
ISourceBuilder SourceBuilder,
IComponentsBuilder ComponentsBuilder,
ITypeSyntaxFilter SyntaxFilter)>(nameof(Root))

.DefaultLifetime(PerBlock)
.Bind().To<SourceBuilder>()
.Bind().To<SyntaxNodeFactory>()
.Bind().To<NameService>()
.Bind().To<ExtensionsFactory>()
.Bind().To<MethodsFactory>()
.Bind().To<DataContainerFactory>()
.Bind(Tag.Type).To<MethodWithFactory>()
.Bind(Tag.Type).To<MethodAddRemoveFactory>()
.Bind(Unique).To<MethodWithFactory>()
.Bind(Unique).To<MethodAddRemoveFactory>()
.Bind().To<CommentsGenerator>()
.Bind().To<Information>()
.Bind().To<Comments>()
Expand Down
84 changes: 22 additions & 62 deletions Immutype/Core/DataContainerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,34 @@ internal class DataContainerFactory : IDataContainerFactory
{
private static readonly Dictionary<string, string> GenericTypeMap = new()
{
{
"List", "List"
},
{
"IEnumerable", "List"
},
{
"IReadOnlyCollection", "List"
},
{
"IReadOnlyList", "List"
},
{
"ICollection", "List"
},
{
"IList", "List"
},
{
"HashSet", "HashSet"
},
{
"ISet", "HashSet"
},
{
"Queue", "Queue"
},
{
"Stack", "Stack"
}
{ "List", "List" },
{ "IEnumerable", "List" },
{ "IReadOnlyCollection", "List" },
{ "IReadOnlyList", "List" },
{ "ICollection", "List" },
{ "IList", "List" },
{ "HashSet", "HashSet" },
{ "ISet", "HashSet" },
{ "Queue", "Queue" },
{ "Stack", "Stack" }
};

private static readonly Dictionary<string, string> ReadonlyTypeMap = new()
{
{
"IReadOnlyCollection", "List"
},
{
"IReadOnlyList", "List"
},
{
"IReadOnlySet", "List"
}
{ "IReadOnlyCollection", "List" },
{ "IReadOnlyList", "List" },
{ "IReadOnlySet", "List" }
};

private static readonly Dictionary<string, string> ImmutableTypeMap = new()
{
{
"ImmutableList", "ImmutableList"
},
{
"IImmutableList", "ImmutableList"
},
{
"ImmutableArray", "ImmutableArray"
},
{
"ImmutableQueue", "ImmutableQueue"
},
{
"IImmutableQueue", "ImmutableQueue"
},
{
"ImmutableStack", "ImmutableStack"
},
{
"IImmutableStack", "ImmutableStack"
}
{ "ImmutableList", "ImmutableList" },
{ "IImmutableList", "ImmutableList" },
{ "ImmutableArray", "ImmutableArray" },
{ "ImmutableQueue", "ImmutableQueue" },
{ "IImmutableQueue", "ImmutableQueue" },
{ "ImmutableStack", "ImmutableStack" },
{ "IImmutableStack", "ImmutableStack" }
};

public bool TryCreate(GenericNameSyntax genericNameSyntax, ref ExpressionSyntax? expressionSyntax, ref ParameterSyntax argumentParameter)
Expand Down Expand Up @@ -111,7 +71,7 @@ public bool TryCreate(GenericNameSyntax genericNameSyntax, ref ExpressionSyntax?
if (ImmutableTypeMap.TryGetValue(genericNameSyntax.Identifier.Text, out var immutableTypeName))
{
var immutableDataTypeName = SyntaxFactory.IdentifierName(SyntaxFactory.Identifier($"System.Collections.Immutable.{immutableTypeName}"));
if (expressionSyntax != default)
if (expressionSyntax != null)
{
expressionSyntax = SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberAccessExpression(
Expand Down Expand Up @@ -142,7 +102,7 @@ private static ExpressionSyntax CreateGenericContainer(ExpressionSyntax? express
.AddTypeArgumentListArguments(elementType);

var result = SyntaxRepo.ObjectCreationExpression(genericDataType);
return expressionSyntax != default
return expressionSyntax != null
? result.AddArgumentListArguments(SyntaxFactory.Argument(expressionSyntax))
: result.AddArgumentListArguments();
}
Expand Down
8 changes: 4 additions & 4 deletions Immutype/Core/ExtensionsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public IEnumerable<Source> Create(GenerationContext<TypeDeclarationSyntax> conte
private static CompilationUnitSyntax CreateRootNode(SyntaxNode targetNode, UsingDirectiveSyntax[] additionalUsings, params MemberDeclarationSyntax[] members)
{
var namespaces = targetNode.Ancestors().OfType<NamespaceType>();
NamespaceType? rootNamespace = default;
NamespaceType? rootNamespace = null;
foreach (var ns in namespaces)
{
var nextNs = ns.WithMembers(new SyntaxList<MemberDeclarationSyntax>([]));
rootNamespace = rootNamespace == default
rootNamespace = rootNamespace == null
? nextNs.AddMembers(members).AddUsings(GetUsings(nextNs.Usings, additionalUsings))
: nextNs.AddMembers(rootNamespace);
}
Expand All @@ -82,7 +82,7 @@ private static CompilationUnitSyntax CreateRootNode(SyntaxNode targetNode, Using
var rootCompilationUnit = (baseCompilationUnit ?? SyntaxFactory.CompilationUnit())
.WithMembers(new SyntaxList<MemberDeclarationSyntax>([]));

return rootNamespace != default
return rootNamespace != null
? rootCompilationUnit.AddMembers(rootNamespace)
: rootCompilationUnit.AddUsings(GetUsings(rootCompilationUnit.Usings, additionalUsings)).AddMembers(members);
}
Expand All @@ -96,7 +96,7 @@ private static UsingDirectiveSyntax[] GetUsings(IEnumerable<UsingDirectiveSyntax
private static ClassDeclarationSyntax TryAddAttribute(SemanticModel semanticModel, ClassDeclarationSyntax classDeclarationSyntax, string attributeClassName)
{
var excludeFromCodeCoverageType = semanticModel.Compilation.GetTypeByMetadataName(attributeClassName + "Attribute");
if (excludeFromCodeCoverageType != default)
if (excludeFromCodeCoverageType != null)
{
classDeclarationSyntax = classDeclarationSyntax.WithNewLine()
.AddAttributeLists(SyntaxFactory.AttributeList().AddAttributes(SyntaxFactory.Attribute(SyntaxFactory.IdentifierName(attributeClassName))))
Expand Down
7 changes: 5 additions & 2 deletions Immutype/Core/Information.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ internal sealed class Information : IInformation

static Information()
{
var assembly = typeof(Information).Assembly;
var version = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
var version = typeof(Information)
.Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion;

if (!string.IsNullOrWhiteSpace(version))
{
CurrentDescription = $"{CurrentDescription} {version}";
Expand Down
Loading

0 comments on commit 0188a9d

Please sign in to comment.