Skip to content

Commit

Permalink
Remove empty line between documentation comment and declaration (RCS1…
Browse files Browse the repository at this point in the history
…036)
  • Loading branch information
josefpihrt committed Feb 2, 2020
1 parent c3f7d19 commit de0f120
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/Analyzers/CSharp/Analysis/RemoveRedundantEmptyLineAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public override void Initialize(AnalysisContext context)
context.RegisterSyntaxNodeAction(AnalyzeFixedStatement, SyntaxKind.FixedStatement);

context.RegisterSyntaxNodeAction(AnalyzeAccessorList, SyntaxKind.AccessorList);

context.RegisterSyntaxNodeAction(AnalyzeBlock, SyntaxKind.Block);
context.RegisterSyntaxNodeAction(AnalyzeSingleLineDocumentationCommentTrivia, SyntaxKind.SingleLineDocumentationCommentTrivia);

context.RegisterSyntaxNodeAction(AnalyzeInitializer, SyntaxKind.ArrayInitializerExpression);
context.RegisterSyntaxNodeAction(AnalyzeInitializer, SyntaxKind.CollectionInitializerExpression);
context.RegisterSyntaxNodeAction(AnalyzeInitializer, SyntaxKind.ObjectInitializerExpression);
}

public static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
{
var classDeclaration = (ClassDeclarationSyntax)context.Node;

Expand All @@ -64,7 +64,7 @@ public static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
classDeclaration.CloseBraceToken);
}

public static void AnalyzeStructDeclaration(SyntaxNodeAnalysisContext context)
private static void AnalyzeStructDeclaration(SyntaxNodeAnalysisContext context)
{
var structDeclaration = (StructDeclarationSyntax)context.Node;

Expand All @@ -75,7 +75,7 @@ public static void AnalyzeStructDeclaration(SyntaxNodeAnalysisContext context)
structDeclaration.CloseBraceToken);
}

public static void AnalyzeInterfaceDeclaration(SyntaxNodeAnalysisContext context)
private static void AnalyzeInterfaceDeclaration(SyntaxNodeAnalysisContext context)
{
var interfaceDeclaration = (InterfaceDeclarationSyntax)context.Node;

Expand All @@ -86,7 +86,7 @@ public static void AnalyzeInterfaceDeclaration(SyntaxNodeAnalysisContext context
interfaceDeclaration.CloseBraceToken);
}

public static void AnalyzeNamespaceDeclaration(SyntaxNodeAnalysisContext context)
private static void AnalyzeNamespaceDeclaration(SyntaxNodeAnalysisContext context)
{
var namespaceDeclaration = (NamespaceDeclarationSyntax)context.Node;

Expand Down Expand Up @@ -115,7 +115,7 @@ public static void AnalyzeNamespaceDeclaration(SyntaxNodeAnalysisContext context
AnalyzeEnd(context, members.Last(), namespaceDeclaration.CloseBraceToken);
}

public static void AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context)
private static void AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context)
{
var switchStatement = (SwitchStatementSyntax)context.Node;

Expand All @@ -128,7 +128,7 @@ public static void AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context)
}
}

public static void AnalyzeTryStatement(SyntaxNodeAnalysisContext context)
private static void AnalyzeTryStatement(SyntaxNodeAnalysisContext context)
{
var tryStatement = (TryStatementSyntax)context.Node;

Expand Down Expand Up @@ -157,7 +157,7 @@ public static void AnalyzeTryStatement(SyntaxNodeAnalysisContext context)
}
}

public static void AnalyzeElseClause(SyntaxNodeAnalysisContext context)
private static void AnalyzeElseClause(SyntaxNodeAnalysisContext context)
{
var elseClause = (ElseClauseSyntax)context.Node;

Expand Down Expand Up @@ -344,7 +344,7 @@ private static void AnalyzeDeclaration(
}
}

public static void AnalyzeBlock(SyntaxNodeAnalysisContext context)
private static void AnalyzeBlock(SyntaxNodeAnalysisContext context)
{
var block = (BlockSyntax)context.Node;

Expand All @@ -361,7 +361,29 @@ public static void AnalyzeBlock(SyntaxNodeAnalysisContext context)
}
}

public static void AnalyzeInitializer(SyntaxNodeAnalysisContext context)
private static void AnalyzeSingleLineDocumentationCommentTrivia(SyntaxNodeAnalysisContext context)
{
var comment = (DocumentationCommentTriviaSyntax)context.Node;

if (comment is IStructuredTriviaSyntax structuredTrivia)
{
SyntaxTrivia trivia = structuredTrivia.ParentTrivia;
SyntaxTriviaList leadingTrivia = trivia.Token.LeadingTrivia;

int index = leadingTrivia.IndexOf(trivia);

if (index >= 0
&& index < leadingTrivia.Count - 1
&& leadingTrivia[index + 1].IsEndOfLineTrivia())
{
DiagnosticHelpers.ReportDiagnostic(context,
DiagnosticDescriptors.RemoveRedundantEmptyLine,
leadingTrivia[index + 1].GetLocation());
}
}
}

private static void AnalyzeInitializer(SyntaxNodeAnalysisContext context)
{
var initializer = (InitializerExpressionSyntax)context.Node;

Expand Down Expand Up @@ -417,7 +439,7 @@ bool IsExpectedTrailingTrivia(SyntaxTriviaList triviaList)
}
}

public static void AnalyzeAccessorList(SyntaxNodeAnalysisContext context)
private static void AnalyzeAccessorList(SyntaxNodeAnalysisContext context)
{
var accessorList = (AccessorListSyntax)context.Node;

Expand Down
23 changes: 23 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1036RemoveRedundantEmptyLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,29 @@ void M()
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveRedundantEmptyLine)]
public async Task Test_EmptyLineAfterDocComment()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
/// <summary></summary>
[|
|] void M()
{
}
}
", @"
class C
{
/// <summary></summary>
void M()
{
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveRedundantEmptyLine)]
public async Task TestNoDiagnostic_ObjectInitializer()
{
Expand Down

0 comments on commit de0f120

Please sign in to comment.