Skip to content

Commit 571f9ac

Browse files
committed
Feat: CodeOFChaos.Extensions.Anazlyers
Gives warning on weird ToGuid() input
1 parent 470caf3 commit 571f9ac

File tree

11 files changed

+189
-1
lines changed

11 files changed

+189
-1
lines changed

CodeOfChaos.Extensions.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeOfChaos.Extensions.Seri
2424
EndProject
2525
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.CodeOfChaos.Extensions.Serilog", "tests\Tests.CodeOfChaos.Extensions.Serilog\Tests.CodeOfChaos.Extensions.Serilog.csproj", "{8670FBAC-E420-4DC6-82B1-AF0C5BF7F797}"
2626
EndProject
27+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeOfChaos.Extensions.Analyzers", "src\CodeOfChaos.Extensions.Analyzers\CodeOfChaos.Extensions.Analyzers.csproj", "{E9BB7403-5167-46CA-BE84-F420CF28C910}"
28+
EndProject
29+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{C632855B-76D1-4D5E-9A0A-E631B5FE71A2}"
30+
EndProject
31+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples.CodeOfChaos.Extensions.Analyzers", "examples\Examples.CodeOfChaos.Extensions.Analyzers\Examples.CodeOfChaos.Extensions.Analyzers.csproj", "{50072DFE-34A3-4E93-A2A1-02CDDCA66CBE}"
32+
EndProject
2733
Global
2834
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2935
Debug|Any CPU = Debug|Any CPU
@@ -66,6 +72,14 @@ Global
6672
{8670FBAC-E420-4DC6-82B1-AF0C5BF7F797}.Debug|Any CPU.Build.0 = Debug|Any CPU
6773
{8670FBAC-E420-4DC6-82B1-AF0C5BF7F797}.Release|Any CPU.ActiveCfg = Release|Any CPU
6874
{8670FBAC-E420-4DC6-82B1-AF0C5BF7F797}.Release|Any CPU.Build.0 = Release|Any CPU
75+
{E9BB7403-5167-46CA-BE84-F420CF28C910}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
76+
{E9BB7403-5167-46CA-BE84-F420CF28C910}.Debug|Any CPU.Build.0 = Debug|Any CPU
77+
{E9BB7403-5167-46CA-BE84-F420CF28C910}.Release|Any CPU.ActiveCfg = Release|Any CPU
78+
{E9BB7403-5167-46CA-BE84-F420CF28C910}.Release|Any CPU.Build.0 = Release|Any CPU
79+
{50072DFE-34A3-4E93-A2A1-02CDDCA66CBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
80+
{50072DFE-34A3-4E93-A2A1-02CDDCA66CBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
81+
{50072DFE-34A3-4E93-A2A1-02CDDCA66CBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
82+
{50072DFE-34A3-4E93-A2A1-02CDDCA66CBE}.Release|Any CPU.Build.0 = Release|Any CPU
6983
EndGlobalSection
7084
GlobalSection(NestedProjects) = preSolution
7185
{26284571-0E09-4BAF-8C2B-DF87DCC1BA0B} = {8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}
@@ -77,5 +91,7 @@ Global
7791
{BC0AB42E-28A5-47FC-9017-1191C6899645} = {8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}
7892
{DCFDADB7-06BE-49BB-A71F-3124A48B0ECF} = {197E72AD-DEAB-4350-AFC3-A3BB38720BF5}
7993
{8670FBAC-E420-4DC6-82B1-AF0C5BF7F797} = {8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}
94+
{E9BB7403-5167-46CA-BE84-F420CF28C910} = {197E72AD-DEAB-4350-AFC3-A3BB38720BF5}
95+
{50072DFE-34A3-4E93-A2A1-02CDDCA66CBE} = {C632855B-76D1-4D5E-9A0A-E631B5FE71A2}
8096
EndGlobalSection
8197
EndGlobal
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\CodeOfChaos.Extensions.Analyzers\CodeOfChaos.Extensions.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
10+
<ProjectReference Include="..\..\src\CodeOfChaos.Extensions\CodeOfChaos.Extensions.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using System;
5+
6+
namespace Examples.CodeOfChaos.Extensions.Analyzers;
7+
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public class ToGuidDiagnostic {
12+
public Guid Something() {
13+
return "b60fbc5b-86eb-472b-bfc6-0439d9a70e94".ToGuid();
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Release 1.0
2+
3+
### New Rules
4+
5+
Rule ID | Category | Severity | Notes
6+
--------|----------|----------|------------------------
7+
COCE001 | Usage | Warning | ToGuid was not parsable
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### New Rules
2+
3+
Rule ID | Category | Severity | Notes
4+
--------|----------|----------|-------
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<IsPackable>true</IsPackable>
6+
<Nullable>enable</Nullable>
7+
<LangVersion>latest</LangVersion>
8+
9+
<DevelopmentDependency>true</DevelopmentDependency>
10+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
11+
<IsRoslynComponent>true</IsRoslynComponent>
12+
13+
<!-- Main package name -->
14+
<PackageId>CodeOfChaos.Extensions.Analyzers</PackageId>
15+
<Version>0.23.1</Version>
16+
<Authors>Anna Sas</Authors>
17+
<Description>Additional diagnostics to make CodeOfChaos.Extensions easier to use as a developer</Description>
18+
<PackageProjectUrl>https://github.com/code-of-chaos/cs-code_of_chaos-extensions</PackageProjectUrl>
19+
<PackageTags>extensions linq collections string</PackageTags>
20+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
21+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
22+
<IncludeSymbols>true</IncludeSymbols>
23+
<DebugType>embedded</DebugType>
24+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
25+
<PackageReadmeFile>README.md</PackageReadmeFile>
26+
<PackageIcon>icon.png</PackageIcon>
27+
</PropertyGroup>
28+
29+
<ItemGroup>
30+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
31+
<PrivateAssets>all</PrivateAssets>
32+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
33+
</PackageReference>
34+
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.12.0">
35+
<PrivateAssets>all</PrivateAssets>
36+
</PackageReference>
37+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0">
38+
<PrivateAssets>all</PrivateAssets>
39+
</PackageReference>
40+
</ItemGroup>
41+
42+
<ItemGroup>
43+
<None Include="..\..\LICENSE" Pack="true" PackagePath="" Visible="false" />
44+
<None Include="README.md" Pack="true" PackagePath="" />
45+
<None Include="../../assets/icon.png" Pack="true" PackagePath="" Visible="false" />
46+
</ItemGroup>
47+
48+
<ItemGroup>
49+
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
50+
</ItemGroup>
51+
52+
</Project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.CodeAnalysis;
5+
using Microsoft.CodeAnalysis.CSharp;
6+
using Microsoft.CodeAnalysis.CSharp.Syntax;
7+
using Microsoft.CodeAnalysis.Diagnostics;
8+
using System;
9+
using System.Collections.Immutable;
10+
11+
namespace CodeOfChaos.Extensions.Analyzers;
12+
// ---------------------------------------------------------------------------------------------------------------------
13+
// Code
14+
// ---------------------------------------------------------------------------------------------------------------------
15+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
16+
public class GuidValidationAnalyzer : DiagnosticAnalyzer {
17+
private static readonly DiagnosticDescriptor Rule = new(
18+
"COCE001",
19+
"Invalid GUID in .ToGuid() call",
20+
"The string '{0}' is not a valid GUID",
21+
"Usage",
22+
DiagnosticSeverity.Warning,
23+
true,
24+
"Checks if the string used with the ToGuid() method is a valid GUID.");
25+
26+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Rule];
27+
28+
// -----------------------------------------------------------------------------------------------------------------
29+
// Methods
30+
// -----------------------------------------------------------------------------------------------------------------
31+
public override void Initialize(AnalysisContext context) {
32+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
33+
context.EnableConcurrentExecution();
34+
context.RegisterSyntaxNodeAction(AnalyzeSyntaxNode, SyntaxKind.InvocationExpression);
35+
}
36+
37+
private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context) {
38+
if (context.Node is not InvocationExpressionSyntax invocation)
39+
return;
40+
41+
if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess
42+
|| memberAccess.Name.Identifier.Text != "ToGuid")
43+
return;
44+
45+
if (memberAccess.Expression is not LiteralExpressionSyntax literalExpression
46+
|| literalExpression.Kind() != SyntaxKind.StringLiteralExpression)
47+
return;
48+
49+
string guidString = literalExpression.Token.ValueText;
50+
if (Guid.TryParse(guidString, out _)) return; // If the string is a valid GUID, we don't need to report a diagnostic.
51+
52+
var diagnostic = Diagnostic.Create(Rule, literalExpression.GetLocation(), guidString);
53+
context.ReportDiagnostic(diagnostic);
54+
}
55+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"DebugRoslynSourceGenerator": {
5+
"commandName": "DebugRoslynComponent",
6+
"targetProject": "../../examples/Examples.CodeOfChaos.Extensions.Analyzers/Examples.CodeOfChaos.Extensions.Analyzers.csproj"
7+
}
8+
}
9+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# CodeOfChaos.Extensions.Analyzers
2+
Additional diagnostics to make CodeOfChaos.Extensions easier to use as a developer

src/CodeOfChaos.Extensions/CodeOfChaos.Extensions.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,18 @@
2828
<None Include="../../assets/icon.png" Pack="true" PackagePath="" Visible="false" />
2929
</ItemGroup>
3030

31+
<ItemGroup>
32+
<ProjectReference Include="..\CodeOfChaos.Extensions.Analyzers\CodeOfChaos.Extensions.Analyzers.csproj" PrivateAssets="all">
33+
<IncludeAssets>compile; runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
34+
</ProjectReference>
35+
</ItemGroup>
36+
37+
<!-- Manually include the Analyzer DLL in the package -->
38+
<ItemGroup>
39+
<None Include="..\CodeOfChaos.Extensions.Analyzers\bin\Release\netstandard2.0\CodeOfChaos.Extensions.Analyzers.dll"
40+
Pack="true"
41+
PackagePath="analyzers/dotnet/cs"
42+
Visible="false" />
43+
</ItemGroup>
44+
3145
</Project>

0 commit comments

Comments
 (0)