Skip to content

Commit

Permalink
Added Rule0000 to Analyzer for Rule0003
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanMaron committed Feb 25, 2022
1 parent b19fff6 commit 1d509e1
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Design/Rule0003DoNotUseObjectIDsInVariablesOrProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace BusinessCentral.LinterCop.Design
[DiagnosticAnalyzer]
public class Rule0003DoNotUseObjectIDsInVariablesOrProperties : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, DiagnosticDescriptors.Rule0005VariableCasingShouldNotDIfferFromDeclaration);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, DiagnosticDescriptors.Rule0005VariableCasingShouldNotDIfferFromDeclaration, DiagnosticDescriptors.Rule0000ErrorInRule);

public override void Initialize(AnalysisContext context)
{
Expand All @@ -34,7 +34,7 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext
if (variable.Type.NavTypeKind == NavTypeKind.Array)
correctName = ((IArrayTypeSymbol)variable.Type).ElementType.Name.ToString();
else
correctName = GetCorrectName(variable.Type.NavTypeKind.ToString(), variable.Type.ToString());
correctName = GetCorrectName(variable.Type.NavTypeKind.ToString(), variable.Type.ToString(), ctx);

if (ctx.Node.ToString().Trim('"').ToUpper() != correctName.ToUpper())
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, ctx.Node.GetLocation(), new object[] { ctx.Node.ToString().Trim('"'), correctName }));
Expand Down Expand Up @@ -92,7 +92,7 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext
if (parameter.ParameterType.NavTypeKind == NavTypeKind.Array)
correctName = ((IArrayTypeSymbol)(parameter.ParameterType)).ElementType.Name.ToString();
else
correctName = GetCorrectName(parameter.ParameterType.NavTypeKind.ToString(), parameter.ParameterType.ToString());
correctName = GetCorrectName(parameter.ParameterType.NavTypeKind.ToString(), parameter.ParameterType.ToString(), ctx);

if (ctx.Node.ToString().Trim('"').ToUpper() != correctName.ToUpper())
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, ctx.Node.GetLocation(), new object[] { ctx.Node.ToString().Trim('"'), correctName }));
Expand All @@ -111,7 +111,7 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext

if (ctx.Node.GetLocation().SourceSpan.End == returnValue.DeclaringSyntaxReference.GetSyntax(CancellationToken.None).Span.End)
{
correctName = GetCorrectName(returnValue.ReturnType.NavTypeKind.ToString(), returnValue.ReturnType.ToString());
correctName = GetCorrectName(returnValue.ReturnType.NavTypeKind.ToString(), returnValue.ReturnType.ToString(), ctx);

if (ctx.Node.ToString().Trim('"').ToUpper() != correctName.ToUpper())
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, ctx.Node.GetLocation(), new object[] { ctx.Node.ToString().Trim('"'), correctName }));
Expand All @@ -125,11 +125,19 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext
}
}

private static string GetCorrectName(string kind, string OldName)
private static string GetCorrectName(string kind, string OldName, SyntaxNodeAnalysisContext ctx)
{
if (OldName.Trim().StartsWith(kind))
{
OldName = OldName.Substring(kind.Length + 1).Trim(' ', '"');
try
{
OldName = OldName.Substring(kind.Length + 1).Trim(' ', '"');
}
catch
{
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0000ErrorInRule, ctx.Node.GetLocation(), new Object[] { "Rule0003", "unknown" }));
}

}

return OldName;
Expand Down

0 comments on commit 1d509e1

Please sign in to comment.