Skip to content

Commit 3927aae

Browse files
authored
Factor out CodeFixes and Analyzers to separate assemblies
1 parent 1ab519c commit 3927aae

23 files changed

+100
-85
lines changed

src/Framework/Analyzer/src/DelegateEndpoints/DelegateEndpointFixer.cs

-41
This file was deleted.

src/Framework/Analyzer/src/DelegateEndpoints/DetectMismatchedParameterOptionality.Fixer.cs

-38
This file was deleted.

src/Framework/App.Ref/src/Microsoft.AspNetCore.App.Ref.csproj

+6-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ This package is an internal implementation of the .NET Core SDK and is not meant
6363
<ItemGroup>
6464
<!-- Note: do not add _TransitiveExternalAspNetCoreAppReference to this list. This is intentionally not listed as a direct package reference. -->
6565
<Reference Include="@(AspNetCoreAppReference);@(AspNetCoreAppReferenceAndPackage);@(ExternalAspNetCoreAppReference)" />
66-
<ProjectReference Include="..\..\Analyzer\src\Microsoft.AspNetCore.App.Analyzers.csproj"
66+
<ProjectReference Include="..\..\AspNetCoreAnalyzers\src\Analyzers\Microsoft.AspNetCore.App.Analyzers.csproj"
67+
ReferenceOutputAssembly="false"
68+
SkipGetTargetFrameworkProperties="true"
69+
UndefineProperties="TargetFramework;TargetFrameworks;RuntimeIdentifier;PublishDir" />
70+
<ProjectReference Include="..\..\AspNetCoreAnalyzers\src\CodeFixes\Microsoft.AspNetCore.App.CodeFixes.csproj"
6771
ReferenceOutputAssembly="false"
6872
SkipGetTargetFrameworkProperties="true"
6973
UndefineProperties="TargetFramework;TargetFrameworks;RuntimeIdentifier;PublishDir" />
@@ -162,6 +166,7 @@ This package is an internal implementation of the .NET Core SDK and is not meant
162166

163167
<RefPackContent Include="$(PkgMicrosoft_Internal_Runtime_AspNetCore_Transport)\$(AnalyzersPackagePath)**\*.*" PackagePath="$(AnalyzersPackagePath)" />
164168
<RefPackContent Include="$(ArtifactsDir)bin\Microsoft.AspNetCore.App.Analyzers\$(Configuration)\netstandard2.0\Microsoft.AspNetCore.App.Analyzers.dll" PackagePath="$(AnalyzersPackagePath)dotnet/cs/" />
169+
<RefPackContent Include="$(ArtifactsDir)bin\Microsoft.AspNetCore.App.CodeFixes\$(Configuration)\netstandard2.0\Microsoft.AspNetCore.App.CodeFixes.dll" PackagePath="$(AnalyzersPackagePath)dotnet/cs/" />
165170

166171
<RefPackContent Include="@(AspNetCoreReferenceAssemblyPath)" PackagePath="$(RefAssemblyPackagePath)" />
167172
<RefPackContent Include="@(AspNetCoreReferenceDocXml)" PackagePath="$(RefAssemblyPackagePath)" />

src/Framework/Analyzer/src/DelegateEndpoints/DetectMismatchedParameterOptionality.cs renamed to src/Framework/AspNetCoreAnalyzers/src/Analyzers/DetectMismatchedParameterOptionality.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private static void DetectMismatchedParameterOptionality(
5050
continue;
5151
}
5252
var argumentIsOptional = parameter.IsOptional || parameter.NullableAnnotation != NullableAnnotation.NotAnnotated;
53-
var location = parameter.DeclaringSyntaxReferences.SingleOrDefault()?.GetSyntax().GetLocation();
53+
var location = parameter.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax().GetLocation();
5454
var routeParamIsOptional = enumerator.CurrentQualifiers.IndexOf('?') > -1;
5555

5656
if (!argumentIsOptional && routeParamIsOptional)

src/Framework/Analyzer/src/Microsoft.AspNetCore.App.Analyzers.csproj renamed to src/Framework/AspNetCoreAnalyzers/src/Analyzers/Microsoft.AspNetCore.App.Analyzers.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<Reference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="All" />
1414

1515
<InternalsVisibleTo Include="Microsoft.AspNetCore.App.Analyzers.Test" />
16+
<InternalsVisibleTo Include="Microsoft.AspNetCore.App.CodeFixes" />
1617
</ItemGroup>
1718

1819
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Linq;
5+
using System.Threading;
6+
using System.Collections.Immutable;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Analyzers.DelegateEndpoints;
9+
using Microsoft.CodeAnalysis;
10+
using Microsoft.CodeAnalysis.CSharp;
11+
using Microsoft.CodeAnalysis.CSharp.Syntax;
12+
using Microsoft.CodeAnalysis.CodeFixes;
13+
using Microsoft.CodeAnalysis.CodeActions;
14+
using Microsoft.CodeAnalysis.Editing;
15+
16+
namespace Microsoft.AspNetCore.Analyzers.DelegateEndpoints.Fixers;
17+
18+
public class DetectMismatchedParameterOptionalityFixer : CodeFixProvider
19+
{
20+
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(DiagnosticDescriptors.DetectMismatchedParameterOptionality.Id);
21+
22+
public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
23+
24+
public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
25+
{
26+
foreach (var diagnostic in context.Diagnostics)
27+
{
28+
context.RegisterCodeFix(
29+
CodeAction.Create("Fix mismatched route parameter and argument optionality",
30+
cancellationToken => FixMismatchedParameterOptionality(context, cancellationToken),
31+
equivalenceKey: DiagnosticDescriptors.DetectMismatchedParameterOptionality.Id),
32+
diagnostic);
33+
}
34+
35+
return Task.CompletedTask;
36+
}
37+
38+
private static async Task<Document> FixMismatchedParameterOptionality(CodeFixContext context, CancellationToken cancellationToken)
39+
{
40+
DocumentEditor editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken);
41+
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
42+
43+
if (root == null)
44+
{
45+
return context.Document;
46+
}
47+
48+
var diagnostic = context.Diagnostics.SingleOrDefault();
49+
50+
if (diagnostic == null)
51+
{
52+
return context.Document;
53+
}
54+
55+
var param = root.FindNode(diagnostic.Location.SourceSpan);
56+
if (param != null && param is ParameterSyntax parameterSyntax)
57+
{
58+
if (parameterSyntax.Type != null)
59+
{
60+
var newParam = parameterSyntax.WithType(SyntaxFactory.NullableType(parameterSyntax.Type));
61+
editor.ReplaceNode(parameterSyntax, newParam);
62+
}
63+
}
64+
65+
return editor.GetChangedDocument();
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Description>CSharp CodeFixes for ASP.NET Core.</Description>
4+
<IsShippingPackage>false</IsShippingPackage>
5+
<AddPublicApiAnalyzers>false</AddPublicApiAnalyzers>
6+
<TargetFramework>netstandard2.0</TargetFramework>
7+
<IncludeBuildOutput>false</IncludeBuildOutput>
8+
<Nullable>Enable</Nullable>
9+
<RootNamespace>Microsoft.AspNetCore.Analyzers</RootNamespace>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<Reference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="All" />
14+
<ProjectReference Include="..\Analyzers\Microsoft.AspNetCore.App.Analyzers.csproj" />
15+
</ItemGroup>
16+
17+
</Project>

src/Framework/Analyzer/test/Microsoft.AspNetCore.App.Analyzers.Test.csproj renamed to src/Framework/AspNetCoreAnalyzers/test/Microsoft.AspNetCore.App.Analyzers.Test.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<ProjectReference Include="..\src\Microsoft.AspNetCore.App.Analyzers.csproj" />
14+
<ProjectReference Include="..\src\Analyzers\Microsoft.AspNetCore.App.Analyzers.csproj" />
15+
<ProjectReference Include="..\src\CodeFixes\Microsoft.AspNetCore.App.CodeFixes.csproj" />
1516
<ProjectReference Include="$(RepoRoot)src\Analyzers\Microsoft.AspNetCore.Analyzer.Testing\src\Microsoft.AspNetCore.Analyzer.Testing.csproj" />
1617
<Reference Include="Microsoft.AspNetCore" />
1718
<Reference Include="Microsoft.AspNetCore.Mvc" />

src/Framework/Analyzer/test/MinimalActions/DetectMismatchedParameterOptionalityTest.cs renamed to src/Framework/AspNetCoreAnalyzers/test/MinimalActions/DetectMismatchedParameterOptionalityTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Xunit;
66
using VerifyCS = Microsoft.AspNetCore.Analyzers.DelegateEndpoints.CSharpDelegateEndpointsCodeFixVerifier<
77
Microsoft.AspNetCore.Analyzers.DelegateEndpoints.DelegateEndpointAnalyzer,
8-
Microsoft.AspNetCore.Analyzers.DelegateEndpoints.DelegateEndpointFixer>;
8+
Microsoft.AspNetCore.Analyzers.DelegateEndpoints.Fixers.DetectMismatchedParameterOptionalityFixer>;
99

1010
namespace Microsoft.AspNetCore.Analyzers.DelegateEndpoints;
1111

src/Framework/Analyzer/test/Verifiers/CSharpDelegateEndpointsCodeFixVerifier.cs renamed to src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpDelegateEndpointsCodeFixVerifier.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
using System.Collections.Immutable;
55
using System.Globalization;
6+
using Microsoft.AspNetCore.Analyzers.DelegateEndpoints.Fixers;
67
using Microsoft.AspNetCore.Analyzer.Testing;
78
using Microsoft.CodeAnalysis;
89
using Microsoft.CodeAnalysis.CSharp.Testing;
10+
using Microsoft.CodeAnalysis.CodeFixes;
911
using Microsoft.CodeAnalysis.Diagnostics;
1012
using Microsoft.CodeAnalysis.Testing;
1113
using Microsoft.CodeAnalysis.Testing.Verifiers;
@@ -15,7 +17,7 @@ namespace Microsoft.AspNetCore.Analyzers.DelegateEndpoints;
1517

1618
public static class CSharpDelegateEndpointsCodeFixVerifier<TAnalyzer, TCodeFix>
1719
where TAnalyzer : DelegateEndpointAnalyzer, new()
18-
where TCodeFix : DelegateEndpointFixer, new()
20+
where TCodeFix : DetectMismatchedParameterOptionalityFixer, new()
1921
{
2022
public static DiagnosticResult Diagnostic(string diagnosticId = null)
2123
=> CSharpCodeFixVerifier<TAnalyzer, TCodeFix, XUnitVerifier>.Diagnostic(diagnosticId);

src/Framework/Framework.slnf

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"src\\Extensions\\Features\\src\\Microsoft.Extensions.Features.csproj",
2020
"src\\FileProviders\\Embedded\\src\\Microsoft.Extensions.FileProviders.Embedded.csproj",
2121
"src\\FileProviders\\Manifest.MSBuildTask\\src\\Microsoft.Extensions.FileProviders.Embedded.Manifest.Task.csproj",
22-
"src\\Framework\\Analyzer\\src\\Microsoft.AspNetCore.App.Analyzers.csproj",
22+
"src\\Framework\\AspNetCoreAnalyzers\\src\\Analyzers\\Microsoft.AspNetCore.App.Analyzers.csproj",
23+
"src\\Framework\\AspNetCoreAnalyzers\\src\\CodeFixes\\Microsoft.AspNetCore.App.CodeFixes.csproj",
2324
"src\\Framework\\Analyzer\\test\\Microsoft.AspNetCore.App.Analyzers.Test.csproj",
2425
"src\\Framework\\App.Ref\\src\\Microsoft.AspNetCore.App.Ref.csproj",
2526
"src\\Framework\\App.Runtime\\src\\Microsoft.AspNetCore.App.Runtime.csproj",

0 commit comments

Comments
 (0)