Skip to content

Commit

Permalink
Init new rule 0056 and 0057
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthurvdv committed Apr 6, 2024
1 parent b461308 commit 045e9c7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Design/Rule0056AccessibilityEnumValueWithCaption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.Dynamics.Nav.CodeAnalysis;
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics;
using Microsoft.Dynamics.Nav.CodeAnalysis.Syntax;
using System.Collections.Immutable;

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

public override void Initialize(AnalysisContext context) => context.RegisterSyntaxNodeAction(new Action<SyntaxNodeAnalysisContext>(this.AnalyzeEnumWithCaption), SyntaxKind.EnumValue);

private void AnalyzeEnumWithCaption(SyntaxNodeAnalysisContext ctx)
{
// Prevent possible duplicate diagnostic
if (ctx.ContainingSymbol.ContainingType is null) return;

if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return;
if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return;

EnumValueSyntax enumValue = ctx.Node as EnumValueSyntax;
if (enumValue == null) return;
LabelPropertyValueSyntax captionProperty = ctx.Node?.GetProperty("Caption")?.Value as LabelPropertyValueSyntax;
if (captionProperty == null) return;

if (enumValue.GetNameStringValue() == "" && captionProperty.Value.LabelText.Value.Value.ToString() != "")
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0056EmptyEnumValueWithCaption, ctx.Node.GetLocation()));

if (captionProperty.Value.Properties?.Values.Where(prop => prop.Identifier.Text.ToLowerInvariant() == "locked").FirstOrDefault() != null) return;

if (enumValue.GetNameStringValue() != "" && captionProperty.Value.LabelText.Value.Value.ToString() == "")
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0057EnumValueWithEmptyCaption, ctx.Node.GetLocation()));
}
}
}
10 changes: 10 additions & 0 deletions LinterCop.ruleset.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@
"id": "LC0055",
"action": "Info",
"justification": "The suffix 'Tok' is meant to be used when the value of the label matches the name."
},
{
"id": "LC0056",
"action": "Info",
"justification": "Empty Enum values should not have a specified Caption property."
},
{
"id": "LC0057",
"action": "Info",
"justification": "Enum values must have non-empty a Caption to be selectable in the client."
}
]
}
2 changes: 2 additions & 0 deletions LinterCopAnalyzers.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@ public static class DiagnosticDescriptors
public static readonly DiagnosticDescriptor Rule0052InternalProceduresNotReferencedAnalyzerDescriptor = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0052", (LocalizableString)new LocalizableResourceString("Rule0052InternalProceduresNotReferencedAnalyzer", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0052InternalProceduresNotReferencedAnalyzerFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, isEnabledByDefault: true, (LocalizableString)new LocalizableResourceString("Rule0052InternalProceduresNotReferencedAnalyzerDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0052");
public static readonly DiagnosticDescriptor Rule0053InternalProcedureOnlyUsedInCurrentObjectAnalyzerDescriptor = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0053", (LocalizableString)new LocalizableResourceString("Rule0053InternalProcedureOnlyUsedInCurrentObjectAnalyzer", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0053InternalProcedureOnlyUsedInCurrentObjectAnalyzerFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, isEnabledByDefault: true, (LocalizableString)new LocalizableResourceString("Rule0053InternalProcedureOnlyUsedInCurrentObjectAnalyzerDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0053");
public static readonly DiagnosticDescriptor Rule0055TokSuffixForTokenLabels = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0055", (LocalizableString)new LocalizableResourceString("Rule0055TokSuffixForTokenLabelsTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0055TokSuffixForTokenLabelsFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0055TokSuffixForTokenLabelsDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0055");
public static readonly DiagnosticDescriptor Rule0056EmptyEnumValueWithCaption = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0056", (LocalizableString)new LocalizableResourceString("Rule0056EmptyEnumValueWithCaptionTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0056EmptyEnumValueWithCaptionFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0056EmptyEnumValueWithCaptionDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0056");
public static readonly DiagnosticDescriptor Rule0057EnumValueWithEmptyCaption = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0057", (LocalizableString)new LocalizableResourceString("Rule0057EnumValueWithEmptyCaptionTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0057EnumValueWithEmptyCaptionFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0057EnumValueWithEmptyCaptionDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0057");
}
}
18 changes: 18 additions & 0 deletions LinterCopAnalyzers.resx
Original file line number Diff line number Diff line change
Expand Up @@ -600,4 +600,22 @@
<data name="Rule0055TokSuffixForTokenLabelsDescription" xml:space="preserve">
<value>The suffix 'Tok' is meant to be used when the value of the label matches the name.</value>
</data>
<data name="Rule0056EmptyEnumValueWithCaptionTitle" xml:space="preserve">
<value>Empty Enum values should not have a specified Caption property.</value>
</data>
<data name="Rule0056EmptyEnumValueWithCaptionFormat" xml:space="preserve">
<value>Empty Enum values should not have a specified Caption property.</value>
</data>
<data name="Rule0056EmptyEnumValueWithCaptionDescription" xml:space="preserve">
<value>Empty Enum values should not have a specified Caption property.</value>
</data>
<data name="Rule0057EnumValueWithEmptyCaptionTitle" xml:space="preserve">
<value>Enum values must have non-empty a Caption to be selectable in the client.</value>
</data>
<data name="Rule0057EnumValueWithEmptyCaptionFormat" xml:space="preserve">
<value>Enum values must have non-empty a Caption to be selectable in the client.</value>
</data>
<data name="Rule0057EnumValueWithEmptyCaptionDescription" xml:space="preserve">
<value>Enum values must have non-empty a Caption to be selectable in the client.</value>
</data>
</root>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ Further note that you should have BcContainerHelper version 2.0.16 (or newer) in
|[LC0052](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0052)|The internal procedure is declared but never used.|Info|
|[LC0053](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0053)|The internal procedure is only used in the object in which it is declared. Consider making the procedure local.|Info|
|[LC0055](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0055)|The suffix `Tok` is meant to be used when the value of the label matches the name.|Info|
|[LC0056](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0056)|Empty Enum values should not have a specified `Caption` property.|Info|
|[LC0057](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0057)|Enum values must have non-empty a `Caption` to be selectable in the client|Info|

## Configuration

Expand Down

0 comments on commit 045e9c7

Please sign in to comment.