Skip to content

Commit 95f4301

Browse files
committed
Polishing before the release.
1 parent 7c2f636 commit 95f4301

File tree

10 files changed

+141
-134
lines changed

10 files changed

+141
-134
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
push:
1111
branches:
1212
- main
13+
1314
jobs:
1415
deploy:
1516
runs-on: ubuntu-latest
@@ -18,16 +19,16 @@ jobs:
1819
- name: Checkout
1920
uses: actions/checkout@v4
2021

21-
- name: Setup .NET Core
22+
- name: Setup .NET
2223
uses: actions/setup-dotnet@v4
2324
with:
24-
global-json-file: global.json
25+
dotnet-version: '8.0.x'
2526

2627
- name: Build
27-
run: dotnet build ${{ env.PROJECT_PATH }}
28+
run: dotnet build ${{ env.PROJECT_PATH }} --configuration Release
2829

2930
- name: Pack
30-
run: dotnet pack ${{ env.PROJECT_PATH }} --output ${{ env.OUTPUT_DIR }}
31+
run: dotnet pack ${{ env.PROJECT_PATH }} --configuration Release --output ${{ env.OUTPUT_DIR }}
3132

3233
- name: Publish
3334
run: dotnet nuget push ${{ env.OUTPUT_DIR }}/*.nupkg -k ${{ env.NUGET_API_KEY }} -s ${{ env.NUGET_SOURCE }}

src/Analyzers/AnalyzerReleases.Shipped.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
### New Rules
44

55
| Rule ID | Category | Severity | Notes |
6-
|--------:|------------|----------|--------------------------------|
6+
|---------|------------|----------|--------------------------------|
77
| PT0001 | AsyncUsage | Warning | AsyncMethodConventionsAnalyzer |
88
| PT0002 | AsyncUsage | Warning | AsyncMethodConventionsAnalyzer |
99
| PT0003 | AsyncUsage | Warning | AsyncMethodConventionsAnalyzer |
10-
| PT0004 | AsyncUsage | Warning | AsyncMethodConventionsAnalyzer |
10+
| PT0004 | AsyncUsage | Warning | AsyncMethodConventionsAnalyzer |

src/Analyzers/AnalyzerReleases.Unshipped.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Release Unshipped
2+
13
### New Rules
24

35
| Rule ID | Category | Severity | Notes |

src/Analyzers/Analyzers.csproj

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.1</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<LangVersion>latest</LangVersion>
77

@@ -16,24 +16,28 @@
1616
<AssemblyName>Pandatech.Analyzers</AssemblyName>
1717

1818
<PackageId>Pandatech.Analyzers</PackageId>
19+
<PackageIcon>pandatech.png</PackageIcon>
20+
<PackageReadmeFile>Readme.md</PackageReadmeFile>
1921
<Version>1.0.0</Version>
2022
<Authors>Pandatech</Authors>
2123
<Description>Pandatech Roslyn analyzers enforcing company coding rules.</Description>
2224
<PackageLicenseExpression>MIT</PackageLicenseExpression>
23-
<PackageTags>Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar</PackageTags>
25+
<PackageTags>Pandatech, analyzers, roslyn, async, cancellation, coding-rules</PackageTags>
2426
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-analyzers</RepositoryUrl>
2527
<PackageReleaseNotes>Init</PackageReleaseNotes>
2628

27-
<!-- Optional: silence RS1038, because we *do* intentionally reference Workspaces -->
28-
<NoWarn>RS1038</NoWarn>
29-
29+
30+
</PropertyGroup>
31+
32+
<PropertyGroup>
33+
<NoWarn>RS1038;NU5128;RS2007</NoWarn>
3034
</PropertyGroup>
3135

3236
<ItemGroup>
33-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" PrivateAssets="all" />
37+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" PrivateAssets="all"/>
3438
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers"
3539
Version="3.11.0"
36-
PrivateAssets="all" />
40+
PrivateAssets="all"/>
3741
</ItemGroup>
3842

3943
<ItemGroup>
@@ -45,7 +49,7 @@
4549
<None Include="$(TargetPath)"
4650
Pack="true"
4751
PackagePath="analyzers/dotnet/cs"
48-
Visible="false" />
52+
Visible="false"/>
4953
</ItemGroup>
5054

5155
</Project>

src/Analyzers/Async/AsyncMethodConventionsAnalyzer.cs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,31 @@ public sealed class AsyncMethodConventionsAnalyzer : DiagnosticAnalyzer
2020
"Async method '{0}' should be suffixed with 'Async'",
2121
"AsyncUsage",
2222
DiagnosticSeverity.Warning,
23-
isEnabledByDefault: true);
23+
true);
2424

2525
private static readonly DiagnosticDescriptor CancellationTokenMissingRule = new(
2626
CancellationTokenMissingId,
2727
"Async methods must accept a CancellationToken",
2828
"Async method '{0}' should declare a CancellationToken parameter",
2929
"AsyncUsage",
3030
DiagnosticSeverity.Warning,
31-
isEnabledByDefault: true);
31+
true);
3232

3333
private static readonly DiagnosticDescriptor CancellationTokenNameRule = new(
3434
CancellationTokenNameId,
3535
"CancellationToken parameter must be named 'ct'",
3636
"Async method '{0}' has CancellationToken parameter '{1}'; rename it to 'ct'",
3737
"AsyncUsage",
3838
DiagnosticSeverity.Warning,
39-
isEnabledByDefault: true);
39+
true);
4040

4141
private static readonly DiagnosticDescriptor CancellationTokenPositionRule = new(
4242
CancellationTokenPositionId,
4343
"CancellationToken parameter must be last",
4444
"Async method '{0}' has CancellationToken parameter '{1}' that should be the last parameter",
4545
"AsyncUsage",
4646
DiagnosticSeverity.Warning,
47-
isEnabledByDefault: true);
47+
true);
4848

4949
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
5050
ImmutableArray.Create(
@@ -118,8 +118,7 @@ private static void AnalyzeAnonymousFunction(OperationAnalysisContext context)
118118
context.ReportDiagnostic);
119119
}
120120

121-
private static void AnalyzeAsyncMember(
122-
IMethodSymbol method,
121+
private static void AnalyzeAsyncMember(IMethodSymbol method,
123122
Location location,
124123
string displayName,
125124
Action<Diagnostic> report)
@@ -160,31 +159,13 @@ private static bool IsTaskLike(INamedTypeSymbol type)
160159
return type.Name is "Task" or "ValueTask";
161160
}
162161

163-
private readonly struct CtInfo
164-
{
165-
public bool HasCt { get; }
166-
public bool IsNamedCt { get; }
167-
public bool IsLast { get; }
168-
public string Name { get; }
169-
170-
public CtInfo(bool hasCt, bool isNamedCt, bool isLast, string name)
171-
{
172-
HasCt = hasCt;
173-
IsNamedCt = isNamedCt;
174-
IsLast = isLast;
175-
Name = name;
176-
}
177-
}
178-
179162
private static CtInfo GetCancellationTokenInfo(IMethodSymbol method)
180163
{
181164
IParameterSymbol? ctParam = null;
182165
var parameters = method.Parameters;
183166

184-
for (var i = 0; i < parameters.Length; i++)
167+
foreach (var p in parameters)
185168
{
186-
var p = parameters[i];
187-
188169
if (p.Type is not INamedTypeSymbol named)
189170
{
190171
continue;
@@ -214,4 +195,12 @@ private static CtInfo GetCancellationTokenInfo(IMethodSymbol method)
214195

215196
return new CtInfo(true, isNamedCt, isLast, ctParam.Name);
216197
}
217-
}
198+
199+
private readonly struct CtInfo(bool hasCt, bool isNamedCt, bool isLast, string name)
200+
{
201+
public bool HasCt { get; } = hasCt;
202+
public bool IsNamedCt { get; } = isNamedCt;
203+
public bool IsLast { get; } = isLast;
204+
public string Name { get; } = name;
205+
}
206+
}

src/Analyzers/Async/AsyncMethodConventionsCodeFixProvider.cs

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,10 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
6060
// 1. Lambda / anonymous function case (PT0002 / PT0003 / PT0004)
6161
// ------------------------------------------------------------------
6262

63-
CSharpSyntaxNode? lambdaNode = node.FirstAncestorOrSelf<ParenthesizedLambdaExpressionSyntax>();
63+
var lambdaNode = node.FirstAncestorOrSelf<ParenthesizedLambdaExpressionSyntax>() ??
64+
(CSharpSyntaxNode?)node.FirstAncestorOrSelf<SimpleLambdaExpressionSyntax>();
6465

65-
if (lambdaNode is null)
66-
{
67-
lambdaNode = node.FirstAncestorOrSelf<SimpleLambdaExpressionSyntax>();
68-
}
69-
70-
if (lambdaNode is null)
71-
{
72-
lambdaNode = node.FirstAncestorOrSelf<AnonymousMethodExpressionSyntax>();
73-
}
66+
lambdaNode ??= node.FirstAncestorOrSelf<AnonymousMethodExpressionSyntax>();
7467

7568
if (lambdaNode is not null &&
7669
diagnosticId is AsyncMethodConventionsAnalyzer.CancellationTokenMissingId
@@ -102,7 +95,7 @@ or AsyncMethodConventionsAnalyzer.CancellationTokenNameId
10295
return;
10396
}
10497

105-
var methodSymbol = semanticModel.GetDeclaredSymbol(methodDecl, cancellationToken) as IMethodSymbol;
98+
var methodSymbol = semanticModel.GetDeclaredSymbol(methodDecl, cancellationToken);
10699
if (methodSymbol is null)
107100
{
108101
return;
@@ -152,7 +145,7 @@ private static void RegisterAsyncSuffixFix(CodeFixContext context,
152145
CodeAction.Create(
153146
title,
154147
c => RenameMethodAsync(context.Document.Project.Solution, methodSymbol, newName, c),
155-
equivalenceKey: "RenameToAsync"),
148+
"RenameToAsync"),
156149
diagnostic);
157150
}
158151

@@ -174,7 +167,7 @@ private static void RegisterCancellationTokenFixesForMethod(CodeFixContext conte
174167
CodeAction.Create(
175168
title,
176169
c => AddCancellationTokenAsync(context.Document, methodDecl, c),
177-
equivalenceKey: "AddCtParameter"),
170+
"AddCtParameter"),
178171
diagnostic);
179172

180173
return;
@@ -188,35 +181,39 @@ private static void RegisterCancellationTokenFixesForMethod(CodeFixContext conte
188181
return;
189182
}
190183

191-
if (diagnosticId == AsyncMethodConventionsAnalyzer.CancellationTokenNameId)
184+
switch (diagnosticId)
192185
{
193-
if (!string.Equals(ctParam.Name, "ct", StringComparison.Ordinal))
186+
case AsyncMethodConventionsAnalyzer.CancellationTokenNameId
187+
when string.Equals(ctParam.Name, "ct", StringComparison.Ordinal):
188+
return;
189+
case AsyncMethodConventionsAnalyzer.CancellationTokenNameId:
194190
{
195191
var renameTitle = $"Rename '{ctParam.Name}' to 'ct'";
196192

197193
context.RegisterCodeFix(
198194
CodeAction.Create(
199195
renameTitle,
200196
c => RenameParameterAsync(context.Document.Project.Solution, ctParam, "ct", c),
201-
equivalenceKey: "RenameCtParameter"),
197+
"RenameCtParameter"),
202198
diagnostic);
203-
}
204199

205-
return;
206-
}
207-
208-
if (diagnosticId == AsyncMethodConventionsAnalyzer.CancellationTokenPositionId)
209-
{
210-
if (ctParam.Ordinal != methodSymbol.Parameters.Length - 1)
200+
return;
201+
}
202+
case AsyncMethodConventionsAnalyzer.CancellationTokenPositionId:
211203
{
212-
var moveTitle = "Move CancellationToken parameter to last position";
204+
if (ctParam.Ordinal != methodSymbol.Parameters.Length - 1)
205+
{
206+
const string moveTitle = "Move CancellationToken parameter to last position";
207+
208+
context.RegisterCodeFix(
209+
CodeAction.Create(
210+
moveTitle,
211+
c => MoveCancellationTokenToLastAsync(context.Document, methodDecl, ctParam, c),
212+
"MoveCtParameterLast"),
213+
diagnostic);
214+
}
213215

214-
context.RegisterCodeFix(
215-
CodeAction.Create(
216-
moveTitle,
217-
c => MoveCancellationTokenToLastAsync(context.Document, methodDecl, ctParam, c),
218-
equivalenceKey: "MoveCtParameterLast"),
219-
diagnostic);
216+
break;
220217
}
221218
}
222219
}
@@ -249,13 +246,13 @@ private static void RegisterLambdaCancellationTokenFixes(CodeFixContext context,
249246
{
250247
case AsyncMethodConventionsAnalyzer.CancellationTokenMissingId:
251248
{
252-
var title = "Add CancellationToken ct parameter";
249+
const string title = "Add CancellationToken ct parameter";
253250

254251
context.RegisterCodeFix(
255252
CodeAction.Create(
256253
title,
257254
c => AddCancellationTokenToLambdaAsync(context.Document, lambdaExpr, c),
258-
equivalenceKey: "AddCtParameterToLambda"),
255+
"AddCtParameterToLambda"),
259256
diagnostic);
260257

261258
break;
@@ -274,7 +271,7 @@ private static void RegisterLambdaCancellationTokenFixes(CodeFixContext context,
274271
CodeAction.Create(
275272
renameTitle,
276273
c => RenameParameterAsync(context.Document.Project.Solution, ctParam, "ct", c),
277-
equivalenceKey: "RenameLambdaCtParameter"),
274+
"RenameLambdaCtParameter"),
278275
diagnostic);
279276

280277
break;
@@ -293,7 +290,7 @@ private static void RegisterLambdaCancellationTokenFixes(CodeFixContext context,
293290
CodeAction.Create(
294291
moveTitle,
295292
c => MoveCancellationTokenToLastInLambdaAsync(context.Document, lambdaExpr, ctParam, c),
296-
equivalenceKey: "MoveLambdaCtParameterLast"),
293+
"MoveLambdaCtParameterLast"),
297294
diagnostic);
298295

299296
break;
@@ -416,11 +413,13 @@ private static async Task<Document> MoveCancellationTokenToLastAsync(Document do
416413
for (var i = 0; i < parameters.Count; i++)
417414
{
418415
var p = parameters[i];
419-
if (string.Equals(p.Identifier.Text, ctSymbol.Name, StringComparison.Ordinal))
416+
if (!string.Equals(p.Identifier.Text, ctSymbol.Name, StringComparison.Ordinal))
420417
{
421-
ctIndex = i;
422-
break;
418+
continue;
423419
}
420+
421+
ctIndex = i;
422+
break;
424423
}
425424

426425
if (ctIndex < 0 || ctIndex == parameters.Count - 1)
@@ -523,12 +522,8 @@ private static async Task<Document> MoveCancellationTokenToLastInLambdaAsync(Doc
523522
{
524523
var root = await document.GetSyntaxRootAsync(cancellationToken)
525524
.ConfigureAwait(false);
526-
if (root is null)
527-
{
528-
return document;
529-
}
530525

531-
if (lambdaExpr is not ParenthesizedLambdaExpressionSyntax parenthesized)
526+
if (root is null || lambdaExpr is not ParenthesizedLambdaExpressionSyntax parenthesized)
532527
{
533528
return document;
534529
}
@@ -539,11 +534,13 @@ private static async Task<Document> MoveCancellationTokenToLastInLambdaAsync(Doc
539534
for (var i = 0; i < parameters.Count; i++)
540535
{
541536
var p = parameters[i];
542-
if (string.Equals(p.Identifier.Text, ctSymbol.Name, StringComparison.Ordinal))
537+
if (!string.Equals(p.Identifier.Text, ctSymbol.Name, StringComparison.Ordinal))
543538
{
544-
ctIndex = i;
545-
break;
539+
continue;
546540
}
541+
542+
ctIndex = i;
543+
break;
547544
}
548545

549546
if (ctIndex < 0 || ctIndex == parameters.Count - 1)

test/Analyzers.Sample/Analyzers.Sample.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<Nullable>enable</Nullable>
66
</PropertyGroup>
77

8-
<ItemGroup>
9-
<ProjectReference Include="..\..\src\Analyzers\Analyzers.csproj"
10-
OutputItemType="Analyzer"
11-
ReferenceOutputAssembly="false"
12-
PrivateAssets="all" />
13-
</ItemGroup>
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\Analyzers\Analyzers.csproj"
10+
OutputItemType="Analyzer"
11+
ReferenceOutputAssembly="false"
12+
PrivateAssets="all"/>
13+
</ItemGroup>
1414

1515
<ItemGroup>
1616
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.11"/>

0 commit comments

Comments
 (0)