Skip to content

Commit

Permalink
Merge pull request StefanMaron#675 from StefanMaron/TableFieldToolTips
Browse files Browse the repository at this point in the history
Add Rule0064 Use table field ToolTip instead of page field ToolTip
  • Loading branch information
Arthurvdv authored Jul 6, 2024
2 parents 9391bef + db52ce1 commit 332f4c9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#if Spring2024OrGreater
using BusinessCentral.LinterCop.AnalysisContextExtension;
using Microsoft.Dynamics.Nav.CodeAnalysis;
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;

namespace BusinessCentral.LinterCop.Design
{
[DiagnosticAnalyzer]
public class Rule0064UseTableFieldToolTipInsteadOfPageFieldToolTip : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0064UseTableFieldToolTipInsteadOfPageFieldToolTip);

public override void Initialize(AnalysisContext context) => context.RegisterSymbolAction(new Action<SymbolAnalysisContext>(this.AnalyzeFlowFieldEditable), SymbolKind.Page, SymbolKind.PageExtension);

private void AnalyzeFlowFieldEditable(SymbolAnalysisContext ctx)
{
if (!VersionChecker.IsSupported(ctx.Symbol, VersionCompatibility.Spring2024OrGreater)) return;

if (ctx.IsObsoletePendingOrRemoved()) return;

IEnumerable<IControlSymbol> pageFields = GetFlattenedControls(ctx.Symbol)
.Where(e => e.ControlKind == ControlKind.Field)
.Where(e => e.GetProperty(PropertyKind.ToolTip) != null)
.Where(e => e.RelatedFieldSymbol != null);
if (pageFields == null) return;

foreach (IControlSymbol page in pageFields)
{
ctx.CancellationToken.ThrowIfCancellationRequested();

IPropertySymbol pageToolTip = page.GetProperty(PropertyKind.ToolTip);
IPropertySymbol tableToolTip = page.RelatedFieldSymbol.GetProperty(PropertyKind.ToolTip);

// Page field has a value for the ToolTip property and table field does not have a value for the ToolTip property
if (tableToolTip == null && page.RelatedFieldSymbol.IsSourceSymbol())
{
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0064UseTableFieldToolTipInsteadOfPageFieldToolTip, pageToolTip.GetLocation()));
continue;
}

// Page field has a value for the ToolTip property and table field also has a value for the ToolTip property but the value is exactly the same
if (tableToolTip != null && pageToolTip.ValueText == tableToolTip.ValueText)
{
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0064UseTableFieldToolTipInsteadOfPageFieldToolTip, pageToolTip.GetLocation()));
continue;
}
}
}

private static IEnumerable<IControlSymbol> GetFlattenedControls(ISymbol symbol)
{
switch (symbol.Kind)
{
case SymbolKind.Page:
return ((IPageBaseTypeSymbol)symbol).FlattenedControls;
case SymbolKind.PageExtension:
return ((IPageExtensionBaseTypeSymbol)symbol).AddedControlsFlattened;
default:
return null;
}
}
}
}
#endif
5 changes: 5 additions & 0 deletions BusinessCentral.LinterCop/LinterCop.ruleset.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@
"id": "LC0063",
"action": "Info",
"justification": "Consider naming field with a more descriptive name."
},
{
"id": "LC0064",
"action": "Info",
"justification": "Use table field ToolTip instead of page field ToolTip."
}
]
}
1 change: 1 addition & 0 deletions BusinessCentral.LinterCop/LinterCopAnalyzers.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ public static class DiagnosticDescriptors
public static readonly DiagnosticDescriptor Rule0061SetODataKeyFieldsWithSystemIdField = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0061", (LocalizableString)new LocalizableResourceString("Rule0061SetODataKeyFieldsWithSystemIdFieldTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0061SetODataKeyFieldsWithSystemIdFieldFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0061SetODataKeyFieldsWithSystemIdFieldDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0061");
public static readonly DiagnosticDescriptor Rule0062MandatoryFieldMissingOnApiPage = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0062", (LocalizableString)new LocalizableResourceString("Rule0062MandatoryFieldMissingOnApiPageTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0062MandatoryFieldMissingOnApiPageFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0062MandatoryFieldMissingOnApiPageDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0062");
public static readonly DiagnosticDescriptor Rule0063GiveFieldMoreDescriptiveName = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0063", (LocalizableString)new LocalizableResourceString("Rule0063GiveFieldMoreDescriptiveNameTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0063GiveFieldMoreDescriptiveNameFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0063GiveFieldMoreDescriptiveNameDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0063");
public static readonly DiagnosticDescriptor Rule0064UseTableFieldToolTipInsteadOfPageFieldToolTip = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0064", (LocalizableString)new LocalizableResourceString("Rule0064UseTableFieldToolTipInsteadOfPageFieldToolTipTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0064UseTableFieldToolTipInsteadOfPageFieldToolTipFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0064UseTableFieldToolTipInsteadOfPageFieldToolTipDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0064");
}
}
9 changes: 9 additions & 0 deletions BusinessCentral.LinterCop/LinterCopAnalyzers.resx
Original file line number Diff line number Diff line change
Expand Up @@ -681,4 +681,13 @@
<data name="Rule0063GiveFieldMoreDescriptiveNameDescription" xml:space="preserve">
<value>Consider naming field with a more descriptive name: '{0}'.</value>
</data>
<data name="Rule0064UseTableFieldToolTipInsteadOfPageFieldToolTipTitle" xml:space="preserve">
<value>Use table field ToolTip instead of page field ToolTip.</value>
</data>
<data name="Rule0064UseTableFieldToolTipInsteadOfPageFieldToolTipFormat" xml:space="preserve">
<value>Use table field ToolTip instead of page field ToolTip.</value>
</data>
<data name="Rule0064UseTableFieldToolTipInsteadOfPageFieldToolTipDescription" xml:space="preserve">
<value>Use table field ToolTip instead of page field ToolTip.</value>
</data>
</root>
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,5 @@ For an example and the default values see: [LinterCop.ruleset.json](LinterCop.ru
|[LC0060](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0060)|The `ApplicationArea` property is not applicable to API pages.|Info|
|[LC0061](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0061)|Pages of type API must have the `ODataKeyFields` property set to the SystemId field.|Info|
|[LC0062](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0062)|Mandatory field is missing on API page.|Info|
|[LC0063](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0063)|Consider naming field with a more descriptive name.|Info|
|[LC0063](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0063)|Consider naming field with a more descriptive name.|Info|
|[LC0064](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0064)|Use table field ToolTip instead of page field ToolTip.|Info|

0 comments on commit 332f4c9

Please sign in to comment.