Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LC0035 - Cross analyse base and extension fields for pages and tables #698

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ private void AnalyzeAllowInCustomization(SymbolAnalysisContext ctx)
// even if no relatedPages exist directly
}

NavTypeKind navTypeKind = ctx.Symbol.GetContainingObjectTypeSymbol().GetNavTypeKindSafe();
ICollection<IFieldSymbol> pageFields = GetPageFields(navTypeKind, relatedPages);
ICollection<IFieldSymbol> pageFields = GetPageFields(relatedPages);
ICollection<IFieldSymbol> fieldsNotReferencedOnPage = tableFields.Except(pageFields).ToList();
foreach (IFieldSymbol field in fieldsNotReferencedOnPage)
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0035ExplicitSetAllowInCustomizations, field.Location));
Expand All @@ -67,50 +66,54 @@ private static ICollection<IFieldSymbol> GetTableFields(ISymbol symbol)
}
}

private static ICollection<IFieldSymbol> GetPageFields(NavTypeKind navTypeKind, IEnumerable<IApplicationObjectTypeSymbol> relatedPages)
private static ICollection<IFieldSymbol> GetPageFields(IEnumerable<IApplicationObjectTypeSymbol> relatedPages)
{
ICollection<IFieldSymbol> pageFields = new Collection<IFieldSymbol>();
switch (navTypeKind)
foreach (IApplicationObjectTypeSymbol relatedPageLike in relatedPages)
{
case NavTypeKind.Record:
foreach (IPageTypeSymbol page in relatedPages.Cast<IPageTypeSymbol>())
{
IEnumerable<IFieldSymbol> 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<IFieldSymbol> 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<IPageExtensionTypeSymbol>())
{
IEnumerable<IFieldSymbol> fields = page.AddedControlsFlattened.Where(x => x.ControlKind == ControlKind.Field && x.RelatedFieldSymbol != null)
.Select(x => (IFieldSymbol)x.RelatedFieldSymbol.OriginalDefinition);
break;
case NavTypeKind.PageExtension:
IEnumerable<IFieldSymbol> 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<IApplicationObjectTypeSymbol> 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<IApplicationObjectTypeSymbol> 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<IApplicationObjectTypeSymbol> 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)
Expand Down