From 0188a9dcd2895b04d29a4bf8df091addc41913e5 Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Fri, 20 Dec 2024 15:27:45 +0300 Subject: [PATCH] Code cleanup --- Immutype.Tests/Integration/TestExtensions.cs | 2 +- .../ExplicitConstructorChoice.cs | 2 +- Immutype.UsageScenarios.Tests/GenericTypes.cs | 2 +- .../ImmutableCollection.cs | 2 +- .../NullableCollection.cs | 4 +- .../RecordWithConstructor.cs | 4 +- Immutype.UsageScenarios.Tests/Set.cs | 2 +- Immutype/Composition.cs | 15 ++- Immutype/Core/DataContainerFactory.cs | 84 ++++--------- Immutype/Core/ExtensionsFactory.cs | 8 +- Immutype/Core/Information.cs | 7 +- Immutype/Core/MethodAddRemoveFactory.cs | 118 ++++++++---------- Immutype/Core/MethodWithFactory.cs | 8 +- Immutype/Core/MethodsFactory.cs | 20 ++- Immutype/Core/Source.cs | 14 +-- Immutype/Core/SourceBuilder.cs | 30 ++--- Immutype/Core/SyntaxRepo.cs | 12 +- Immutype/Immutype.csproj | 2 +- 18 files changed, 136 insertions(+), 200 deletions(-) diff --git a/Immutype.Tests/Integration/TestExtensions.cs b/Immutype.Tests/Integration/TestExtensions.cs index 0767da1..979d085 100644 --- a/Immutype.Tests/Integration/TestExtensions.cs +++ b/Immutype.Tests/Integration/TestExtensions.cs @@ -31,7 +31,7 @@ private static CSharpCompilation CreateCompilation() => MetadataReference.CreateFromFile(typeof(Uri).Assembly.Location), MetadataReference.CreateFromFile(typeof(SourceBuilder).Assembly.Location)); - public static IReadOnlyList Run(this string setupCode, out string generatedCode, RunOptions? options = default) + public static IReadOnlyList Run(this string setupCode, out string generatedCode, RunOptions? options = null) { var curOptions = options ?? new RunOptions(); var parseOptions = CSharpParseOptions.Default.WithLanguageVersion(curOptions.LanguageVersion); diff --git a/Immutype.UsageScenarios.Tests/ExplicitConstructorChoice.cs b/Immutype.UsageScenarios.Tests/ExplicitConstructorChoice.cs index 499c770..2cc8575 100644 --- a/Immutype.UsageScenarios.Tests/ExplicitConstructorChoice.cs +++ b/Immutype.UsageScenarios.Tests/ExplicitConstructorChoice.cs @@ -29,7 +29,7 @@ internal readonly struct Person public Person( string name, int age = 0, - IImmutableList? friends = default) + IImmutableList? friends = null) { Name = name; Age = age; diff --git a/Immutype.UsageScenarios.Tests/GenericTypes.cs b/Immutype.UsageScenarios.Tests/GenericTypes.cs index a4e1ecd..84a667a 100644 --- a/Immutype.UsageScenarios.Tests/GenericTypes.cs +++ b/Immutype.UsageScenarios.Tests/GenericTypes.cs @@ -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(string Name, TAge Age = default, IEnumerable>? Friends = default) + internal record Person(string Name, TAge Age = default, IEnumerable>? Friends = null) where TAge : struct; public class GenericTypes diff --git a/Immutype.UsageScenarios.Tests/ImmutableCollection.cs b/Immutype.UsageScenarios.Tests/ImmutableCollection.cs index e15e4cc..002c565 100644 --- a/Immutype.UsageScenarios.Tests/ImmutableCollection.cs +++ b/Immutype.UsageScenarios.Tests/ImmutableCollection.cs @@ -26,7 +26,7 @@ internal readonly struct Person public Person( string name, int age = 0, - IImmutableList? friends = default) + IImmutableList? friends = null) { Name = name; Age = age; diff --git a/Immutype.UsageScenarios.Tests/NullableCollection.cs b/Immutype.UsageScenarios.Tests/NullableCollection.cs index acac8ff..14c6344 100644 --- a/Immutype.UsageScenarios.Tests/NullableCollection.cs +++ b/Immutype.UsageScenarios.Tests/NullableCollection.cs @@ -19,8 +19,8 @@ namespace Immutype.UsageScenarios.Tests.NullableCollection [Immutype.Target] internal record Person( string Name, - int? Age = default, - ICollection? Friends = default); + int? Age = null, + ICollection? Friends = null); public class NullableCollection { diff --git a/Immutype.UsageScenarios.Tests/RecordWithConstructor.cs b/Immutype.UsageScenarios.Tests/RecordWithConstructor.cs index 5b6fd2e..9f87eaa 100644 --- a/Immutype.UsageScenarios.Tests/RecordWithConstructor.cs +++ b/Immutype.UsageScenarios.Tests/RecordWithConstructor.cs @@ -21,8 +21,8 @@ internal record Person { public Person( string name, - int? age = default, - ICollection? friends = default) + int? age = null, + ICollection? friends = null) { Name = name; Age = age; diff --git a/Immutype.UsageScenarios.Tests/Set.cs b/Immutype.UsageScenarios.Tests/Set.cs index 11d9781..8159426 100644 --- a/Immutype.UsageScenarios.Tests/Set.cs +++ b/Immutype.UsageScenarios.Tests/Set.cs @@ -19,7 +19,7 @@ namespace Immutype.UsageScenarios.Tests.Set internal record Person( string Name, int Age = 0, - ISet? Friends = default); + ISet? Friends = null); public class Set { diff --git a/Immutype/Composition.cs b/Immutype/Composition.cs index 3f79dc2..c6ec3bd 100644 --- a/Immutype/Composition.cs +++ b/Immutype/Composition.cs @@ -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() .Bind().To() .Bind().To() .Bind().To() .Bind().To() .Bind().To() - .Bind(Tag.Type).To() - .Bind(Tag.Type).To() + .Bind(Unique).To() + .Bind(Unique).To() .Bind().To() .Bind().To() .Bind().To() diff --git a/Immutype/Core/DataContainerFactory.cs b/Immutype/Core/DataContainerFactory.cs index a42d9e4..627c7b0 100644 --- a/Immutype/Core/DataContainerFactory.cs +++ b/Immutype/Core/DataContainerFactory.cs @@ -6,74 +6,34 @@ internal class DataContainerFactory : IDataContainerFactory { private static readonly Dictionary 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 ReadonlyTypeMap = new() { - { - "IReadOnlyCollection", "List" - }, - { - "IReadOnlyList", "List" - }, - { - "IReadOnlySet", "List" - } + { "IReadOnlyCollection", "List" }, + { "IReadOnlyList", "List" }, + { "IReadOnlySet", "List" } }; private static readonly Dictionary 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) @@ -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( @@ -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(); } diff --git a/Immutype/Core/ExtensionsFactory.cs b/Immutype/Core/ExtensionsFactory.cs index 2bf25ec..73ec040 100644 --- a/Immutype/Core/ExtensionsFactory.cs +++ b/Immutype/Core/ExtensionsFactory.cs @@ -69,11 +69,11 @@ public IEnumerable Create(GenerationContext conte private static CompilationUnitSyntax CreateRootNode(SyntaxNode targetNode, UsingDirectiveSyntax[] additionalUsings, params MemberDeclarationSyntax[] members) { var namespaces = targetNode.Ancestors().OfType(); - NamespaceType? rootNamespace = default; + NamespaceType? rootNamespace = null; foreach (var ns in namespaces) { var nextNs = ns.WithMembers(new SyntaxList([])); - rootNamespace = rootNamespace == default + rootNamespace = rootNamespace == null ? nextNs.AddMembers(members).AddUsings(GetUsings(nextNs.Usings, additionalUsings)) : nextNs.AddMembers(rootNamespace); } @@ -82,7 +82,7 @@ private static CompilationUnitSyntax CreateRootNode(SyntaxNode targetNode, Using var rootCompilationUnit = (baseCompilationUnit ?? SyntaxFactory.CompilationUnit()) .WithMembers(new SyntaxList([])); - return rootNamespace != default + return rootNamespace != null ? rootCompilationUnit.AddMembers(rootNamespace) : rootCompilationUnit.AddUsings(GetUsings(rootCompilationUnit.Usings, additionalUsings)).AddMembers(members); } @@ -96,7 +96,7 @@ private static UsingDirectiveSyntax[] GetUsings(IEnumerable()?.InformationalVersion; + var version = typeof(Information) + .Assembly + .GetCustomAttribute() + ?.InformationalVersion; + if (!string.IsNullOrWhiteSpace(version)) { CurrentDescription = $"{CurrentDescription} {version}"; diff --git a/Immutype/Core/MethodAddRemoveFactory.cs b/Immutype/Core/MethodAddRemoveFactory.cs index e11683e..deaf8c2 100644 --- a/Immutype/Core/MethodAddRemoveFactory.cs +++ b/Immutype/Core/MethodAddRemoveFactory.cs @@ -1,38 +1,26 @@ // ReSharper disable ClassNeverInstantiated.Global namespace Immutype.Core; -internal class MethodAddRemoveFactory : IMethodFactory +internal class MethodAddRemoveFactory( + INameService nameService, + ISyntaxNodeFactory syntaxNodeFactory, + IDataContainerFactory dataContainerFactory, + ICommentsGenerator commentsGenerator) + : IMethodFactory { - private readonly INameService _nameService; - private readonly ISyntaxNodeFactory _syntaxNodeFactory; - private readonly IDataContainerFactory _dataContainerFactory; - private readonly ICommentsGenerator _commentsGenerator; - - public MethodAddRemoveFactory( - INameService nameService, - ISyntaxNodeFactory syntaxNodeFactory, - IDataContainerFactory dataContainerFactory, - ICommentsGenerator commentsGenerator) - { - _nameService = nameService; - _syntaxNodeFactory = syntaxNodeFactory; - _dataContainerFactory = dataContainerFactory; - _commentsGenerator = commentsGenerator; - } - public IEnumerable Create(GenerationContext context, TypeSyntax targetType, IEnumerable parameters, ParameterSyntax currentParameter, ParameterSyntax thisParameter) { - if (currentParameter.Type == default) + if (currentParameter.Type == null) { yield break; } var curParameters = parameters as ParameterSyntax[] ?? parameters.ToArray(); - var name = _nameService.ConvertToName(currentParameter.Identifier.Text); + var name = nameService.ConvertToName(currentParameter.Identifier.Text); var targetDeclaration = context.Syntax; var elementType = GetElementType(currentParameter.Type); - if (elementType == default) + if (elementType == null) { yield break; } @@ -43,33 +31,33 @@ public IEnumerable Create(GenerationContext{_nameService.ConvertToName(currentParameter.Identifier.Text)}.", + $"Add {nameService.ConvertToName(currentParameter.Identifier.Text)}.", currentParameter, - $"{_nameService.ConvertToName(currentParameter.Identifier.Text)} to be added to the copy of the instance.", - _syntaxNodeFactory.CreateExtensionMethod(targetType, $"Add{name}" + targetDeclaration.TypeParameterList) + $"{nameService.ConvertToName(currentParameter.Identifier.Text)} to be added to the copy of the instance.", + syntaxNodeFactory.CreateExtensionMethod(targetType, $"Add{name}" + targetDeclaration.TypeParameterList) .AddParameterListParameters(thisParameter, arrayParameter) .WithConstraintClauses(targetDeclaration.ConstraintClauses) - .AddBodyStatements(_syntaxNodeFactory.CreateGuards(context, thisParameter, !_syntaxNodeFactory.IsValueType(context.Syntax)).ToArray()) - .AddBodyStatements(_syntaxNodeFactory.CreateGuards(context, arrayParameter, false).ToArray()) - .AddBodyStatements(_syntaxNodeFactory.CreateReturnStatement(targetType, addArgs))); + .AddBodyStatements(syntaxNodeFactory.CreateGuards(context, thisParameter, !syntaxNodeFactory.IsValueType(context.Syntax)).ToArray()) + .AddBodyStatements(syntaxNodeFactory.CreateGuards(context, arrayParameter, false).ToArray()) + .AddBodyStatements(syntaxNodeFactory.CreateReturnStatement(targetType, addArgs))); } var removeArgs = CreateArguments(nameof(Enumerable.Except), targetDeclaration, thisParameter, curParameters, currentParameter, arrayParameter); if (removeArgs.Any()) { - yield return _commentsGenerator.AddComments( + yield return commentsGenerator.AddComments( context, - $"Remove {_nameService.ConvertToName(currentParameter.Identifier.Text)}.", + $"Remove {nameService.ConvertToName(currentParameter.Identifier.Text)}.", currentParameter, - $"{_nameService.ConvertToName(currentParameter.Identifier.Text)} to be removed from the instance copy.", - _syntaxNodeFactory.CreateExtensionMethod(targetType, $"Remove{name}" + targetDeclaration.TypeParameterList) + $"{nameService.ConvertToName(currentParameter.Identifier.Text)} to be removed from the instance copy.", + syntaxNodeFactory.CreateExtensionMethod(targetType, $"Remove{name}" + targetDeclaration.TypeParameterList) .AddParameterListParameters(thisParameter, arrayParameter) .WithConstraintClauses(targetDeclaration.ConstraintClauses) - .AddBodyStatements(_syntaxNodeFactory.CreateGuards(context, thisParameter, !_syntaxNodeFactory.IsValueType(context.Syntax)).ToArray()) - .AddBodyStatements(_syntaxNodeFactory.CreateGuards(context, arrayParameter, false).ToArray()) - .AddBodyStatements(_syntaxNodeFactory.CreateReturnStatement(targetType, removeArgs))); + .AddBodyStatements(syntaxNodeFactory.CreateGuards(context, thisParameter, !syntaxNodeFactory.IsValueType(context.Syntax)).ToArray()) + .AddBodyStatements(syntaxNodeFactory.CreateGuards(context, arrayParameter, false).ToArray()) + .AddBodyStatements(syntaxNodeFactory.CreateReturnStatement(targetType, removeArgs))); } if (currentParameter.Type is ArrayTypeSyntax) @@ -80,43 +68,43 @@ public IEnumerable Create(GenerationContext{_nameService.ConvertToName(currentParameter.Identifier.Text)}.", + $"Add {nameService.ConvertToName(currentParameter.Identifier.Text)}.", currentParameter, - $"{_nameService.ConvertToName(currentParameter.Identifier.Text)} to be added to the copy of the instance.", - _syntaxNodeFactory.CreateExtensionMethod(targetType, $"Add{name}" + targetDeclaration.TypeParameterList) + $"{nameService.ConvertToName(currentParameter.Identifier.Text)} to be added to the copy of the instance.", + syntaxNodeFactory.CreateExtensionMethod(targetType, $"Add{name}" + targetDeclaration.TypeParameterList) .AddParameterListParameters(thisParameter, currentParameter) .WithConstraintClauses(targetDeclaration.ConstraintClauses) - .AddBodyStatements(_syntaxNodeFactory.CreateGuards(context, thisParameter, !_syntaxNodeFactory.IsValueType(context.Syntax)).ToArray()) - .AddBodyStatements(_syntaxNodeFactory.CreateGuards(context, currentParameter, false).ToArray()) - .AddBodyStatements(_syntaxNodeFactory.CreateReturnStatement(targetType, addArgs))); + .AddBodyStatements(syntaxNodeFactory.CreateGuards(context, thisParameter, !syntaxNodeFactory.IsValueType(context.Syntax)).ToArray()) + .AddBodyStatements(syntaxNodeFactory.CreateGuards(context, currentParameter, false).ToArray()) + .AddBodyStatements(syntaxNodeFactory.CreateReturnStatement(targetType, addArgs))); } removeArgs = CreateArguments(nameof(Enumerable.Except), targetDeclaration, thisParameter, curParameters, currentParameter, currentParameter); if (removeArgs.Any()) { - yield return _commentsGenerator.AddComments( + yield return commentsGenerator.AddComments( context, - $"Remove {_nameService.ConvertToName(currentParameter.Identifier.Text)}.", + $"Remove {nameService.ConvertToName(currentParameter.Identifier.Text)}.", currentParameter, - $"{_nameService.ConvertToName(currentParameter.Identifier.Text)} to be removed from the instance copy.", - _syntaxNodeFactory.CreateExtensionMethod(targetType, $"Remove{name}" + targetDeclaration.TypeParameterList) + $"{nameService.ConvertToName(currentParameter.Identifier.Text)} to be removed from the instance copy.", + syntaxNodeFactory.CreateExtensionMethod(targetType, $"Remove{name}" + targetDeclaration.TypeParameterList) .AddParameterListParameters(thisParameter, currentParameter) .WithConstraintClauses(targetDeclaration.ConstraintClauses) - .AddBodyStatements(_syntaxNodeFactory.CreateGuards(context, thisParameter, !_syntaxNodeFactory.IsValueType(context.Syntax)).ToArray()) - .AddBodyStatements(_syntaxNodeFactory.CreateGuards(context, currentParameter, false).ToArray()) - .AddBodyStatements(_syntaxNodeFactory.CreateReturnStatement(targetType, removeArgs))); + .AddBodyStatements(syntaxNodeFactory.CreateGuards(context, thisParameter, !syntaxNodeFactory.IsValueType(context.Syntax)).ToArray()) + .AddBodyStatements(syntaxNodeFactory.CreateGuards(context, currentParameter, false).ToArray()) + .AddBodyStatements(syntaxNodeFactory.CreateReturnStatement(targetType, removeArgs))); } } private TypeSyntax? GetElementType(TypeSyntax typeSyntax) => - _syntaxNodeFactory.GetUnqualified(typeSyntax) switch + syntaxNodeFactory.GetUnqualified(typeSyntax) switch { NullableTypeSyntax nullableTypeSyntax => GetElementType(nullableTypeSyntax.ElementType), GenericNameSyntax genericNameSyntax => genericNameSyntax.TypeArgumentList.Arguments[0], ArrayTypeSyntax arrayTypeSyntax => arrayTypeSyntax.ElementType, - _ => default + _ => null }; private IReadOnlyCollection CreateArguments(string enumerableMethod, TypeDeclarationSyntax owner, ParameterSyntax thisParameter, IEnumerable parameters, ParameterSyntax currentParameter, ParameterSyntax arrayParameter) @@ -126,7 +114,7 @@ private IReadOnlyCollection CreateArguments(string enumerableMet { if (parameter == currentParameter) { - var thisArg = _syntaxNodeFactory.CreateTransientArgumentExpression(owner, thisParameter, currentParameter); + var thisArg = syntaxNodeFactory.CreateTransientArgumentExpression(owner, thisParameter, currentParameter); var expression = TryCreateExpression( enumerableMethod, @@ -134,7 +122,7 @@ private IReadOnlyCollection CreateArguments(string enumerableMet currentParameter.Type, arrayParameter); - if (expression == default) + if (expression == null) { return Array.Empty(); } @@ -143,7 +131,7 @@ private IReadOnlyCollection CreateArguments(string enumerableMet } else { - args.Add(_syntaxNodeFactory.CreateTransientArgument(owner, thisParameter, parameter)); + args.Add(syntaxNodeFactory.CreateTransientArgument(owner, thisParameter, parameter)); } } @@ -152,16 +140,16 @@ private IReadOnlyCollection CreateArguments(string enumerableMet private ExpressionSyntax? TryCreateExpression(string enumerableMethod, ExpressionSyntax? thisExpression, TypeSyntax? currentParameterType, ParameterSyntax arrayParameter, bool addCheck = true) { - ExpressionSyntax? result = default; - if (thisExpression != default) + ExpressionSyntax? result = null; + if (thisExpression != null) { ExpressionSyntax valExpression = SyntaxFactory.IdentifierName(arrayParameter.Identifier); if (addCheck) { - var defaultExpression = TryCreateExpression(enumerableMethod, default, currentParameterType, arrayParameter); - if (defaultExpression == default) + var defaultExpression = TryCreateExpression(enumerableMethod, null, currentParameterType, arrayParameter); + if (defaultExpression == null) { - return default; + return null; } thisExpression = SyntaxFactory.ParenthesizedExpression(SyntaxFactory.ConditionalExpression( @@ -172,10 +160,10 @@ private IReadOnlyCollection CreateArguments(string enumerableMet if (arrayParameter.Type is NullableTypeSyntax nullableTypeSyntax) { - var defaultExpression = TryCreateExpression(enumerableMethod, default, nullableTypeSyntax.ElementType, arrayParameter); - if (defaultExpression == default) + var defaultExpression = TryCreateExpression(enumerableMethod, null, nullableTypeSyntax.ElementType, arrayParameter); + if (defaultExpression == null) { - return default; + return null; } valExpression = SyntaxFactory.ParenthesizedExpression(SyntaxFactory.ConditionalExpression( @@ -192,14 +180,14 @@ private IReadOnlyCollection CreateArguments(string enumerableMet .AddArgumentListArguments(SyntaxFactory.Argument(valExpression)); } - switch (_syntaxNodeFactory.GetUnqualified(currentParameterType)) + switch (syntaxNodeFactory.GetUnqualified(currentParameterType)) { case NullableTypeSyntax nullableTypeSyntax: // ReSharper disable once TailRecursiveCall return TryCreateExpression(enumerableMethod, thisExpression, nullableTypeSyntax.ElementType, arrayParameter, false); case GenericNameSyntax genericNameSyntax: - if (_dataContainerFactory.TryCreate(genericNameSyntax, ref result, ref arrayParameter)) + if (dataContainerFactory.TryCreate(genericNameSyntax, ref result, ref arrayParameter)) { return result!; } @@ -207,7 +195,7 @@ private IReadOnlyCollection CreateArguments(string enumerableMet break; case ArrayTypeSyntax arrayTypeSyntax: - if (result != default) + if (result != null) { return SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( diff --git a/Immutype/Core/MethodWithFactory.cs b/Immutype/Core/MethodWithFactory.cs index 9ef438c..9b73ab5 100644 --- a/Immutype/Core/MethodWithFactory.cs +++ b/Immutype/Core/MethodWithFactory.cs @@ -25,7 +25,7 @@ public MethodWithFactory( public IEnumerable Create(GenerationContext context, TypeSyntax targetType, IEnumerable parameters, ParameterSyntax currentParameter, ParameterSyntax thisParameter) { var curParameters = parameters.ToArray(); - var argumentParameter = currentParameter.WithDefault(default); + var argumentParameter = currentParameter.WithDefault(null); var newArgumentParameter = argumentParameter; var targetDeclaration = context.Syntax; var arguments = new List(); @@ -101,7 +101,7 @@ public IEnumerable Create(GenerationContext parameter == currentParameter ? SyntaxFactory.Argument(currentParameter.Default.Value) : _syntaxNodeFactory.CreateTransientArgument(targetDeclaration, thisParameter, parameter)); yield return _commentsGenerator.AddComments( @@ -124,7 +124,7 @@ private static bool AreEqu(BaseParameterSyntax argumentParameter, BaseParameterS return true; } - if (argumentParameter.Type == default || newArgumentParameter.Type == default) + if (argumentParameter.Type == null || newArgumentParameter.Type == null) { return false; } @@ -140,7 +140,7 @@ private ExpressionSyntax CreateWithExpression(ParameterSyntax currentParameter, private ExpressionSyntax CreateWithExpression(TypeSyntax? currentParameterType, ref ParameterSyntax argumentParameter, ExpressionSyntax result) { - if (currentParameterType == default) + if (currentParameterType == null) { return result; } diff --git a/Immutype/Core/MethodsFactory.cs b/Immutype/Core/MethodsFactory.cs index 20eb5c7..b8752ee 100644 --- a/Immutype/Core/MethodsFactory.cs +++ b/Immutype/Core/MethodsFactory.cs @@ -1,17 +1,11 @@ // ReSharper disable ClassNeverInstantiated.Global namespace Immutype.Core; -internal class MethodsFactory : IMethodsFactory +internal class MethodsFactory( + IReadOnlyCollection methodFactories, + ISyntaxNodeFactory syntaxNodeFactory) + : IMethodsFactory { - private readonly ImmutableArray _methodFactories; - private readonly ISyntaxNodeFactory _syntaxNodeFactory; - - public MethodsFactory(ImmutableArray methodFactories, ISyntaxNodeFactory syntaxNodeFactory) - { - _methodFactories = methodFactories; - _syntaxNodeFactory = syntaxNodeFactory; - } - public IEnumerable Create(GenerationContext context, TypeSyntax targetType, IReadOnlyList parameters) { var thisParameter = @@ -21,7 +15,7 @@ public IEnumerable Create(GenerationContext= LanguageVersion.CSharp7_2 } - && _syntaxNodeFactory.IsReadonlyType(context.Syntax)) + && syntaxNodeFactory.IsReadonlyType(context.Syntax)) { thisParameter = thisParameter.AddModifiers(SyntaxKind.InKeyword.WithSpace()); } @@ -29,8 +23,8 @@ public IEnumerable Create(GenerationContext Build(GenerationContext context) => from typeDeclarationSyntax in context.Syntax.DescendantNodes().OfType() where !context.CancellationToken.IsCancellationRequested - where _typeSyntaxFilter.IsAccepted(typeDeclarationSyntax) + where typeSyntaxFilter.IsAccepted(typeDeclarationSyntax) from source in Build(new GenerationContext(context.Options, context.Compilation, context.SemanticModel, typeDeclarationSyntax, context.CancellationToken, ImmutableDictionary.Empty)) select source; public IEnumerable Build(GenerationContext context) { - IReadOnlyList? parameters = default; + IReadOnlyList? parameters = null; if (context.Syntax is RecordDeclarationSyntax recordDeclarationSyntax && recordDeclarationSyntax.ParameterList?.Parameters.Count != 0) { parameters = recordDeclarationSyntax.ParameterList?.Parameters; @@ -40,15 +30,15 @@ public IEnumerable Build(GenerationContext contex from ctor in context.Syntax.Members.OfType() where !context.CancellationToken.IsCancellationRequested where ctor.ParameterList.Parameters.Count > 0 && ctor.Modifiers.Any(i => i.IsKind(SyntaxKind.PublicKeyword) || i.IsKind(SyntaxKind.InternalKeyword)) || !ctor.Modifiers.Any() - let weight = ctor.ParameterList.Parameters.Count + (_syntaxNodeFactory.HasTargetAttribute(ctor) ? 0xffff : 0) + let weight = ctor.ParameterList.Parameters.Count + (syntaxNodeFactory.HasTargetAttribute(ctor) ? 0xffff : 0) orderby weight descending select ctor) .FirstOrDefault() ?.ParameterList .Parameters; - return parameters != default - ? _unitFactory.Create(context, parameters) + return parameters != null + ? unitFactory.Create(context, parameters) : []; } } \ No newline at end of file diff --git a/Immutype/Core/SyntaxRepo.cs b/Immutype/Core/SyntaxRepo.cs index 97511f4..0e6ce28 100644 --- a/Immutype/Core/SyntaxRepo.cs +++ b/Immutype/Core/SyntaxRepo.cs @@ -17,21 +17,21 @@ public static TSyntax WithSpace(this TSyntax node) where TSyntax : Synt public static TSyntax WithNewLine(this TSyntax node) where TSyntax : SyntaxNode => node.WithLeadingTrivia(node.GetLeadingTrivia().Concat([SyntaxFactory.CarriageReturn, SyntaxFactory.LineFeed])); - public static ObjectCreationExpressionSyntax ObjectCreationExpression(TypeSyntax type, ArgumentListSyntax? argumentList = default, InitializerExpressionSyntax? initializer = default) + public static ObjectCreationExpressionSyntax ObjectCreationExpression(TypeSyntax type, ArgumentListSyntax? argumentList = null, InitializerExpressionSyntax? initializer = null) => SyntaxFactory.ObjectCreationExpression(SyntaxKind.NewKeyword.WithSpace(), type, argumentList, initializer); - public static ReturnStatementSyntax ReturnStatement(ExpressionSyntax? expression = default) + public static ReturnStatementSyntax ReturnStatement(ExpressionSyntax? expression = null) => SyntaxFactory.ReturnStatement(default, SyntaxKind.ReturnKeyword.WithSpace(), expression, SyntaxFactory.Token(SyntaxKind.SemicolonToken)); public static MethodDeclarationSyntax MethodDeclaration(TypeSyntax returnType, string identifier) - => SyntaxFactory.MethodDeclaration(default, default, returnType, default, SyntaxFactory.Identifier(identifier).WithSpace(), default, SyntaxFactory.ParameterList(), default, default, default, default); + => SyntaxFactory.MethodDeclaration(default, default, returnType, null, SyntaxFactory.Identifier(identifier).WithSpace(), null, SyntaxFactory.ParameterList(), default, null, null, default); public static ClassDeclarationSyntax ClassDeclaration(string identifier) - => SyntaxFactory.ClassDeclaration(default, default, SyntaxKind.ClassKeyword.WithSpace(), SyntaxFactory.Identifier(identifier), default, default, default, SyntaxKind.OpenBraceToken.WithNewLine(), default, SyntaxKind.CloseBraceToken.WithNewLine(), default); + => SyntaxFactory.ClassDeclaration(default, default, SyntaxKind.ClassKeyword.WithSpace(), SyntaxFactory.Identifier(identifier), null, null, default, SyntaxKind.OpenBraceToken.WithNewLine(), default, SyntaxKind.CloseBraceToken.WithNewLine(), default); public static ParameterSyntax Parameter(SyntaxToken identifier) - => SyntaxFactory.Parameter(default, default, default, identifier.WithSpace(), default); + => SyntaxFactory.Parameter(default, default, null, identifier.WithSpace(), null); - public static ThrowStatementSyntax ThrowStatement(ExpressionSyntax? expression = default) + public static ThrowStatementSyntax ThrowStatement(ExpressionSyntax? expression = null) => SyntaxFactory.ThrowStatement(default, SyntaxKind.ThrowKeyword.WithSpace(), expression, SyntaxFactory.Token(SyntaxKind.SemicolonToken)); } \ No newline at end of file diff --git a/Immutype/Immutype.csproj b/Immutype/Immutype.csproj index 7af2a8c..3e053de 100644 --- a/Immutype/Immutype.csproj +++ b/Immutype/Immutype.csproj @@ -32,7 +32,7 @@ - + all analyzers