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

Add Ternary operator to Rule0009/0010 #959

Merged
merged 2 commits into from
Feb 20, 2025
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
34 changes: 19 additions & 15 deletions BusinessCentral.LinterCop/Design/Rule0009CodeMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,43 @@ public class Rule0009CodeMetrics : DiagnosticAnalyzer
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(DiagnosticDescriptors.Rule0009CodeMetricsInfo, DiagnosticDescriptors.Rule0010CodeMetricsWarning);

private static readonly HashSet<string> eventPublisherDecoratorNames = new(StringComparer.OrdinalIgnoreCase)
{
"BusinessEvent",
"IntegrationEvent",
"ExternalBusinessEvent"
};

private static readonly HashSet<SyntaxKind> OperatorAndOperandKinds =
Enum.GetValues(typeof(SyntaxKind))
.Cast<SyntaxKind>()
.Where(value =>
(value.ToString().Contains("Keyword") ||
value.ToString().Contains("Token")) ||
IsOperandKind(value))
.Where(value => value.ToString().Contains("Keyword") ||
value.ToString().Contains("Token") ||
IsOperandKind(value))
.ToHashSet();

public override void Initialize(AnalysisContext context) =>
context.RegisterCodeBlockAction(new Action<CodeBlockAnalysisContext>(this.CheckCodeMetrics));

private void CheckCodeMetrics(CodeBlockAnalysisContext context)
{
if ((context.CodeBlock.Kind != SyntaxKind.MethodDeclaration) &&
(context.CodeBlock.Kind != SyntaxKind.TriggerDeclaration))
if (context.IsObsoletePendingOrRemoved() || context.CodeBlock is not MethodOrTriggerDeclarationSyntax methodOrTrigger)
return;

if (context.IsObsoletePendingOrRemoved()) return;

var containingObjectTypeSymbol = context.OwningSymbol.GetContainingObjectTypeSymbol();
if (containingObjectTypeSymbol.NavTypeKind == NavTypeKind.Interface ||
containingObjectTypeSymbol.NavTypeKind == NavTypeKind.ControlAddIn)
return;

SyntaxNode bodyNode = context.CodeBlock.Kind == SyntaxKind.MethodDeclaration
? (context.CodeBlock as MethodDeclarationSyntax)?.Body
: (context.CodeBlock as TriggerDeclarationSyntax)?.Body;

if (bodyNode is null)
if (methodOrTrigger.Body is null ||
methodOrTrigger.Body.Statements.Count == 0 &&
methodOrTrigger.Attributes.Any(attr => eventPublisherDecoratorNames.Contains(attr.GetIdentifierOrLiteralValue() ?? string.Empty)))
return;

var descendants = bodyNode.DescendantNodesAndTokens(e => true).ToArray();
var descendants = methodOrTrigger.Body.DescendantNodesAndTokens(e => true).ToArray();

int cyclomaticComplexity = GetCyclomaticComplexity(descendants);
double HalsteadVolume = GetHalsteadVolume(context, bodyNode, descendants, cyclomaticComplexity);
double HalsteadVolume = GetHalsteadVolume(context, methodOrTrigger.Body, descendants, cyclomaticComplexity);

if (LinterSettings.instance is null)
LinterSettings.Create(context.SemanticModel.Compilation.FileSystem.GetDirectoryPath());
Expand Down Expand Up @@ -130,6 +131,9 @@ private static bool IsComplexKind(SyntaxKind kind)
case SyntaxKind.ForEachKeyword:
case SyntaxKind.WhileKeyword:
case SyntaxKind.UntilKeyword:
#if !LessThenFall2024
case SyntaxKind.ConditionalExpression: // Ternary operator
#endif
return true;
}

Expand Down