Skip to content

Commit

Permalink
Merge pull request #507 from StefanMaron/development
Browse files Browse the repository at this point in the history
Resolve NullReferenceException
  • Loading branch information
Arthurvdv authored Jan 12, 2024
2 parents cc23f14 + 1f9165a commit 6edac70
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions Design/Rule0003DoNotUseObjectIDsInVariablesOrProperties.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
using Microsoft.Dynamics.Nav.CodeAnalysis;
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics;
using Microsoft.Dynamics.Nav.CodeAnalysis.Symbols;
using System.Collections.Immutable;

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)
{
context.RegisterSyntaxNodeAction(new Action<SyntaxNodeAnalysisContext>(this.CheckForObjectIDsInVariablesOrProperties), new SyntaxKind[] {
SyntaxKind.ObjectReference,
SyntaxKind.PermissionValue
});

}

private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext ctx)
{
if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return;
if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return;

var correctName = "";

string correctName;
if (ctx.ContainingSymbol.Kind == SymbolKind.LocalVariable || ctx.ContainingSymbol.Kind == SymbolKind.GlobalVariable)
{
IVariableSymbol variable = (IVariableSymbol)ctx.ContainingSymbol;
if (variable.Type.NavTypeKind == NavTypeKind.DotNet) return;
if (variable.Type.GetNavTypeKindSafe() == NavTypeKind.DotNet) return;

if (variable.Type.NavTypeKind == NavTypeKind.Array)
if (variable.Type.GetNavTypeKindSafe() == NavTypeKind.Array)
correctName = ((IArrayTypeSymbol)variable.Type).ElementType.Name.ToString();
else
correctName = variable.Type.Name;
Expand All @@ -43,7 +43,6 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext
if (ctx.ContainingSymbol.Kind == SymbolKind.Property)
{
IPropertySymbol property = (IPropertySymbol)ctx.ContainingSymbol;

if (ctx.Node.Kind == SyntaxKind.PermissionValue)
{
var nodes = ctx.Node.ChildNodesAndTokens().GetEnumerator();
Expand All @@ -60,7 +59,6 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, nodes.Current.GetLocation(), new object[] { "", "the object name" }));
};
}

};
}

Expand All @@ -80,15 +78,12 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext

foreach (IParameterSymbol parameter in method.Parameters)
{
if (parameter.ParameterType.NavTypeKind == NavTypeKind.DotNet)
{
continue;
}
if (parameter.ParameterType.GetNavTypeKindSafe() == NavTypeKind.DotNet) continue;

if (ctx.Node.GetLocation().SourceSpan.End == parameter.DeclaringSyntaxReference.GetSyntax(CancellationToken.None).Span.End)
{
if (parameter.ParameterType.NavTypeKind == NavTypeKind.Array)
correctName = ((IArrayTypeSymbol)(parameter.ParameterType)).ElementType.Name.ToString();
if (parameter.ParameterType.GetNavTypeKindSafe() == NavTypeKind.Array)
correctName = ((IArrayTypeSymbol)parameter.ParameterType).ElementType.Name.ToString();
else
correctName = parameter.ParameterType.Name;

Expand All @@ -99,21 +94,27 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration, ctx.Node.GetLocation(), new object[] { correctName, "" }));
}
}
IReturnValueSymbol returnValue = method.ReturnValueSymbol;
if (returnValue == null || returnValue.ReturnType.NavTypeKind == NavTypeKind.DotNet)
return;

if (ctx.Node.GetLocation().SourceSpan.End == returnValue.DeclaringSyntaxReference.GetSyntax(CancellationToken.None).Span.End)
try
{
correctName = returnValue.ReturnType.Name;
IReturnValueSymbol returnValue = method.ReturnValueSymbol;
if (returnValue == null || returnValue.ReturnType.GetNavTypeKindSafe() == NavTypeKind.DotNet) return;

if (ctx.Node.GetLastToken().ToString().Trim('"').ToUpper() != correctName.ToUpper())
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, ctx.Node.GetLocation(), new object[] { ctx.Node.ToString().Trim('"'), correctName }));
if (ctx.Node.GetLocation().SourceSpan.End == returnValue.DeclaringSyntaxReference.GetSyntax(CancellationToken.None).Span.End)
{
correctName = returnValue.ReturnType.Name;

if (ctx.Node.GetLastToken().ToString().Trim('"') != correctName)
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration, ctx.Node.GetLocation(), new object[] { correctName, "" }));
if (ctx.Node.GetLastToken().ToString().Trim('"').ToUpper() != correctName.ToUpper())
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, ctx.Node.GetLocation(), new object[] { ctx.Node.ToString().Trim('"'), correctName }));

if (ctx.Node.GetLastToken().ToString().Trim('"') != correctName)
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration, ctx.Node.GetLocation(), new object[] { correctName, "" }));
}
}
catch (NullReferenceException)
{
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0000ErrorInRule, ctx.Node.GetLocation(), new Object[] { "Rule0003/Rule0005", "NullReferenceException", "" }));
}
}
}
}
}
}

0 comments on commit 6edac70

Please sign in to comment.