Skip to content

Commit 07e2ae5

Browse files
committed
Migrated DiagnosticDescriptor declarations to their own Descriptors class
1 parent 9a7a0e1 commit 07e2ae5

13 files changed

Lines changed: 135 additions & 86 deletions

src/Lucene.Net.CodeAnalysis.Dev/LuceneDev1000_FloatingPointEqualityCSCodeAnalyzer.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,7 @@ namespace Lucene.Net.CodeAnalysis.Dev
1414
[DiagnosticAnalyzer(LanguageNames.CSharp)]
1515
public class LuceneDev1000_FloatingPointEqualityCSCodeAnalyzer : DiagnosticAnalyzer
1616
{
17-
public const string DiagnosticId = "LuceneDev1000";
18-
19-
// You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat.
20-
// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Localizing%20Analyzers.md for more on localization
21-
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.LuceneDev1000_AnalyzerTitle), Resources.ResourceManager, typeof(Resources));
22-
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.LuceneDev1000_AnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
23-
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.LuceneDev1000_AnalyzerDescription), Resources.ResourceManager, typeof(Resources));
24-
private const string Category = "Design";
25-
26-
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
27-
28-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
17+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Descriptors.LuceneDev1000_FloatingPointEquality];
2918

3019
public override void Initialize(AnalysisContext context)
3120
{
@@ -58,7 +47,7 @@ private static void AnalyzeNodeCS(SyntaxNodeAnalysisContext context)
5847
if (!FloatingPoint.IsFloatingPointType(leftSymbolInfo) && !FloatingPoint.IsFloatingPointType(rightSymbolInfo))
5948
return; // Check passed
6049

61-
context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation(), binaryExpression.ToString()));
50+
context.ReportDiagnostic(Diagnostic.Create(Descriptors.LuceneDev1000_FloatingPointEquality, context.Node.GetLocation(), binaryExpression.ToString()));
6251
}
6352
else if (context.Node is Microsoft.CodeAnalysis.CSharp.Syntax.MemberAccessExpressionSyntax memberAccessExpression)
6453
{
@@ -73,7 +62,7 @@ private static void AnalyzeNodeCS(SyntaxNodeAnalysisContext context)
7362
if (!FloatingPoint.IsFloatingPointType(leftSymbolInfo))
7463
return; // Check passed
7564

76-
context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation(), memberAccessExpression.ToString()));
65+
context.ReportDiagnostic(Diagnostic.Create(Descriptors.LuceneDev1000_FloatingPointEquality, context.Node.GetLocation(), memberAccessExpression.ToString()));
7766
}
7867
}
7968
}

src/Lucene.Net.CodeAnalysis.Dev/LuceneDev1001_FloatingPointFormattingCSCodeAnalyzer.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,7 @@ namespace Lucene.Net.CodeAnalysis.Dev
1414
[DiagnosticAnalyzer(LanguageNames.CSharp)]
1515
public class LuceneDev1001_FloatingPointFormattingCSCodeAnalyzer : DiagnosticAnalyzer
1616
{
17-
public const string DiagnosticId = "LuceneDev1001";
18-
19-
// You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat.
20-
// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Localizing%20Analyzers.md for more on localization
21-
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.LuceneDev1001_AnalyzerTitle), Resources.ResourceManager, typeof(Resources));
22-
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.LuceneDev1001_AnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
23-
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.LuceneDev1001_AnalyzerDescription), Resources.ResourceManager, typeof(Resources));
24-
private const string Category = "Design";
25-
26-
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
27-
28-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
17+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Descriptors.LuceneDev1001_FloatingPointFormatting];
2918

3019
public override void Initialize(AnalysisContext context)
3120
{
@@ -54,7 +43,7 @@ private static void AnalyzeNodeCS(SyntaxNodeAnalysisContext context)
5443
if (!FloatingPoint.IsFloatingPointType(leftSymbolInfo))
5544
return; // Check passed
5645

57-
context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation(), memberAccessExpression.ToString()));
46+
context.ReportDiagnostic(Diagnostic.Create(Descriptors.LuceneDev1001_FloatingPointFormatting, context.Node.GetLocation(), memberAccessExpression.ToString()));
5847
}
5948
}
6049
}

src/Lucene.Net.CodeAnalysis.Dev/LuceneDev1002_FloatingPointArithmeticCSCodeAnalyzer.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,7 @@ namespace Lucene.Net.CodeAnalysis.Dev
1414
[DiagnosticAnalyzer(LanguageNames.CSharp)]
1515
public class LuceneDev1002_FloatingPointArithmeticCSCodeAnalyzer : DiagnosticAnalyzer
1616
{
17-
public const string DiagnosticId = "LuceneDev1002";
18-
19-
// You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat.
20-
// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Localizing%20Analyzers.md for more on localization
21-
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.LuceneDev1002_AnalyzerTitle), Resources.ResourceManager, typeof(Resources));
22-
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.LuceneDev1002_AnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
23-
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.LuceneDev1002_AnalyzerDescription), Resources.ResourceManager, typeof(Resources));
24-
private const string Category = "Design";
25-
26-
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
27-
28-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
17+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Descriptors.LuceneDev1002_FloatingPointArithmetic];
2918

3019
public override void Initialize(AnalysisContext context)
3120
{
@@ -70,7 +59,7 @@ private static void AnalyzeNodeCS(SyntaxNodeAnalysisContext context)
7059

7160
// }
7261

73-
context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation(), context.Node.ToString()));
62+
context.ReportDiagnostic(Diagnostic.Create(Descriptors.LuceneDev1002_FloatingPointArithmetic, context.Node.GetLocation(), context.Node.ToString()));
7463
}
7564
}
7665
}

src/Lucene.Net.CodeAnalysis.Dev/LuceneDev1003_ArrayMethodParameterCSCodeAnalyzer.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.CodeAnalysis;
1+
using Lucene.Net.CodeAnalysis.Dev.Utility;
2+
using Microsoft.CodeAnalysis;
23
using Microsoft.CodeAnalysis.CSharp;
34
using Microsoft.CodeAnalysis.CSharp.Syntax;
45
using Microsoft.CodeAnalysis.Diagnostics;
@@ -9,18 +10,7 @@ namespace Lucene.Net.CodeAnalysis.Dev
910
[DiagnosticAnalyzer(LanguageNames.CSharp)]
1011
public class LuceneDev1003_ArrayMethodParameterCSCodeAnalyzer : DiagnosticAnalyzer
1112
{
12-
public const string DiagnosticId = "LuceneDev1003";
13-
14-
// You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat.
15-
// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Localizing%20Analyzers.md for more on localization
16-
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.LuceneDev1003_AnalyzerTitle), Resources.ResourceManager, typeof(Resources));
17-
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.LuceneDev1003_AnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
18-
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.LuceneDev1003_AnalyzerDescription), Resources.ResourceManager, typeof(Resources));
19-
private const string Category = "Design";
20-
21-
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
22-
23-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
13+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Descriptors.LuceneDev1003_ArrayMethodParameter];
2414

2515
public override void Initialize(AnalysisContext context)
2616
{
@@ -43,7 +33,7 @@ private static void AnalyzeNodeCS(SyntaxNodeAnalysisContext context)
4333
if (arrayTypeSyntax.ElementType is PredefinedTypeSyntax predefinedTypeSyntax)
4434
{
4535
if (predefinedTypeSyntax.Keyword.ValueText != "char")
46-
context.ReportDiagnostic(Diagnostic.Create(Rule, parameter.GetLocation(), parameter.ToString()));
36+
context.ReportDiagnostic(Diagnostic.Create(Descriptors.LuceneDev1003_ArrayMethodParameter, parameter.GetLocation(), parameter.ToString()));
4737
}
4838
}
4939
}

src/Lucene.Net.CodeAnalysis.Dev/LuceneDev1004_ArrayMethodReturnValueCSCodeAnalyzer.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,7 @@ namespace Lucene.Net.CodeAnalysis.Dev
1515
[DiagnosticAnalyzer(LanguageNames.CSharp)]
1616
public class LuceneDev1004_ArrayMethodReturnValueCSCodeAnalyzer : DiagnosticAnalyzer
1717
{
18-
public const string DiagnosticId = "LuceneDev1004";
19-
20-
// You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat.
21-
// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Localizing%20Analyzers.md for more on localization
22-
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.LuceneDev1004_AnalyzerTitle), Resources.ResourceManager, typeof(Resources));
23-
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.LuceneDev1004_AnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
24-
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.LuceneDev1004_AnalyzerDescription), Resources.ResourceManager, typeof(Resources));
25-
private const string Category = "Design";
26-
27-
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
28-
29-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
18+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Descriptors.LuceneDev1004_ArrayMethodReturnValue];
3019

3120
public override void Initialize(AnalysisContext context)
3221
{
@@ -47,7 +36,7 @@ private static void AnalyzeNodeCS(SyntaxNodeAnalysisContext context)
4736
if (arrayTypeSyntax.ElementType is PredefinedTypeSyntax predefinedTypeSyntax)
4837
{
4938
if (predefinedTypeSyntax.Keyword.ValueText != "char")
50-
context.ReportDiagnostic(Diagnostic.Create(Rule, arrayTypeSyntax.GetLocation(), arrayTypeSyntax.ToString()));
39+
context.ReportDiagnostic(Diagnostic.Create(Descriptors.LuceneDev1004_ArrayMethodReturnValue, arrayTypeSyntax.GetLocation(), arrayTypeSyntax.ToString()));
5140
}
5241
}
5342
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Lucene.Net.CodeAnalysis.Dev.Utility
6+
{
7+
public enum Category
8+
{
9+
Design,
10+
Globalization,
11+
Mobility,
12+
Performance,
13+
Security,
14+
Usage,
15+
Naming,
16+
Interoperability,
17+
Maintainability,
18+
Reliability,
19+
Documentation,
20+
}
21+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Microsoft.CodeAnalysis;
2+
using static Microsoft.CodeAnalysis.DiagnosticSeverity;
3+
using static Lucene.Net.CodeAnalysis.Dev.Utility.Category;
4+
5+
namespace Lucene.Net.CodeAnalysis.Dev.Utility
6+
{
7+
public static partial class Descriptors
8+
{
9+
public static DiagnosticDescriptor LuceneDev1000_FloatingPointEquality { get; } =
10+
Diagnostic(
11+
"LuceneDev1000",
12+
Design,
13+
Warning
14+
);
15+
16+
public static DiagnosticDescriptor LuceneDev1001_FloatingPointFormatting { get; } =
17+
Diagnostic(
18+
"LuceneDev1001",
19+
Design,
20+
Warning
21+
);
22+
23+
public static DiagnosticDescriptor LuceneDev1002_FloatingPointArithmetic { get; } =
24+
Diagnostic(
25+
"LuceneDev1002",
26+
Design,
27+
Warning
28+
);
29+
30+
public static DiagnosticDescriptor LuceneDev1003_ArrayMethodParameter { get; } =
31+
Diagnostic(
32+
"LuceneDev1003",
33+
Design,
34+
Warning
35+
);
36+
37+
public static DiagnosticDescriptor LuceneDev1004_ArrayMethodReturnValue { get; } =
38+
Diagnostic(
39+
"LuceneDev1004",
40+
Design,
41+
Warning
42+
);
43+
}
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.CodeAnalysis;
2+
using System.Collections.Concurrent;
3+
4+
namespace Lucene.Net.CodeAnalysis.Dev.Utility
5+
{
6+
public static partial class Descriptors
7+
{
8+
static readonly ConcurrentDictionary<Category, string> categoryMapping = new();
9+
10+
static DiagnosticDescriptor Diagnostic(
11+
string id,
12+
Category category,
13+
DiagnosticSeverity defaultSeverity)
14+
=> Diagnostic(id, category, defaultSeverity, isEnabledByDefault: true);
15+
16+
static DiagnosticDescriptor Diagnostic(
17+
string id,
18+
Category category,
19+
DiagnosticSeverity defaultSeverity,
20+
bool isEnabledByDefault)
21+
{
22+
//string? helpLink = null;
23+
var categoryString = categoryMapping.GetOrAdd(category, c => c.ToString());
24+
25+
var title = new LocalizableResourceString($"{id}_AnalyzerTitle", Resources.ResourceManager, typeof(Resources));
26+
var messageFormat = new LocalizableResourceString($"{id}_AnalyzerMessageFormat", Resources.ResourceManager, typeof(Resources));
27+
var description = new LocalizableResourceString($"{id}_AnalyzerDescription", Resources.ResourceManager, typeof(Resources));
28+
29+
//return new DiagnosticDescriptor(id, title, messageFormat, categoryString, defaultSeverity, isEnabledByDefault: true, helpLinkUri: helpLink);
30+
return new DiagnosticDescriptor(id, title, messageFormat, categoryString, defaultSeverity, isEnabledByDefault);
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)