Skip to content

Commit aa689dc

Browse files
authored
housekeeping: Cleanup code base (#6)
1 parent 4ca3ae7 commit aa689dc

File tree

8 files changed

+17
-42
lines changed

8 files changed

+17
-42
lines changed

src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterLazySingletonTests.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
// ReactiveUI Association Incorporated licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for full license information.
44

5-
using System;
65
using System.Threading;
76
using System.Threading.Tasks;
87

98
using VerifyXunit;
109

1110
using Xunit;
1211
using Xunit.Abstractions;
13-
using Xunit.Sdk;
1412

1513
namespace Splat.DependencyInjection.SourceGenerator.Tests
1614
{
@@ -34,14 +32,14 @@ public RegisterLazySingletonTests(ITestOutputHelper testOutputHelper)
3432
[InlineData(LazyThreadSafetyMode.None, "Test2")]
3533
public Task ConstructionAndMultiplePropertyInjectionWithLazyMode(LazyThreadSafetyMode mode, string contract)
3634
{
37-
string arguments = string.Empty;
35+
string arguments;
3836
if (string.IsNullOrWhiteSpace(contract))
3937
{
40-
arguments = "LazyThreadSafetyMode." + mode.ToString();
38+
arguments = $"LazyThreadSafetyMode.{mode}";
4139
}
4240
else
4341
{
44-
arguments = '"' + contract + '"' + ", LazyThreadSafetyMode." + mode.ToString();
42+
arguments = $"\"{contract}\", LazyThreadSafetyMode.{mode}";
4543
}
4644

4745
var source = @$"

src/Splat.DependencyInjection.SourceGenerator.Tests/Splat.DependencyInjection.SourceGenerator.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<PackageReference Include="Verify.Xunit" Version="11.20.3" />
2525
<PackageRefernece Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" PrivateAssets="all" />
2626
</ItemGroup>
27-
27+
2828
<ItemGroup>
2929
<ProjectReference Include="..\Splat.DependencyInjection.SourceGenerator\Splat.DependencyInjection.SourceGenerator.csproj" />
3030
</ItemGroup>

src/Splat.DependencyInjection.SourceGenerator.Tests/TestBase.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99

10-
using ICSharpCode.Decompiler.Metadata;
11-
1210
using Microsoft.CodeAnalysis;
1311

1412
using NuGet.LibraryModel;
@@ -51,7 +49,7 @@ public async Task InitializeAsync()
5149
var inputGroup = await NuGetPackageHelper.DownloadPackageFilesAndFolder(_splatLibrary, targetFrameworks, packageOutputDirectory: null).ConfigureAwait(false);
5250

5351
var framework = targetFrameworks[0];
54-
EventCompiler = new EventBuilderCompiler(inputGroup, inputGroup, framework);
52+
EventCompiler = new(inputGroup, inputGroup, framework);
5553
}
5654

5755
public Task DisposeAsync()
@@ -120,7 +118,7 @@ public static class DIRegister
120118
static DIRegister()
121119
{{
122120
SplatRegistrations.{_testMethod}<ITest1, TestConcrete1>({arguments});
123-
SplatRegistrations.{_testMethod}<ITest2, TestConcrete2>();
121+
SplatRegistrations.{_testMethod}<ITest2, TestConcrete2>();
124122
}}
125123
}}
126124
@@ -642,7 +640,7 @@ protected Task TestFail(string source, string contractParameter, [CallerFilePath
642640

643641
GeneratorDriver? driver = null;
644642

645-
Assert.Throws<InvalidOperationException>(() => utility.RunGenerator<Generator>(EventCompiler, out var _, out var _, out driver, source));
643+
Assert.Throws<InvalidOperationException>(() => utility.RunGenerator<Generator>(EventCompiler, out _, out _, out driver, source));
646644

647645
VerifySettings settings = new();
648646
settings.UseParameters(contractParameter);
@@ -686,7 +684,7 @@ private GeneratorDriver Generate(string source)
686684

687685
var utility = new SourceGeneratorUtility(x => TestOutputHelper.WriteLine(x));
688686

689-
utility.RunGenerator<Generator>(EventCompiler, out var _, out var _, out var driver, source);
687+
utility.RunGenerator<Generator>(EventCompiler, out _, out _, out var driver, source);
690688

691689
return driver;
692690
}

src/Splat.DependencyInjection.SourceGenerator/Constants.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ internal static class Constants
88
{
99
public const string ClassName = "SplatRegistrations";
1010
public const string NamespaceName = "Splat";
11-
public const string Locator = "Splat.Locator";
1211
public const string LocatorCurrent = "Splat.Locator.Current";
1312
public const string LocatorGetService = "GetService";
1413

15-
public const string LazyModeType = "global::System.Threading.LazyThreadSafetyMode";
16-
1714
public const string ConstructorAttribute = "global::Splat.DependencyInjectionConstructorAttribute";
1815
public const string PropertyAttribute = "global::Splat.DependencyInjectionPropertyAttribute";
1916

src/Splat.DependencyInjection.SourceGenerator/Metadata/ParameterMetadata.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
// ReactiveUI Association Incorporated licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for full license information.
44

5-
using System;
6-
using System.Collections.Generic;
7-
using System.Text;
8-
95
namespace Splat.DependencyInjection.SourceGenerator.Metadata
106
{
117
internal record ParameterMetadata(string ParameterName, string ParameterValue);

src/Splat.DependencyInjection.SourceGenerator/MetadataExtractor.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8-
using System.Reflection;
98

109
using Microsoft.CodeAnalysis;
1110
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -96,22 +95,22 @@ public static IEnumerable<MethodMetadata> GetValidMethods(GeneratorExecutionCont
9695
private static IEnumerable<ParameterMetadata> GetRegisterParameters(IMethodSymbol methodSymbol, SemanticModel semanticModel, InvocationExpressionSyntax invocationExpression)
9796
{
9897
for (int i = 0; i < invocationExpression.ArgumentList.Arguments.Count; ++i)
99-
{
98+
{
10099
var argument = invocationExpression.ArgumentList.Arguments[i];
101100
var argumentName = methodSymbol.Parameters[i].Name;
102101
var expression = argument.Expression;
103102

104103
if (expression is LiteralExpressionSyntax literal)
105104
{
106-
yield return new ParameterMetadata(argumentName, literal.ToString());
105+
yield return new(argumentName, literal.ToString());
107106
}
108107
else
109108
{
110109
var mode = semanticModel.GetSymbolInfo(expression);
111110

112111
if (mode.Symbol is not null)
113112
{
114-
yield return new ParameterMetadata(argumentName, mode.Symbol.ToDisplayString());
113+
yield return new(argumentName, mode.Symbol.ToDisplayString());
115114
}
116115
}
117116
}
@@ -177,7 +176,7 @@ private static IEnumerable<PropertyDependencyMetadata> GetPropertyDependencies(I
177176
throw new ContextDiagnosticException(Diagnostic.Create(DiagnosticWarnings.PropertyMustPublicBeSettable, property.SetMethod?.Locations.FirstOrDefault(), property.ToDisplayString(RoslynCommonHelpers.TypeFormat)));
178177
}
179178

180-
yield return new PropertyDependencyMetadata(property);
179+
yield return new(property);
181180
}
182181
}
183182
}

src/Splat.DependencyInjection.SourceGenerator/SourceGeneratorHelpers.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static string Generate(GeneratorExecutionContext context, Compilation com
2929

3030
methods = MetadataDependencyChecker.CheckMetadata(context, methods);
3131

32-
var invocations = Generate(compilation, methods);
32+
var invocations = Generate(methods);
3333

3434
var staticConstructor = ConstructorDeclaration(default, new[] { SyntaxKind.StaticKeyword }, Array.Empty<ParameterSyntax>(), Constants.ClassName, Block(invocations.ToList(), 2), 1);
3535

@@ -42,12 +42,10 @@ public static string Generate(GeneratorExecutionContext context, Compilation com
4242
return compilationUnit.ToFullString();
4343
}
4444

45-
private static IEnumerable<StatementSyntax> Generate(Compilation compilation, IEnumerable<MethodMetadata> methodMetadatas)
45+
private static IEnumerable<StatementSyntax> Generate(IEnumerable<MethodMetadata> methodMetadatas)
4646
{
4747
foreach (var methodMetadata in methodMetadatas)
4848
{
49-
var semanticModel = compilation.GetSemanticModel(methodMetadata.MethodInvocation.SyntaxTree);
50-
5149
var typeConstructorArguments = new List<ArgumentSyntax>();
5250

5351
foreach (var parameter in methodMetadata.ConstructorDependencies)
@@ -111,13 +109,7 @@ private static BlockSyntax GetLazyBlock(MethodMetadata methodMetadata, Expressio
111109
const string lazyTypeValueProperty = "Value";
112110
const string lazyVariableName = "lazy";
113111

114-
var lambdaArguments = new ArgumentSyntax[]
115-
{
116-
Argument(ParenthesizedLambdaExpression(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, lazyVariableName, lazyTypeValueProperty))),
117-
Argument($"typeof({methodMetadata.InterfaceTypeName})")
118-
};
119-
120-
var lazyArguments = new List<ArgumentSyntax>()
112+
var lazyArguments = new List<ArgumentSyntax>
121113
{
122114
Argument(ParenthesizedLambdaExpression(call))
123115
};

src/Splat.DependencyInjection.SourceGenerator/SyntaxReceiver.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace Splat.DependencyInjection.SourceGenerator
1111
{
1212
internal class SyntaxReceiver : ISyntaxReceiver
1313
{
14-
public List<InvocationExpressionSyntax> Register { get; } = new List<InvocationExpressionSyntax>();
14+
public List<InvocationExpressionSyntax> Register { get; } = new();
1515

16-
public List<InvocationExpressionSyntax> RegisterLazySingleton { get; } = new List<InvocationExpressionSyntax>();
16+
public List<InvocationExpressionSyntax> RegisterLazySingleton { get; } = new();
1717

1818
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
1919
{
@@ -35,11 +35,6 @@ public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
3535

3636
private void HandleSimpleName(SimpleNameSyntax simpleName, InvocationExpressionSyntax invocationExpression)
3737
{
38-
if (simpleName is null)
39-
{
40-
return;
41-
}
42-
4338
var methodName = simpleName.Identifier.Text;
4439

4540
if (string.Equals(methodName, nameof(Register)))

0 commit comments

Comments
 (0)