Skip to content

Commit

Permalink
Merge pull request #402 from StefanMaron/development
Browse files Browse the repository at this point in the history
LC0039 support for DrillDownPageId and LookupPageId
  • Loading branch information
Arthurvdv authored Dec 5, 2023
2 parents 66c993d + 8e36325 commit 29bedde
Showing 1 changed file with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
using Microsoft.Dynamics.Nav.CodeAnalysis;
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics;
using Microsoft.Dynamics.Nav.CodeAnalysis.Symbols;
using System.Collections;
using System.Collections.Immutable;

namespace BusinessCentral.LinterCop.Design
{
[DiagnosticAnalyzer]
public class Rule0039PageRunTableMismatch : DiagnosticAnalyzer
public class Rule0039ArgumentDifferentTypeThenExpected : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected);
private static readonly List<PropertyKind> referencePageProviders = new List<PropertyKind>
{
PropertyKind.LookupPageId,
PropertyKind.DrillDownPageId
};

public override void Initialize(AnalysisContext context)
{
context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeRunPageArguments), OperationKind.InvocationExpression);
context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeSetRecordArgument), OperationKind.InvocationExpression);
context.RegisterSymbolAction(new Action<SymbolAnalysisContext>(this.AnalyzeTableReferencePageProvider), SymbolKind.Table);
context.RegisterSymbolAction(new Action<SymbolAnalysisContext>(this.AnalyzeTableExtensionReferencePageProvider), SymbolKind.TableExtension);
}

private void AnalyzeRunPageArguments(OperationAnalysisContext ctx)
Expand Down Expand Up @@ -74,6 +82,43 @@ private void AnalyzeSetRecordArgument(OperationAnalysisContext ctx)
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, ctx.Operation.Syntax.GetLocation(), new object[] { 1, operand.GetSymbol().GetTypeSymbol().ToString(), pageSourceTable.GetNavTypeKindSafe() + " \"" + pageSourceTable.Name + "\"" }));
}

private void AnalyzeTableReferencePageProvider(SymbolAnalysisContext ctx)
{
if (ctx.Symbol.IsObsoletePending || ctx.Symbol.IsObsoleteRemoved) return;

ITableTypeSymbol table = (ITableTypeSymbol)ctx.Symbol;
foreach (PropertyKind propertyKind in referencePageProviders)
{
IPropertySymbol pageReference = table.GetProperty(propertyKind);
if (pageReference == null) continue;
IPageTypeSymbol page = (IPageTypeSymbol)pageReference.Value;
ITableTypeSymbol pageSourceTable = page.RelatedTable;
if (pageSourceTable == null) continue;

if (!AreTheSameNavObjects(table, pageSourceTable))
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, pageReference.GetLocation(), new object[] { 1, table.GetTypeSymbol().GetNavTypeKindSafe() + " \"" + table.Name + "\"", pageSourceTable.GetNavTypeKindSafe() + " \"" + pageSourceTable.Name + "\"" }));
}
}

private void AnalyzeTableExtensionReferencePageProvider(SymbolAnalysisContext ctx)
{
if (ctx.Symbol.IsObsoletePending || ctx.Symbol.IsObsoleteRemoved) return;

ITableExtensionTypeSymbol tableExtension = (ITableExtensionTypeSymbol)ctx.Symbol;
ITableTypeSymbol table = (ITableTypeSymbol)tableExtension.Target;
foreach (PropertyKind propertyKind in referencePageProviders)
{
IPropertySymbol pageReference = tableExtension.GetProperty(propertyKind);
if (pageReference == null) continue;
IPageTypeSymbol page = (IPageTypeSymbol)pageReference.Value;
ITableTypeSymbol pageSourceTable = page.RelatedTable;
if (pageSourceTable == null) continue;

if (!AreTheSameNavObjects(table, pageSourceTable))
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, pageReference.GetLocation(), new object[] { 1, table.GetTypeSymbol().GetNavTypeKindSafe() + " \"" + table.Name + "\"", pageSourceTable.GetNavTypeKindSafe() + " \"" + pageSourceTable.Name + "\"" }));
}
}

private static bool AreTheSameNavObjects(ITableTypeSymbol left, ITableTypeSymbol right)
{
if (left.GetNavTypeKindSafe() != right.GetNavTypeKindSafe()) return false;
Expand Down

0 comments on commit 29bedde

Please sign in to comment.