Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DependencyInjection.slnx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<Solution>
<Project Path="src/Attributed/Attributed.msbuildproj" Type="13b669be-bb05-4ddf-9536-439f39a36129" />
<Project Path="src/CodeAnalysis.Tests/CodeAnalysis.Tests.csproj" />
<Project Path="src/DependencyInjection.CodeFixes/DependencyInjection.CodeFixes.csproj" Id="2dbf1d6d-2147-407b-9e3a-8caf0eda549c" />
<Project Path="src/DependencyInjection.Tests/DependencyInjection.Tests.csproj" />
<Project Path="src/DependencyInjection/DependencyInjection.csproj" />
<Project Path="src/DependencyInjection.CodeAnalysis/DependencyInjection.CodeAnalysis.csproj" />
<Project Path="src/NoAddServices/NoAddServices.csproj" />
<Project Path="src/Package/DependencyInjection.Package.msbuildproj" Type="13b669be-bb05-4ddf-9536-439f39a36129" />
</Solution>
6 changes: 4 additions & 2 deletions src/CodeAnalysis.Tests/CodeAnalysis.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.14.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" Version="1.1.2" />
<PackageReference Include="ThisAssembly.Resources" Version="2.1.2" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DependencyInjection\DependencyInjection.csproj" AdditionalProperties="ILRepack=true" />
<ProjectReference Include="..\DependencyInjection.CodeFixes\DependencyInjection.CodeFixes.csproj" />
<ProjectReference Include="..\DependencyInjection.CodeAnalysis\DependencyInjection.CodeAnalysis.csproj" AdditionalProperties="ILRepack=true" />
</ItemGroup>

<ItemGroup>
<EmbeddedCode Include="..\DependencyInjection\compile\*.cs" />
<EmbeddedCode Include="..\DependencyInjection.CodeAnalysis\compile\*.cs" />
</ItemGroup>

</Project>
74 changes: 74 additions & 0 deletions src/CodeAnalysis.Tests/LegacyServiceAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Devlooped.Extensions.DependencyInjection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Xunit;
using Xunit.Abstractions;
using AnalyzerTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerTest<Devlooped.Extensions.DependencyInjection.LegacyServiceAttributeAnalyzer, Microsoft.CodeAnalysis.Testing.DefaultVerifier>;
using Verifier = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerVerifier<Devlooped.Extensions.DependencyInjection.LegacyServiceAttributeAnalyzer, Microsoft.CodeAnalysis.Testing.DefaultVerifier>;

namespace Tests.CodeAnalysis;

public class LegacyServiceAttributeTests(ITestOutputHelper Output)
{
[Fact]
public async Task ErrorIfTKeyMatchesStringConstant()
{
var test = new AnalyzerTest
{
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck,
TestCode =
"""
using System;
using Microsoft.Extensions.DependencyInjection;

[{|#0:Service<string>("my")|}]
public class MyService { }
"""
}.WithTestState();

var expected = Verifier.Diagnostic(LegacyServiceAttributeAnalyzer.ServiceTypeNotKeyType).WithLocation(0);
test.ExpectedDiagnostics.Add(expected);

await test.RunAsync();
}

[Fact]
public async Task ExecuteSyncWithAsyncCommand()
{
var test = new CSharpCodeFixTest<LegacyServiceAttributeAnalyzer, LegacyServiceAttributeFixer, DefaultVerifier>
{
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck,
TestCode =
"""
using System;
using Microsoft.Extensions.DependencyInjection;

[{|#0:Service<string>("my")|}]
public class MyService { }
""",
FixedCode =
"""
using System;
using Microsoft.Extensions.DependencyInjection;

[Service("my")]
public class MyService { }
""",
}.WithTestState();

test.ExpectedDiagnostics.Add(new DiagnosticResult(LegacyServiceAttributeAnalyzer.ServiceTypeNotKeyType).WithLocation(0));
test.FixedState.Sources.AddStaticFiles();

await test.RunAsync();
}
}
25 changes: 25 additions & 0 deletions src/CodeAnalysis.Tests/TestExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Resources;

namespace Tests.CodeAnalysis;

Expand All @@ -31,4 +34,26 @@ public static TAnalyzerTest WithPreprocessorSymbols<TAnalyzerTest>(this TAnalyze

return test;
}

public static TAnalyzerTest WithTestState<TAnalyzerTest>(this TAnalyzerTest test)
where TAnalyzerTest : AnalyzerTest<DefaultVerifier>
{
test.TestState.Sources.AddStaticFiles();
test.TestState.ReferenceAssemblies = new ReferenceAssemblies(
"net8.0",
new PackageIdentity(
"Microsoft.NETCore.App.Ref", "8.0.0"),
Path.Combine("ref", "net8.0"))
.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.Extensions.DependencyInjection", "8.0.0")));

return test;
}

public static void AddStaticFiles(this SourceFileList sources)
{
sources.Add(ThisAssembly.Resources.AddServicesNoReflectionExtension.Text);
sources.Add(ThisAssembly.Resources.ServiceAttribute.Text);
sources.Add(ThisAssembly.Resources.ServiceAttribute_1.Text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,12 @@
<PropertyGroup>
<LangVersion>Preview</LangVersion>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Devlooped.Extensions.DependencyInjection</AssemblyName>
<PackageId>Devlooped.Extensions.DependencyInjection</PackageId>
<Title>
Automatic compile-time service registrations for Microsoft.Extensions.DependencyInjection with no run-time dependencies, from conventions or attributes.
</Title>
<Description>$(Title)</Description>
<AssemblyName>Devlooped.Extensions.DependencyInjection.CodeAnalysis</AssemblyName>
<PackFolder>analyzers/dotnet</PackFolder>
<IsRoslynComponent>true</IsRoslynComponent>
<DevelopmentDependency>true</DevelopmentDependency>
<DefineConstants>$(DefineConstants);DDI_ADDSERVICE;DDI_ADDSERVICES</DefineConstants>
<ImplicitUsings>false</ImplicitUsings>
<PackageLicenseExpression></PackageLicenseExpression>
<PackageLicenseFile>OSMFEULA.txt</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<ILRepack>true</ILRepack>
</PropertyGroup>

Expand All @@ -30,7 +22,6 @@
</ItemGroup>

<ItemGroup>
<None Include="..\..\osmfeula.txt" Link="osmfeula.txt" PackagePath="OSMFEULA.txt" />
<None Update="Devlooped.Extensions.DependencyInjection.props" CopyToOutputDirectory="PreserveNewest" PackFolder="build\netstandard2.0" />
<None Update="Devlooped.Extensions.DependencyInjection.targets" CopyToOutputDirectory="PreserveNewest" PackFolder="build\netstandard2.0" />
<None Include="compile\*.cs" CopyToOutputDirectory="PreserveNewest" PackFolder="build\netstandard2.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Devlooped.Extensions.DependencyInjection.CodeFixes</AssemblyName>
<PackFolder>analyzers/dotnet</PackFolder>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NuGetizer" Version="1.4.6" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" Pack="false" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.2.0" Pack="false" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DependencyInjection.CodeAnalysis\DependencyInjection.CodeAnalysis.csproj" />
</ItemGroup>

</Project>
55 changes: 55 additions & 0 deletions src/DependencyInjection.CodeFixes/LegacyServiceAttributeFixer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Immutable;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Devlooped.Extensions.DependencyInjection;

[Shared]
[ExportCodeFixProvider(LanguageNames.CSharp)]
public class LegacyServiceAttributeFixer : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(
LegacyServiceAttributeAnalyzer.ServiceTypeNotKeyType.Id);

public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
if (root == null)
return;

var attribute = root.FindNode(context.Span).FirstAncestorOrSelf<AttributeSyntax>();
if (attribute == null)
return;

context.RegisterCodeFix(new RemoveGenericParameterCodeAction(context.Document, attribute), context.Diagnostics);
}

class RemoveGenericParameterCodeAction(Document document, AttributeSyntax syntax) : CodeAction
{
public override string Title => "Remove generic parameter";
public override string? EquivalenceKey => Title;

protected override async Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
if (root == null)
return document;

var isGeneric = syntax.Name is GenericNameSyntax;
if (!isGeneric)
return document;

var newName = IdentifierName("Service");

return document.WithSyntaxRoot(root.ReplaceNode(syntax, syntax.WithName(newName)));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\DependencyInjection\Devlooped.Extensions.DependencyInjection.props" />
<Import Project="..\DependencyInjection.CodeAnalysis\Devlooped.Extensions.DependencyInjection.props" />

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
Expand All @@ -24,15 +24,16 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DependencyInjection\DependencyInjection.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
<ProjectReference Include="..\DependencyInjection.CodeAnalysis\DependencyInjection.CodeAnalysis.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
<ProjectReference Include="..\DependencyInjection.CodeFixes\DependencyInjection.CodeFixes.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
<Using Include="Xunit.Abstractions" />
</ItemGroup>

<Import Project="..\DependencyInjection\Devlooped.Extensions.DependencyInjection.targets" />
<Import Project="..\DependencyInjection.CodeAnalysis\Devlooped.Extensions.DependencyInjection.targets" />

<ItemGroup>
<Analyzer Include="$(PkgMicrosoft_Bcl_HashCode)\lib\netstandard2.0\Microsoft.Bcl.HashCode.dll" />
Expand Down
6 changes: 3 additions & 3 deletions src/NoAddServices/NoAddServices.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\DependencyInjection\Devlooped.Extensions.DependencyInjection.props" />
<Import Project="..\DependencyInjection.CodeAnalysis\Devlooped.Extensions.DependencyInjection.props" />

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
Expand All @@ -14,9 +14,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DependencyInjection\DependencyInjection.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
<ProjectReference Include="..\DependencyInjection.CodeAnalysis\DependencyInjection.CodeAnalysis.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
</ItemGroup>

<Import Project="..\DependencyInjection\Devlooped.Extensions.DependencyInjection.targets" />
<Import Project="..\DependencyInjection.CodeAnalysis\Devlooped.Extensions.DependencyInjection.targets" />

</Project>
24 changes: 24 additions & 0 deletions src/Package/DependencyInjection.Package.msbuildproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.Build.NoTargets/3.5.0" TreatAsLocalProperty="Version">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Devlooped.Extensions.DependencyInjection</PackageId>
<Title>
Automatic compile-time service registrations for Microsoft.Extensions.DependencyInjection with no run-time dependencies, from conventions or attributes.
</Title>
<Description>$(Title)</Description>
<PackageLicenseExpression>
</PackageLicenseExpression>
<PackageLicenseFile>OSMFEULA.txt</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGetizer" Version="1.2.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DependencyInjection.CodeFixes\DependencyInjection.CodeFixes.csproj" />
<ProjectReference Include="..\DependencyInjection.CodeAnalysis\DependencyInjection.CodeAnalysis.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\osmfeula.txt" Link="osmfeula.txt" PackagePath="OSMFEULA.txt" />
</ItemGroup>
</Project>
File renamed without changes.