|
| 1 | +// © 2025 Koninklijke Philips N.V. See License.md in the project root for license information. |
| 2 | + |
| 3 | +using System.Collections.Immutable; |
| 4 | +using System.Linq; |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using Microsoft.CodeAnalysis.CSharp; |
| 7 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 8 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 9 | +using Microsoft.CodeAnalysis.Text; |
| 10 | +using Philips.CodeAnalysis.Common; |
| 11 | + |
| 12 | +namespace Philips.CodeAnalysis.MaintainabilityAnalyzers.Readability |
| 13 | +{ |
| 14 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 15 | + public class AvoidEmptyRegionsAnalyzer : DiagnosticAnalyzerBase |
| 16 | + { |
| 17 | + private const string AvoidEmptyRegionTitle = @"Avoid empty regions"; |
| 18 | + public const string AvoidEmptyRegionMessageFormat = @"Remove the empty region"; |
| 19 | + private const string AvoidEmptyRegionDescription = @"Remove the empty region"; |
| 20 | + private const string AvoidEmptyRegionCategory = Categories.Readability; |
| 21 | + |
| 22 | + private static readonly DiagnosticDescriptor AvoidEmpty = new(DiagnosticId.AvoidEmptyRegions.ToId(), AvoidEmptyRegionTitle, |
| 23 | + AvoidEmptyRegionMessageFormat, AvoidEmptyRegionCategory, DiagnosticSeverity.Error, isEnabledByDefault: true, description: AvoidEmptyRegionDescription); |
| 24 | + |
| 25 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(AvoidEmpty); |
| 26 | + |
| 27 | + protected override void InitializeCompilation(CompilationStartAnalysisContext context) |
| 28 | + { |
| 29 | + context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.RegionDirectiveTrivia); |
| 30 | + } |
| 31 | + |
| 32 | + private static void Analyze(SyntaxNodeAnalysisContext context) |
| 33 | + { |
| 34 | + var regionDirective = (RegionDirectiveTriviaSyntax)context.Node; |
| 35 | + |
| 36 | + // Find the matching #endregion |
| 37 | + EndRegionDirectiveTriviaSyntax endRegionDirective = FindMatchingEndRegion(regionDirective); |
| 38 | + if (endRegionDirective == null) |
| 39 | + { |
| 40 | + return; // No matching end region found |
| 41 | + } |
| 42 | + |
| 43 | + // Check if the region is empty (contains only whitespace and comments) |
| 44 | + if (IsRegionEmpty(regionDirective, endRegionDirective, context.SemanticModel.SyntaxTree.GetText())) |
| 45 | + { |
| 46 | + var diagnostic = Diagnostic.Create(AvoidEmpty, regionDirective.GetLocation()); |
| 47 | + context.ReportDiagnostic(diagnostic); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static EndRegionDirectiveTriviaSyntax FindMatchingEndRegion(RegionDirectiveTriviaSyntax regionStart) |
| 52 | + { |
| 53 | + SyntaxTree syntaxTree = regionStart.SyntaxTree; |
| 54 | + SyntaxNode root = syntaxTree.GetRoot(); |
| 55 | + |
| 56 | + var regionDepth = 1; |
| 57 | + var startPosition = regionStart.SpanStart; |
| 58 | + |
| 59 | + // Find all region and endregion directives after the current region |
| 60 | + IOrderedEnumerable<SyntaxTrivia> allDirectives = root.DescendantTrivia(descendIntoTrivia: true) |
| 61 | + .Where(trivia => trivia.SpanStart > startPosition && |
| 62 | + (trivia.IsKind(SyntaxKind.RegionDirectiveTrivia) || |
| 63 | + trivia.IsKind(SyntaxKind.EndRegionDirectiveTrivia))) |
| 64 | + .OrderBy(trivia => trivia.SpanStart); |
| 65 | + |
| 66 | + foreach (SyntaxTrivia trivia in allDirectives) |
| 67 | + { |
| 68 | + if (trivia.IsKind(SyntaxKind.RegionDirectiveTrivia)) |
| 69 | + { |
| 70 | + regionDepth++; |
| 71 | + } |
| 72 | + else if (trivia.IsKind(SyntaxKind.EndRegionDirectiveTrivia)) |
| 73 | + { |
| 74 | + regionDepth--; |
| 75 | + if (regionDepth == 0) |
| 76 | + { |
| 77 | + return (EndRegionDirectiveTriviaSyntax)trivia.GetStructure(); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + private static bool IsRegionEmpty(RegionDirectiveTriviaSyntax regionStart, EndRegionDirectiveTriviaSyntax regionEnd, SourceText sourceText) |
| 86 | + { |
| 87 | + var regionStartLine = regionStart.GetLocation().GetLineSpan().StartLinePosition.Line; |
| 88 | + var regionEndLine = regionEnd.GetLocation().GetLineSpan().StartLinePosition.Line; |
| 89 | + |
| 90 | + // Check each line between the region start and end |
| 91 | + for (var lineNumber = regionStartLine + 1; lineNumber < regionEndLine; lineNumber++) |
| 92 | + { |
| 93 | + TextLine line = sourceText.Lines[lineNumber]; |
| 94 | + var lineText = line.ToString().Trim(); |
| 95 | + |
| 96 | + // If we find any non-empty, non-comment line, the region is not empty |
| 97 | + if (!string.IsNullOrEmpty(lineText) && !lineText.StartsWith("//")) |
| 98 | + { |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return true; // Region contains only whitespace and/or comments |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments