Skip to content

Commit 73ff4a3

Browse files
committed
Move analyzer to separate project.
1 parent 35f0a0c commit 73ff4a3

11 files changed

+133
-48
lines changed

Carter.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Carter.ResponseNegotiators.
3232
EndProject
3333
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Carter.ResponseNegotiators.Newtonsoft.Tests", "test\Carter.ResponseNegotiators.Newtonsoft.Tests\Carter.ResponseNegotiators.Newtonsoft.Tests.csproj", "{47DD3AA8-F072-41E3-AF7F-119DBBD8D14A}"
3434
EndProject
35+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Carter.Analyzers", "src\Carter.Analyzers\Carter.Analyzers.csproj", "{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}"
36+
EndProject
3537
Global
3638
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3739
Debug|Any CPU = Debug|Any CPU
@@ -150,6 +152,18 @@ Global
150152
{47DD3AA8-F072-41E3-AF7F-119DBBD8D14A}.Release|x64.Build.0 = Release|Any CPU
151153
{47DD3AA8-F072-41E3-AF7F-119DBBD8D14A}.Release|x86.ActiveCfg = Release|Any CPU
152154
{47DD3AA8-F072-41E3-AF7F-119DBBD8D14A}.Release|x86.Build.0 = Release|Any CPU
155+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
156+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
157+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Debug|x64.ActiveCfg = Debug|Any CPU
158+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Debug|x64.Build.0 = Debug|Any CPU
159+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Debug|x86.ActiveCfg = Debug|Any CPU
160+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Debug|x86.Build.0 = Debug|Any CPU
161+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
162+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Release|Any CPU.Build.0 = Release|Any CPU
163+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Release|x64.ActiveCfg = Release|Any CPU
164+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Release|x64.Build.0 = Release|Any CPU
165+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Release|x86.ActiveCfg = Release|Any CPU
166+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C}.Release|x86.Build.0 = Release|Any CPU
153167
EndGlobalSection
154168
GlobalSection(SolutionProperties) = preSolution
155169
HideSolutionNode = FALSE
@@ -164,6 +178,7 @@ Global
164178
{E5D99119-96EB-4C01-9EC2-A06FC3EEC182} = {35DE35A0-758D-4FDD-BDA3-67F04F65677D}
165179
{D30E87B0-39AE-41FE-8BA3-E5F9FBFFDD64} = {5D056A8A-821C-4C3C-A281-4FA7F8CE251B}
166180
{47DD3AA8-F072-41E3-AF7F-119DBBD8D14A} = {DCB5B9A0-F06D-4BDF-917D-A459C790C718}
181+
{C15C71C0-73CE-41DA-8EEE-C66318BF3A6C} = {5D056A8A-821C-4C3C-A281-4FA7F8CE251B}
167182
EndGlobalSection
168183
GlobalSection(ExtensibilityGlobals) = postSolution
169184
SolutionGuid = {9096DE78-6327-48BA-AE0E-336F769681A7}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<LangVersion>12</LangVersion>
8+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
9+
<NoWarn>RS2008</NoWarn>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<InternalsVisibleTo Include="Carter.Tests" />
18+
</ItemGroup>
19+
</Project>

src/Carter/Analyzers/CarterModuleShouldNotHaveDependenciesAnalyzer.cs renamed to src/Carter.Analyzers/CarterModuleShouldNotHaveDependenciesAnalyzer.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,28 @@ private static void OnTypeAnalysis(SymbolAnalysisContext context, INamedTypeSymb
4444
foreach (var syntaxReference in constructor.DeclaringSyntaxReferences)
4545
{
4646
var node = syntaxReference.GetSyntax();
47-
SyntaxToken identifier;
48-
if (node is ConstructorDeclarationSyntax constructorDeclaration)
47+
SyntaxToken? identifier = node switch
4948
{
50-
identifier = constructorDeclaration.Identifier;
51-
} else if (node is RecordDeclarationSyntax recordDeclaration)
52-
{
53-
identifier = recordDeclaration.Identifier;
54-
}
55-
else
49+
ConstructorDeclarationSyntax constructorDeclaration => constructorDeclaration.Identifier,
50+
RecordDeclarationSyntax recordDeclaration => recordDeclaration.Identifier,
51+
ClassDeclarationSyntax classDeclaration => classDeclaration.Identifier,
52+
StructDeclarationSyntax structDeclaration => structDeclaration.Identifier,
53+
_ => null
54+
};
55+
if (!identifier.HasValue)
5656
{
5757
continue;
5858
}
5959

6060
var diagnostic = Diagnostic.Create(
6161
DiagnosticDescriptors.CarterModuleShouldNotHaveDependencies,
62-
identifier.GetLocation(),
63-
identifier.Text
62+
identifier.Value.GetLocation(),
63+
identifier.Value.Text
6464
);
6565
context.ReportDiagnostic(diagnostic);
6666
}
6767
}
6868
}
6969

70-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [DiagnosticDescriptors.CarterModuleShouldNotHaveDependencies];
70+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(DiagnosticDescriptors.CarterModuleShouldNotHaveDependencies);
7171
}

src/Carter/Carter.csproj

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,24 @@
1313
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
1414
<MinVerSkip Condition="'$(Configuration)' == 'Debug'">true</MinVerSkip>
1515
<PackageReadmeFile>README.md</PackageReadmeFile>
16-
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
17-
<NoWarn>RS2008</NoWarn>
1816
</PropertyGroup>
1917
<ItemGroup>
2018
<None Include="..\..\media\carterlogo.png" Pack="true" PackagePath="\" />
2119
<None Include="..\..\README.md" Pack="true" PackagePath="\"/>
22-
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
20+
<None Include="$(OutputPath)\Carter.Analyzers.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
2321
</ItemGroup>
2422
<ItemGroup>
2523
<FrameworkReference Include="Microsoft.AspNetCore.App" />
2624
</ItemGroup>
25+
<ItemGroup>
26+
<ProjectReference Include="..\Carter.Analyzers\Carter.Analyzers.csproj" OutputItemType="Analyzer" />
27+
</ItemGroup>
2728
<ItemGroup>
2829
<PackageReference Include="FluentValidation" Version="11.8.0" />
29-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" PrivateAssets="compile" />
3030
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="8.0.0" />
3131
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
3232
<PackageReference Include="MinVer" Version="2.5.0">
3333
<PrivateAssets>all</PrivateAssets>
3434
</PackageReference>
3535
</ItemGroup>
36-
<ItemGroup>
37-
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
38-
</ItemGroup>
3936
</Project>

src/Carter/DependencyContextAssemblyCatalog.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ private static Assembly SafeLoadAssembly(AssemblyName assemblyName)
8989
{
9090
try
9191
{
92-
#pragma warning disable RS1035
9392
return Assembly.Load(assemblyName);
94-
#pragma warning restore RS1035
9593
}
9694
catch (Exception)
9795
{

src/Carter/ICarterModule.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ protected CarterModule() : this(string.Empty)
5454
/// Initializes a new instance of <see cref="CarterModule"/>
5555
/// </summary>
5656
/// <param name="basePath">A base path to group routes in your <see cref="CarterModule"/></param>
57+
#pragma warning disable CARTER1
5758
protected CarterModule(string basePath)
59+
#pragma warning restore CARTER1
5860
{
5961
this.basePath = basePath;
6062
}

src/Carter/ModelBinding/BindExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,12 @@ public static async Task BindAndSaveFile(this HttpRequest request, string saveLo
8888

8989
private static async Task SaveFileInternal(IFormFile file, string saveLocation, string fileName = "")
9090
{
91-
#pragma warning disable RS1035
9291
if (!Directory.Exists(saveLocation))
9392
Directory.CreateDirectory(saveLocation);
9493

9594
fileName = !string.IsNullOrWhiteSpace(fileName) ? fileName : file.FileName;
9695

9796
using (var fileToSave = File.Create(Path.Combine(saveLocation, fileName)))
9897
await file.CopyToAsync(fileToSave);
99-
#pragma warning restore RS1035
10098
}
10199
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace Carter.Tests.Analyzers;
2+
3+
using System.IO;
4+
using Microsoft.CodeAnalysis;
5+
using Microsoft.CodeAnalysis.CSharp;
6+
using Microsoft.CodeAnalysis.CSharp.Testing;
7+
using Microsoft.CodeAnalysis.Diagnostics;
8+
using Microsoft.CodeAnalysis.Testing;
9+
using Microsoft.CodeAnalysis.Testing.Verifiers;
10+
11+
public sealed class CSharpPreviewAnalyzerTest<TAnalyzer> : CSharpAnalyzerTest<TAnalyzer, XUnitVerifier>
12+
where TAnalyzer : DiagnosticAnalyzer, new()
13+
{
14+
public CSharpPreviewAnalyzerTest(string code)
15+
{
16+
var carterPackage = new PackageIdentity("Carter", "8.1.0");
17+
var aspNetPackage = new PackageIdentity("Microsoft.AspNetCore.App.Ref", "8.0.0");
18+
var bclPackage = new PackageIdentity("Microsoft.NETCore.App.Ref", "8.0.0");
19+
ReferenceAssemblies = new ReferenceAssemblies("net8.0", bclPackage, Path.Combine("ref", "net8.0")).AddPackages([carterPackage, aspNetPackage]);
20+
TestCode = code;
21+
22+
}
23+
protected override ParseOptions CreateParseOptions()
24+
=> new CSharpParseOptions(LanguageVersion.Preview, DocumentationMode.Diagnose);
25+
}

test/Carter.Tests/Analyzers/CarterModuleShouldNotHaveDependenciesTests.cs

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
namespace Carter.Tests.Analyzers;
22

3-
using System.IO;
43
using System.Threading.Tasks;
54
using Carter.Analyzers;
6-
using Microsoft.CodeAnalysis.CSharp.Testing;
75
using Microsoft.CodeAnalysis.Testing;
86
using Microsoft.CodeAnalysis.Testing.Verifiers;
97
using Xunit;
@@ -33,7 +31,7 @@ public void AddRoutes(IEndpointRouteBuilder app) {}
3331
.WithLocation(0)
3432
.WithArguments("MyCarterModule");
3533

36-
return this.VerifyAsync(code, diagnosticResult);
34+
return VerifyAsync(code, diagnosticResult);
3735
}
3836

3937
[Theory]
@@ -55,7 +53,7 @@ public void AddRoutes(IEndpointRouteBuilder app) {}
5553
.WithLocation(0)
5654
.WithArguments("MyCarterModule");
5755

58-
return this.VerifyAsync(code, diagnosticResult);
56+
return VerifyAsync(code, diagnosticResult);
5957
}
6058

6159
[Theory]
@@ -81,7 +79,7 @@ public void AddRoutes(IEndpointRouteBuilder app) {}
8179
.WithLocation(0)
8280
.WithArguments("MyCarterModule");
8381

84-
return this.VerifyAsync(code, diagnosticResult);
82+
return VerifyAsync(code, diagnosticResult);
8583
}
8684

8785
[Theory]
@@ -103,7 +101,7 @@ public void AddRoutes(IEndpointRouteBuilder app) {}
103101
.WithLocation(0)
104102
.WithArguments("MyCarterModule");
105103

106-
return this.VerifyAsync(code, diagnosticResult);
104+
return VerifyAsync(code, diagnosticResult);
107105
}
108106

109107
[Theory]
@@ -129,7 +127,7 @@ public void AddRoutes(IEndpointRouteBuilder app) {}
129127
.WithLocation(0)
130128
.WithArguments("MyCarterModule");
131129

132-
return this.VerifyAsync(code, diagnosticResult);
130+
return VerifyAsync(code, diagnosticResult);
133131
}
134132

135133
[Theory]
@@ -151,7 +149,7 @@ public void AddRoutes(IEndpointRouteBuilder app) {}
151149
.WithLocation(0)
152150
.WithArguments("MyCarterModule");
153151

154-
return this.VerifyAsync(code, diagnosticResult);
152+
return VerifyAsync(code, diagnosticResult);
155153
}
156154

157155
[Theory]
@@ -173,7 +171,7 @@ public void AddRoutes(IEndpointRouteBuilder app) {}
173171
}
174172
""";
175173

176-
return this.VerifyAsync(code);
174+
return VerifyAsync(code);
177175
}
178176

179177
[Theory]
@@ -195,7 +193,7 @@ public void AddRoutes(IEndpointRouteBuilder app) {}
195193
}
196194
""";
197195

198-
return this.VerifyAsync(code);
196+
return VerifyAsync(code);
199197
}
200198

201199
[Theory]
@@ -217,7 +215,7 @@ public void AddRoutes(IEndpointRouteBuilder app) {}
217215
}
218216
""";
219217

220-
return this.VerifyAsync(code);
218+
return VerifyAsync(code);
221219
}
222220

223221
[Theory]
@@ -243,7 +241,7 @@ public void AddRoutes(IEndpointRouteBuilder app) {}
243241
}
244242
""";
245243

246-
return this.VerifyAsync(code);
244+
return VerifyAsync(code);
247245
}
248246

249247
[Theory]
@@ -262,7 +260,7 @@ internal MyCarterModule(string s, int i) {}
262260
}
263261
""";
264262

265-
return this.VerifyAsync(code);
263+
return VerifyAsync(code);
266264
}
267265

268266
[Theory]
@@ -278,7 +276,7 @@ public Task RecordNonCarterModuleWithConstructorDependencies_NoDiagnostic(string
278276
}
279277
""";
280278

281-
return this.VerifyAsync(code);
279+
return VerifyAsync(code);
282280
}
283281

284282
[Theory]
@@ -301,22 +299,54 @@ public MySubCarterModule(string s) {}
301299
}
302300
""";
303301

304-
return this.VerifyAsync(code);
302+
return VerifyAsync(code);
305303
}
306304

307-
private Task VerifyAsync(string code, DiagnosticResult? diagnosticResult = null)
305+
[Theory]
306+
[InlineData("class")]
307+
[InlineData("struct")]
308+
public Task EmptyPrimaryConstructor_NoDiagnostic(string type)
308309
{
309-
var carterPackage = new PackageIdentity("Carter", "8.1.0");
310-
var aspNetPackage = new PackageIdentity("Microsoft.AspNetCore.App.Ref", "8.0.0");
311-
var bclPackage = new PackageIdentity("Microsoft.NETCore.App.Ref", "8.0.0");
312-
var referenceAssemblies = new ReferenceAssemblies("net8.0", bclPackage, Path.Combine("ref", "net8.0"))
313-
.AddPackages([carterPackage, aspNetPackage]);
314-
AnalyzerTest<XUnitVerifier> test = new CSharpAnalyzerTest<CarterModuleShouldNotHaveDependenciesAnalyzer, XUnitVerifier>
315-
{
316-
TestCode = code,
317-
ReferenceAssemblies = referenceAssemblies
318-
};
310+
var code = $$"""
311+
using Carter;
312+
using Microsoft.AspNetCore.Routing;
313+
314+
{{type}} MyCarterModule() : ICarterModule
315+
{
316+
public void AddRoutes(IEndpointRouteBuilder app) {}
317+
}
318+
""";
319+
320+
return VerifyAsync(code);
321+
}
322+
323+
[Theory]
324+
[InlineData("class", "string s")]
325+
[InlineData("struct", "string s")]
326+
[InlineData("class", "string s, int i")]
327+
[InlineData("struct", "string s, int i")]
328+
public Task PrimaryConstructor_Diagnostic(string type, string parameters)
329+
{
330+
var code = $$"""
331+
using Carter;
332+
using Microsoft.AspNetCore.Routing;
333+
334+
{{type}} {|#0:MyCarterModule|}({{parameters}}) : ICarterModule
335+
{
336+
public void AddRoutes(IEndpointRouteBuilder app) {}
337+
}
338+
""";
339+
340+
var diagnosticResult = new DiagnosticResult(DiagnosticDescriptors.CarterModuleShouldNotHaveDependencies)
341+
.WithLocation(0)
342+
.WithArguments("MyCarterModule");
343+
344+
return VerifyAsync(code, diagnosticResult);
345+
}
319346

347+
private static Task VerifyAsync(string code, DiagnosticResult? diagnosticResult = null)
348+
{
349+
AnalyzerTest<XUnitVerifier> test = new CSharpPreviewAnalyzerTest<CarterModuleShouldNotHaveDependenciesAnalyzer>(code);
320350
if (diagnosticResult.HasValue)
321351
{
322352
test.ExpectedDiagnostics.Add(diagnosticResult.Value);

test/Carter.Tests/Carter.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
</ItemGroup>
55
<ItemGroup>
66
<ProjectReference Include="..\..\src\Carter\Carter.csproj" />
7+
<ProjectReference Include="..\..\src\Carter.Analyzers\Carter.Analyzers.csproj" />
78
</ItemGroup>
89
<ItemGroup>
910
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.1.0" />

0 commit comments

Comments
 (0)