-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Analyzer and codefix for static property
- Loading branch information
Dylan Snel
committed
Oct 23, 2023
1 parent
c749879
commit 91240c7
Showing
9 changed files
with
153 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
; Shipped analyzer releases | ||
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
; Unshipped analyzer release | ||
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md | ||
|
||
### New Rules | ||
|
||
Rule ID | Category | Severity | Notes | ||
--------|----------|----------|------- | ||
EE0002 | EpicEnums | Error | StaticPropertiesAnalyzer |
9 changes: 7 additions & 2 deletions
9
Source/EpicEnums/EpicEnums/Analyzers/PartialRecordAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
using EpicEnums.SourceGeneration.Extensions; | ||
using EpicEnums.CodeFix; | ||
using EpicEnums.SourceGeneration.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
|
||
namespace EpicEnums.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(PartialRecordCodeFixProvider)), Shared] | ||
public class PartialRecordAnalyzer : DiagnosticAnalyzer | ||
{ | ||
internal const string ErrorId = "EE0001"; | ||
readonly DiagnosticDescriptor _recordShouldBePartialDescriptor = new( | ||
id: "EE0001", | ||
id: ErrorId, | ||
Check warning on line 19 in Source/EpicEnums/EpicEnums/Analyzers/PartialRecordAnalyzer.cs GitHub Actions / build
|
||
title: "EpicEnums", | ||
messageFormat: "EpicEnums: Record {0} inherits {1} should be marked partial", | ||
category: "EpicEnums", | ||
|
65 changes: 65 additions & 0 deletions
65
Source/EpicEnums/EpicEnums/Analyzers/StaticEnumPropertiesAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using EpicEnums.CodeFix; | ||
using EpicEnums.SourceGeneration.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Data; | ||
|
||
namespace EpicEnums.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(StaticEnumPropertiesCodeFixProvider)), Shared] | ||
public class StaticEnumPropertiesAnalyzer : DiagnosticAnalyzer | ||
{ | ||
internal const string ErrorId = "EE0002"; | ||
readonly DiagnosticDescriptor _enumPropertiesShouldBeStaticDescriptor = new( | ||
id: ErrorId, | ||
title: "EpicEnums", | ||
messageFormat: "EpicEnums: Property '{0}' of type '{1}' should be marked as static", | ||
category: "EpicEnums", | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(_enumPropertiesShouldBeStaticDescriptor); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
Check warning on line 29 in Source/EpicEnums/EpicEnums/Analyzers/StaticEnumPropertiesAnalyzer.cs GitHub Actions / build
|
||
{ | ||
context.RegisterSyntaxNodeAction(AnalyzePropertyDeclaration, SyntaxKind.PropertyDeclaration); | ||
} | ||
|
||
private void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context) | ||
{ | ||
var propertyDeclaration = (PropertyDeclarationSyntax)context.Node; | ||
//#if DEBUG | ||
// if (!System.Diagnostics.Debugger.IsAttached) | ||
// { | ||
// System.Diagnostics.Debugger.Launch(); | ||
// } | ||
//#endif | ||
// Check if the enclosing type inherits from EpicEnum<T> | ||
if (propertyDeclaration.Parent is RecordDeclarationSyntax recordDeclaration && | ||
recordDeclaration.BaseList is not null) | ||
{ | ||
foreach (var baseType in recordDeclaration.BaseList.Types) | ||
{ | ||
var typeSymbol = context.SemanticModel.GetTypeInfo(baseType.Type).Type as INamedTypeSymbol; | ||
|
||
if (typeSymbol?.ConstructedFrom.Name == "EpicEnum" && | ||
typeSymbol.TypeArguments.Length == 1 && | ||
typeSymbol.TypeArguments[0].Name == propertyDeclaration.Type.ToString()) | ||
{ | ||
// Check if the property is not static | ||
if (!propertyDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword)) | ||
{ | ||
var diagnostic = Diagnostic.Create(_enumPropertiesShouldBeStaticDescriptor, propertyDeclaration.GetLocation(), propertyDeclaration.Identifier.Text, propertyDeclaration.Type.ToString()); | ||
context.ReportDiagnostic(diagnostic); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
12 changes: 8 additions & 4 deletions
12
...Analyzers/PartialRecordCodeFixProvider.cs → ...s/CodeFix/PartialRecordCodeFixProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Source/EpicEnums/EpicEnums/CodeFix/StaticEnumPropertiesCodeFixProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using EpicEnums.Analyzers; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Editing; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Text; | ||
|
||
namespace EpicEnums.CodeFix; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(StaticEnumPropertiesCodeFixProvider)), Shared] | ||
internal class StaticEnumPropertiesCodeFixProvider : CodeFixProvider | ||
{ | ||
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(StaticEnumPropertiesAnalyzer.ErrorId); | ||
|
||
public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
||
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
var diagnostic = context.Diagnostics.First(); | ||
var diagnosticSpan = diagnostic.Location.SourceSpan; | ||
|
||
// Find the property declaration identified by the diagnostic. | ||
var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<PropertyDeclarationSyntax>().First(); | ||
Check warning on line 30 in Source/EpicEnums/EpicEnums/CodeFix/StaticEnumPropertiesCodeFixProvider.cs GitHub Actions / build
|
||
|
||
// Register a code action that will invoke the fix. | ||
context.RegisterCodeFix( | ||
CodeAction.Create( | ||
title: "Make property static", | ||
createChangedDocument: c => MakePropertyStaticAsync(context.Document, declaration, c), | ||
equivalenceKey: "MakePropertyStatic"), | ||
diagnostic); | ||
} | ||
|
||
private async Task<Document> MakePropertyStaticAsync(Document document, PropertyDeclarationSyntax propertyDecl, CancellationToken cancellationToken) | ||
{ | ||
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); | ||
var staticToken = SyntaxFactory.Token(SyntaxKind.StaticKeyword); | ||
|
||
// Add the static modifier to the property | ||
editor.SetModifiers(propertyDecl, editor.Generator.GetModifiers(propertyDecl).WithIsStatic(true)); | ||
|
||
return editor.GetChangedDocument(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters