Skip to content

Commit

Permalink
Fix empty line before #endregion (RCS0005) (#1114)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Jul 18, 2023
1 parent f23a674 commit d102391
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix [RCS1146](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1146.md) ([#1098](https://github.com/JosefPihrt/Roslynator/pull/1098)).
- Fix [RCS1154](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1154.md) ([#1105](https://github.com/JosefPihrt/Roslynator/pull/1105)).
- Fix [RCS1211](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1211.md) ([#1095](https://github.com/JosefPihrt/Roslynator/pull/1095)).
- Fix [RCS0005](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS0005.md) ([#1114](https://github.com/JosefPihrt/Roslynator/pull/1114)).

## [4.3.0] - 2023-04-24

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ private static void AnalyzeEndRegionDirectiveTrivia(SyntaxNodeAnalysisContext co
{
var endRegionDirective = (EndRegionDirectiveTriviaSyntax)context.Node;

if (IsPrecededWithEmptyLineOrRegionDirective())
return;

DiagnosticHelpers.ReportDiagnostic(
context,
DiagnosticRules.AddBlankLineBeforeEndRegionDirective,
Location.Create(endRegionDirective.SyntaxTree, endRegionDirective.Span.WithLength(0)));
if (!IsPrecededWithEmptyLineOrRegionDirective(endRegionDirective))
{
DiagnosticHelpers.ReportDiagnostic(
context,
DiagnosticRules.AddBlankLineBeforeEndRegionDirective,
Location.Create(endRegionDirective.SyntaxTree, endRegionDirective.Span.WithLength(0)));
}

bool IsPrecededWithEmptyLineOrRegionDirective()
static bool IsPrecededWithEmptyLineOrRegionDirective(EndRegionDirectiveTriviaSyntax endRegionDirective)
{
SyntaxTrivia parentTrivia = endRegionDirective.ParentTrivia;

Expand Down Expand Up @@ -78,7 +78,10 @@ bool IsPrecededWithEmptyLineOrRegionDirective()
return false;
}

return en.Current.IsEndOfLineTrivia();
return en.Current.IsKind(
SyntaxKind.EndOfLineTrivia,
SyntaxKind.RegionDirectiveTrivia,
SyntaxKind.EndRegionDirectiveTrivia);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,39 @@ void M()
#endregion
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddBlankLineBeforeEndRegionDirective)]
public async Task TestNoDiagnostic_TwoEndRegions()
{
await VerifyNoDiagnosticAsync(@"
#region
class C
{
#region
void M()
{
}
#endregion
#endregion
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddBlankLineBeforeEndRegionDirective)]
public async Task TestNoDiagnostic_EmptyRegions()
{
await VerifyNoDiagnosticAsync(@"
#region
#region
#endregion
#endregion
");
}
}

0 comments on commit d102391

Please sign in to comment.