Skip to content

CodeCracker Base Classes? #214

@AdamSpeight2008

Description

@AdamSpeight2008

The base class for all CodeCracker Analyzers

MustInherit Class CodeCracker_Analyzer
  Inherits CodeAnalyzer

  Public ReadOnly Property DiagnosticID As String = ""
  Public ReadOnly Property Title As String = ""
  Public ReadOnly Property Category As String = ""
  Public ReadOnly Property Severity As DiagnosticSeverity = DiagnosticSeverity.Warning
  Public ReadOnly Property MsgFormat As String = ""
  Public ReadOnly Propery IsEnable As Boolean = True

  Friend _Diagnostics As ImmutabaleArray(Of DiagnosticDescriptor)

  Friend Sub New( ID As String, Title As String, MsgFormat As String, Category As String,
                    Optional IsEnable As Boolean = True,
                    Optional Severity As DiagnosticSeverity = DiagnosticSeverity.Warning)

    MyBase.New()
    Me._DiagnosticID = ID
    Me._Title =  "ForInArrayAnalyzer") 
    Me._Category = Category 
    Me._MsgFormat = MsgFormat
    Me._Severity = Severity
    Me._IsEnabled = IsEnabled
  End Sub

  Public Overrides ReadOnly Property SupportedDiagnostics() As ImmutableArray(Of DiagnositcDescriptor)
    Get
      Return _Diagnostics
    End Get
  End Property

  Friend Function GetDescriptor() As DiagnositcDescriptor
    Return New DiagnosticDescriptor( DiagnosticID, Title, MsgFormat, Category, Severity, IsEnabledByDefault, HelpLink.ForDiagnostic( DiagnosticID ) )
  End Function

 Public Override Sub Initialize(context As AnalysisContext )
   context.RegisterSyntaxNodeAction(Analyzer, SyntaxKind.CatchClause);
 End Sub

End Class

Now a base class for CSharp Analyzers

<DiagnosticAnalyzer(LanguageNames.CSharp)>
Public MustInheric CodeCracker_CSharp
  Friend Sub New( ID As String, Title As String, MsgFormat As String, Category As String,
                    Optional IsEnable As Boolean = True,
                    Optional Severity As DiagnosticSeverity = DiagnosticSeverity.Warning)

    MyBase.New()
    Me._DiagnosticID = ID
    Me._Title =  "ForInArrayAnalyzer") 
    Ne._Category = Category 
  End Sub
End Sub

The one for each category, (This case Design)

Public MustInheric Design_Analyzer
  Inherits CodeCracker_CSharp

  Friend Sub New( ID As String, Title As String, MsgFormat As String, Optional IsEnable As Boolean = True,
                                                                      Optional Severity As DiagnosticSeverity = DiagnosticSeverity.Warning)
    MyBase.New( Id, Title, SupportedCategories.Design, MsgFormat, IsEnabled, Severity )
  End Sub

End Class

The implement the analyzer. ( CatchEmpty )

Public Class CatchEmpty
  Inherits Design_Analyzer

  Public Sub New()
    MyBase.New( "CC0003", "Your catch maye include some exception","{0}")
  End Sub

  Private Sub Analyzer(context As SyntaxNodeAnalysisContext)
    Dim catchStatement = TryCast(context.Node, CatchClauseSyntax)
    If (catchStatement Is Nothing) OrElse (catchStatement.Declaration IsNot Nothing) Then Return
    If (catchStatement.Block?.Statements.Count == 0) Then return ' there is another analizer for this: EmptyCatchBlock
    Dim diagnostic = Diagnostic.Create(Rule, catchStatement.GetLocation(), "Consider put an Exception Class in catch.");
    context.ReportDiagnostic(diagnostic);
  End Sub
End Class

Also have similar ones for CodeFixes.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions