From 6a1a7e6ddc57403b87c7e9bd6cc9cfc7a8c1901a Mon Sep 17 00:00:00 2001 From: Ansgar Barthel Date: Fri, 12 Jul 2024 09:02:05 +0200 Subject: [PATCH] cross-analyse base and extension fields on tables and pages --- ...ule0035ExplicitSetAllowInCustomizations.cs | 61 ++++++++++--------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/BusinessCentral.LinterCop/Design/Rule0035ExplicitSetAllowInCustomizations.cs b/BusinessCentral.LinterCop/Design/Rule0035ExplicitSetAllowInCustomizations.cs index f2a9310f..dc844f25 100644 --- a/BusinessCentral.LinterCop/Design/Rule0035ExplicitSetAllowInCustomizations.cs +++ b/BusinessCentral.LinterCop/Design/Rule0035ExplicitSetAllowInCustomizations.cs @@ -47,8 +47,7 @@ private void AnalyzeAllowInCustomization(SymbolAnalysisContext ctx) // even if no relatedPages exist directly } - NavTypeKind navTypeKind = ctx.Symbol.GetContainingObjectTypeSymbol().GetNavTypeKindSafe(); - ICollection pageFields = GetPageFields(navTypeKind, relatedPages); + ICollection pageFields = GetPageFields(relatedPages); ICollection fieldsNotReferencedOnPage = tableFields.Except(pageFields).ToList(); foreach (IFieldSymbol field in fieldsNotReferencedOnPage) ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0035ExplicitSetAllowInCustomizations, field.Location)); @@ -67,50 +66,54 @@ private static ICollection GetTableFields(ISymbol symbol) } } - private static ICollection GetPageFields(NavTypeKind navTypeKind, IEnumerable relatedPages) + private static ICollection GetPageFields(IEnumerable relatedPages) { ICollection pageFields = new Collection(); - switch (navTypeKind) + foreach (IApplicationObjectTypeSymbol relatedPageLike in relatedPages) { - case NavTypeKind.Record: - foreach (IPageTypeSymbol page in relatedPages.Cast()) - { - IEnumerable fields = page.FlattenedControls.Where(x => x.ControlKind == ControlKind.Field && x.RelatedFieldSymbol != null) - .Select(x => (IFieldSymbol)x.RelatedFieldSymbol.OriginalDefinition); - + switch (relatedPageLike.GetNavTypeKindSafe()) + { + case NavTypeKind.Page: + IEnumerable fields = ((IPageTypeSymbol)relatedPageLike).FlattenedControls.Where(x => x.ControlKind == ControlKind.Field && x.RelatedFieldSymbol != null) + .Select(x => (IFieldSymbol)x.RelatedFieldSymbol.OriginalDefinition); pageFields = pageFields.Union(fields).Distinct().ToList(); - } - return pageFields; - case NavTypeKind.TableExtension: - foreach (IPageExtensionTypeSymbol page in relatedPages.Cast()) - { - IEnumerable fields = page.AddedControlsFlattened.Where(x => x.ControlKind == ControlKind.Field && x.RelatedFieldSymbol != null) - .Select(x => (IFieldSymbol)x.RelatedFieldSymbol.OriginalDefinition); + break; + case NavTypeKind.PageExtension: + IEnumerable extFields = ((IPageExtensionTypeSymbol)relatedPageLike).AddedControlsFlattened.Where(x => x.ControlKind == ControlKind.Field && x.RelatedFieldSymbol != null) + .Select(x => (IFieldSymbol)x.RelatedFieldSymbol.OriginalDefinition); - pageFields = pageFields.Union(fields).Distinct().ToList(); - } - return pageFields; - default: - return pageFields; + pageFields = pageFields.Union(extFields).Distinct().ToList(); + break; + } } + return pageFields; } private static IEnumerable GetRelatedPages(SymbolAnalysisContext ctx) { + // table and tableextension fields can each be referenced on both pages and pageextensions + ITableTypeSymbol table = null; switch (ctx.Symbol.GetContainingObjectTypeSymbol().GetNavTypeKindSafe()) { case NavTypeKind.Record: - return ctx.Compilation.GetDeclaredApplicationObjectSymbols() - .Where(x => x.GetNavTypeKindSafe() == NavTypeKind.Page) - .Where(x => ((IPageTypeSymbol)x.GetTypeSymbol()).PageType != PageTypeKind.API) - .Where(x => ((IPageTypeSymbol)x.GetTypeSymbol()).RelatedTable == (ITableTypeSymbol)ctx.Symbol); + table = (ITableTypeSymbol)ctx.Symbol; + break; case NavTypeKind.TableExtension: - return ctx.Compilation.GetDeclaredApplicationObjectSymbols() - .Where(x => x.GetNavTypeKindSafe() == NavTypeKind.PageExtension) - .Where(x => ((IPageTypeSymbol)((IApplicationObjectExtensionTypeSymbol)x).Target.GetTypeSymbol()).RelatedTable == ((IApplicationObjectExtensionTypeSymbol)ctx.Symbol).Target); + table = (ITableTypeSymbol)((IApplicationObjectExtensionTypeSymbol)ctx.Symbol).Target; + break; default: return null; } + IEnumerable pages = ctx.Compilation.GetDeclaredApplicationObjectSymbols() + .Where(x => x.GetNavTypeKindSafe() == NavTypeKind.Page) + .Where(x => ((IPageTypeSymbol)x.GetTypeSymbol()).PageType != PageTypeKind.API) + .Where(x => ((IPageTypeSymbol)x.GetTypeSymbol()).RelatedTable == table); + + IEnumerable pageExtensions = ctx.Compilation.GetDeclaredApplicationObjectSymbols() + .Where(x => x.GetNavTypeKindSafe() == NavTypeKind.PageExtension) + .Where(x => ((IPageTypeSymbol)((IApplicationObjectExtensionTypeSymbol)x).Target.GetTypeSymbol()).RelatedTable == table); + + return pages.Union(pageExtensions); } private static bool IsSupportedType(NavTypeKind navTypeKind)