Skip to content

Commit

Permalink
Temporary add Try/Catch to resolve exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthurvdv committed Jan 3, 2024
1 parent 0edefde commit 32def37
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
49 changes: 28 additions & 21 deletions Design/Rule0039ArgumentDifferentTypeThenExpected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace BusinessCentral.LinterCop.Design
[DiagnosticAnalyzer]
public class Rule0039ArgumentDifferentTypeThenExpected : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, DiagnosticDescriptors.Rule0000ErrorInRule);

private static readonly List<PropertyKind> referencePageProviders = new List<PropertyKind>
{
Expand Down Expand Up @@ -54,32 +54,39 @@ private void AnalyzeRunPageArguments(OperationAnalysisContext ctx)

private void AnalyzeSetRecordArgument(OperationAnalysisContext ctx)
{
if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return;
if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return;
try
{
if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return;
if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return;

IInvocationExpression operation = (IInvocationExpression)ctx.Operation;
if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return;
IInvocationExpression operation = (IInvocationExpression)ctx.Operation;
if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return;

if (operation.TargetMethod.ContainingType.GetTypeSymbol().GetNavTypeKindSafe() != NavTypeKind.Page) return;
string[] procedureNames = { "GetRecord", "SetRecord", "SetSelectionFilter", "SetTableView" };
if (!procedureNames.Contains(operation.TargetMethod.Name)) return;
if (operation.Arguments.Count() != 1) return;
if (operation.TargetMethod.ContainingType.GetTypeSymbol().GetNavTypeKindSafe() != NavTypeKind.Page) return;
string[] procedureNames = { "GetRecord", "SetRecord", "SetSelectionFilter", "SetTableView" };
if (!procedureNames.Contains(operation.TargetMethod.Name)) return;
if (operation.Arguments.Count() != 1) return;

if (operation.Arguments[0].Syntax.Kind != SyntaxKind.IdentifierName || operation.Arguments[0].Value.Kind != OperationKind.ConversionExpression) return;
if (operation.Arguments[0].Syntax.Kind != SyntaxKind.IdentifierName || operation.Arguments[0].Value.Kind != OperationKind.ConversionExpression) return;

IOperation pageReference = ctx.Operation.DescendantsAndSelf().Where(x => x.GetSymbol() != null)
.Where(x => x.Type.GetNavTypeKindSafe() == NavTypeKind.Page)
.SingleOrDefault();
if (pageReference == null) return;
IVariableSymbol variableSymbol = (IVariableSymbol)pageReference.GetSymbol().OriginalDefinition;
IPageTypeSymbol pageTypeSymbol = (IPageTypeSymbol)variableSymbol.GetTypeSymbol().OriginalDefinition;
ITableTypeSymbol pageSourceTable = pageTypeSymbol.RelatedTable;
IOperation pageReference = ctx.Operation.DescendantsAndSelf().Where(x => x.GetSymbol() != null)
.Where(x => x.Type.GetNavTypeKindSafe() == NavTypeKind.Page)
.SingleOrDefault();
if (pageReference == null) return;
IVariableSymbol variableSymbol = (IVariableSymbol)pageReference.GetSymbol().OriginalDefinition;
IPageTypeSymbol pageTypeSymbol = (IPageTypeSymbol)variableSymbol.GetTypeSymbol().OriginalDefinition;
ITableTypeSymbol pageSourceTable = pageTypeSymbol.RelatedTable;

IOperation operand = ((IConversionExpression)operation.Arguments[0].Value).Operand;
ITableTypeSymbol recordArgument = ((IRecordTypeSymbol)operand.GetSymbol().GetTypeSymbol()).BaseTable;
IOperation operand = ((IConversionExpression)operation.Arguments[0].Value).Operand;
ITableTypeSymbol recordArgument = ((IRecordTypeSymbol)operand.GetSymbol().GetTypeSymbol()).BaseTable;

if (!AreTheSameNavObjects(recordArgument, pageSourceTable))
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, ctx.Operation.Syntax.GetLocation(), new object[] { 1, operand.GetSymbol().GetTypeSymbol().ToString(), pageSourceTable.GetNavTypeKindSafe() + " \"" + pageSourceTable.Name + "\"" }));
if (!AreTheSameNavObjects(recordArgument, pageSourceTable))
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, ctx.Operation.Syntax.GetLocation(), new object[] { 1, operand.GetSymbol().GetTypeSymbol().ToString(), pageSourceTable.GetNavTypeKindSafe() + " \"" + pageSourceTable.Name + "\"" }));
}
catch (NullReferenceException)
{
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0000ErrorInRule, ctx.Operation.Syntax.GetLocation(), new Object[] { "Rule0039", "NullReferenceException" }));
}
}

private void AnalyzeTableReferencePageProvider(SymbolAnalysisContext ctx)
Expand Down
43 changes: 25 additions & 18 deletions Design/Rule0048ErrorWithTextConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,41 @@ namespace BusinessCentral.LinterCop.Design
[DiagnosticAnalyzer]
public class Rule0048ErrorWithTextConstant : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0048ErrorWithTextConstant);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0048ErrorWithTextConstant, DiagnosticDescriptors.Rule0000ErrorInRule);

public override void Initialize(AnalysisContext context) => context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeErrorMethod), OperationKind.InvocationExpression);

private void AnalyzeErrorMethod(OperationAnalysisContext ctx)
{
if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return;
if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return;
try
{
if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return;
if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return;

IInvocationExpression operation = (IInvocationExpression)ctx.Operation;
if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return;
if (!SemanticFacts.IsSameName(operation.TargetMethod.Name, "Error")) return;
if (operation.Arguments.Length == 0) return;

IInvocationExpression operation = (IInvocationExpression)ctx.Operation;
if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return;
if (!SemanticFacts.IsSameName(operation.TargetMethod.Name, "Error")) return;
if (operation.Arguments.Length == 0) return;
if (operation.Arguments[0].Value.Type.GetNavTypeKindSafe() == NavTypeKind.ErrorInfo) return;

if (operation.Arguments[0].Value.Type.GetNavTypeKindSafe() == NavTypeKind.ErrorInfo) return;
switch (operation.Arguments[0].Syntax.Kind)
{
case SyntaxKind.IdentifierName:
IOperation operand = ((IConversionExpression)operation.Arguments[0].Value).Operand;
if (operand.GetSymbol().OriginalDefinition.GetTypeSymbol().GetNavTypeKindSafe() == NavTypeKind.Label) return;
break;
case SyntaxKind.LiteralExpression:
if (operation.Arguments[0].Syntax.GetIdentifierOrLiteralValue() == "") return;
break;
}

switch (operation.Arguments[0].Syntax.Kind)
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0048ErrorWithTextConstant, ctx.Operation.Syntax.GetLocation()));
}
catch (InvalidCastException)
{
case SyntaxKind.IdentifierName:
IOperation operand = ((IConversionExpression)operation.Arguments[0].Value).Operand;
if (operand.GetSymbol().OriginalDefinition.GetTypeSymbol().GetNavTypeKindSafe() == NavTypeKind.Label) return;
break;
case SyntaxKind.LiteralExpression:
if (operation.Arguments[0].Syntax.GetIdentifierOrLiteralValue() == "") return;
break;
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0000ErrorInRule, ctx.Operation.Syntax.GetLocation(), new Object[] { "Rule0048", "InvalidCastException" }));
}

ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0048ErrorWithTextConstant, ctx.Operation.Syntax.GetLocation()));
}
}
}

0 comments on commit 32def37

Please sign in to comment.