Skip to content

Commit eeadee0

Browse files
committed
Remove duplicate using statements
1 parent 014bdc9 commit eeadee0

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

Net.Sdk.Web.Extensions.SourceGenerators/Net.Sdk.Web.Extensions.SourceGenerators.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>0.8.22</Version>
5+
<Version>0.9.0</Version>
66
<LangVersion>latest</LangVersion>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
<Authors>Alexandru Macocian</Authors>
99
<Description>Source generators extensions for Net.Sdk.Web. Generates mapping to be used in AOT projects.</Description>
1010
<EnableNETAnalyzers>true</EnableNETAnalyzers>
1111
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
12+
<EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>
1213
<IsRoslynComponent>true</IsRoslynComponent>
1314
<IsRoslynAnalyzer>true</IsRoslynAnalyzer>
1415
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
1516
</PropertyGroup>
1617

1718
<ItemGroup>
18-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
19+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
1920
<PrivateAssets>all</PrivateAssets>
2021
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2122
</PackageReference>
22-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.9.0" PrivateAssets="all" />
23+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" PrivateAssets="all" />
2324
<PackageReference Include="Sybil" Version="0.8.4" PrivateAssets="all" />
2425
</ItemGroup>
2526

Net.Sdk.Web.Extensions.SourceGenerators/UseRoutesGenerator.cs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
using Microsoft.CodeAnalysis.CSharp;
33
using Microsoft.CodeAnalysis.CSharp.Syntax;
44
using Sybil;
5-
using System;
65
using System.Collections.Generic;
76
using System.Collections.Immutable;
8-
using System.Diagnostics;
97
using System.Linq;
10-
using System.Runtime.CompilerServices;
118
using System.Text;
129

1310
namespace Net.Sdk.Web.Extensions.SourceGenerators;
@@ -68,16 +65,17 @@ private static void Execute(Compilation compilation, ImmutableArray<(ClassDeclar
6865
}
6966

7067
var languageVersion = maybeLanguageVersion.Value;
71-
var builder = SyntaxBuilder.CreateCompilationUnit()
72-
.WithUsing(Constants.UsingSystemThreading)
73-
.WithUsing(Constants.UsingMicrosoftAspNetCoreRouting)
74-
.WithUsing(Constants.UsingMicrosoftAspNetCoreHttp)
75-
.WithUsing(Constants.UsingMicrosoftAspNetCoreBuilder)
76-
.WithUsing(Constants.UsingMicrosoftAspNetCoreMvc)
77-
.WithUsing(Constants.UsingSystemRuntimeCompilerServices);
78-
79-
var routeUsings = new HashSet<string>();
80-
68+
var routeUsings = new HashSet<string>
69+
{
70+
Constants.UsingSystemThreading,
71+
Constants.UsingMicrosoftAspNetCoreRouting,
72+
Constants.UsingMicrosoftAspNetCoreHttp,
73+
Constants.UsingMicrosoftAspNetCoreBuilder,
74+
Constants.UsingMicrosoftAspNetCoreMvc,
75+
Constants.UsingSystemRuntimeCompilerServices
76+
};
77+
78+
var builder = SyntaxBuilder.CreateCompilationUnit();
8179
var namespaceBuilder = languageVersion >= LanguageVersion.CSharp10 ? SyntaxBuilder.CreateFileScopedNamespace(Constants.Namespace) : SyntaxBuilder.CreateNamespace(Constants.Namespace);
8280
builder.WithNamespace(namespaceBuilder);
8381
var webAppBuilder = SyntaxBuilder.CreateClass(Constants.WebApplicationExtensionsName)
@@ -88,13 +86,7 @@ private static void Execute(Compilation compilation, ImmutableArray<(ClassDeclar
8886
.WithModifiers($"{Constants.Public} {Constants.Static}")
8987
.WithAttribute(SyntaxBuilder.CreateAttribute(Constants.MethodImplAttribute)
9088
.WithRawArgument(Constants.MethodImplArgument));
91-
var registerWebAppMethodBuilder = SyntaxBuilder.CreateMethod(Constants.WebApplicationTypeName, Constants.RegisterRoutesMethodName)
92-
.WithParameter(Constants.WebApplicationTypeName, Constants.BuilderParameterName)
93-
.WithModifiers($"{Constants.Public} {Constants.Static}")
94-
.WithAttribute(SyntaxBuilder.CreateAttribute(Constants.MethodImplAttribute)
95-
.WithRawArgument(Constants.MethodImplArgument));
9689
webAppBuilder.WithMethod(useRoutesWebAppMethodBuilder);
97-
webAppBuilder.WithMethod(registerWebAppMethodBuilder);
9890

9991
var useRoutesWebAppBody = new StringBuilder();
10092
foreach (var classToMethodMap in classToMethodMapping)
@@ -164,22 +156,22 @@ or Constants.DeleteAttributeName
164156
var innerPattern = $"/{pattern?.Trim('/')}";
165157
var finalPattern = $"{outerPattern}{innerPattern}".Replace("//", "/");
166158

167-
if (methodAttributes.FirstOrDefault(a => a.Name.ToString() is Constants.GetAttributeName or Constants.GetAttributeShortName) is AttributeSyntax &&
159+
if (methodAttributes.FirstOrDefault(a => a.Name.ToString() is Constants.GetAttributeName or Constants.GetAttributeShortName) is not null &&
168160
GetMethodBodyByType("Get", finalPattern, classDeclarationSyntax, methodDeclarationSyntax, compilation, routeUsings) is string getMethodBody)
169161
{
170162
useRoutesWebAppBody.AppendLine(getMethodBody);
171163
}
172-
else if (methodAttributes.FirstOrDefault(a => a.Name.ToString() is Constants.PostAttributeName or Constants.PostAttributeShortName) is AttributeSyntax &&
164+
else if (methodAttributes.FirstOrDefault(a => a.Name.ToString() is Constants.PostAttributeName or Constants.PostAttributeShortName) is not null &&
173165
GetMethodBodyByType("Post", finalPattern, classDeclarationSyntax, methodDeclarationSyntax, compilation, routeUsings) is string postMethodBody)
174166
{
175167
useRoutesWebAppBody.AppendLine(postMethodBody);
176168
}
177-
else if (methodAttributes.FirstOrDefault(a => a.Name.ToString() is Constants.PutAttributeName or Constants.PutAttributeShortName) is AttributeSyntax &&
169+
else if (methodAttributes.FirstOrDefault(a => a.Name.ToString() is Constants.PutAttributeName or Constants.PutAttributeShortName) is not null &&
178170
GetMethodBodyByType("Put", finalPattern, classDeclarationSyntax, methodDeclarationSyntax, compilation, routeUsings) is string putMethodBoty)
179171
{
180172
useRoutesWebAppBody.AppendLine(putMethodBoty);
181173
}
182-
else if (methodAttributes.FirstOrDefault(a => a.Name.ToString() is Constants.DeleteAttributeName or Constants.DeleteAttributeShortName) is AttributeSyntax &&
174+
else if (methodAttributes.FirstOrDefault(a => a.Name.ToString() is Constants.DeleteAttributeName or Constants.DeleteAttributeShortName) is not null &&
183175
GetMethodBodyByType("Delete", finalPattern, classDeclarationSyntax, methodDeclarationSyntax, compilation, routeUsings) is string deleteMethodBody)
184176
{
185177
useRoutesWebAppBody.AppendLine(deleteMethodBody);
@@ -189,7 +181,6 @@ or Constants.DeleteAttributeName
189181

190182
useRoutesWebAppBody.AppendLine("return builder;");
191183
useRoutesWebAppMethodBuilder.WithBody(useRoutesWebAppBody.ToString());
192-
registerWebAppMethodBuilder.WithBody(useRoutesWebAppBody.ToString());
193184
foreach (var classUsing in routeUsings)
194185
{
195186
builder.WithUsing(classUsing);
@@ -231,15 +222,13 @@ or Constants.DeleteAttributeName
231222
return true;
232223
}))
233224
{
234-
235225
if (attribute.Name is not GenericNameSyntax genericNameSyntax || genericNameSyntax.TypeArgumentList.Arguments.Count != 1)
236226
{
237227
continue;
238228
}
239229

240230
var routeFilterTypeSyntax = genericNameSyntax.TypeArgumentList.Arguments[0];
241-
var routeFilterTypeSymbol = semanticModel.GetSymbolInfo(routeFilterTypeSyntax).Symbol as ITypeSymbol;
242-
if (routeFilterTypeSymbol is null)
231+
if (semanticModel.GetSymbolInfo(routeFilterTypeSyntax).Symbol is not ITypeSymbol routeFilterTypeSymbol)
243232
{
244233
continue;
245234
}
@@ -273,10 +262,10 @@ or Constants.DeleteAttributeName
273262
return returnTypeSymbol?.ToDisplayString() switch
274263
{
275264
"System.Threading.Tasks.Task<Microsoft.AspNetCore.Http.IResult>" => @$"
276-
builder.Map{type}(""{pattern}"", (HttpContext httpContext, {classDeclarationSyntax.Identifier} route{(parameters.Length > 0 ? $", {parameters}" : "")}) =>
265+
builder.Map{type}(""{pattern}"", async (HttpContext httpContext, {classDeclarationSyntax.Identifier} route{(parameters.Length > 0 ? $", {parameters}" : "")}) =>
277266
{{
278267
var cancellationToken = httpContext.RequestAborted;
279-
return route.{methodDeclarationSyntax.Identifier}({variables});
268+
return await route.{methodDeclarationSyntax.Identifier}({variables});
280269
}}){routeFilterSb};",
281270
"Microsoft.AspNetCore.Http.IResult" => @$"
282271
builder.Map{type}(""{pattern}"", (HttpContext httpContext, {classDeclarationSyntax.Identifier} route{(parameters.Length > 0 ? $", {parameters}" : "")}) =>

0 commit comments

Comments
 (0)