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

SA1114: Allow all valid trivia #3259

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ namespace StyleCop.Analyzers.ReadabilityRules
{
using System;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;

/// <summary>
Expand Down Expand Up @@ -195,22 +193,8 @@ private static void AnalyzeRankSpecifiers(SyntaxNodeAnalysisContext context, Arr
return;
}

var firstSize = arrayRankSpecifierSyntax.Sizes[0];

var firstSizeLineSpan = firstSize.GetLineSpan();
if (!firstSizeLineSpan.IsValid)
{
return;
}

var openBracketLineSpan = openBracketToken.GetLineSpan();
if (!openBracketLineSpan.IsValid)
{
return;
}

if (openBracketLineSpan.EndLinePosition.Line != firstSizeLineSpan.StartLinePosition.Line &&
openBracketLineSpan.EndLinePosition.Line != (firstSizeLineSpan.StartLinePosition.Line - 1))
var firstSize = arrayRankSpecifierSyntax.Sizes.First();
if (SpanMultipleLines(openBracketToken, firstSize))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, firstSize.GetLocation()));
}
Expand All @@ -227,22 +211,8 @@ private static void AnalyzeAttributeList(SyntaxNodeAnalysisContext context, Attr
return;
}

var firstAttribute = attributesList.Attributes[0];

var firstAttributeLineSpan = firstAttribute.GetLineSpan();
if (!firstAttributeLineSpan.IsValid)
{
return;
}

var openBracketLineSpan = openBracketToken.GetLineSpan();
if (!openBracketLineSpan.IsValid)
{
return;
}

if (openBracketLineSpan.EndLinePosition.Line != firstAttributeLineSpan.StartLinePosition.Line &&
openBracketLineSpan.EndLinePosition.Line != (firstAttributeLineSpan.StartLinePosition.Line - 1))
var firstAttribute = attributesList.Attributes.First();
if (SpanMultipleLines(openBracketToken, firstAttribute))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, firstAttribute.GetLocation()));
}
Expand All @@ -258,26 +228,8 @@ private static void AnalyzeArgumentList(SyntaxNodeAnalysisContext context, Brack
return;
}

var firstArgument = argumentListSyntax.Arguments[0];
if (firstArgument.GetLeadingTrivia().Any(SyntaxKind.PragmaWarningDirectiveTrivia))
{
return;
}

var firstArgumentLineSpan = firstArgument.GetLineSpan();
if (!firstArgumentLineSpan.IsValid)
{
return;
}

var openBracketLineSpan = openBracketToken.GetLineSpan();
if (!openBracketLineSpan.IsValid)
{
return;
}

if (openBracketLineSpan.EndLinePosition.Line != firstArgumentLineSpan.StartLinePosition.Line &&
openBracketLineSpan.EndLinePosition.Line != (firstArgumentLineSpan.StartLinePosition.Line - 1))
var firstArgument = argumentListSyntax.Arguments.First();
if (SpanMultipleLines(openBracketToken, firstArgument))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, firstArgument.GetLocation()));
}
Expand All @@ -293,26 +245,8 @@ private static void AnalyzeArgumentList(SyntaxNodeAnalysisContext context, Attri
return;
}

var firstArgument = argumentListSyntax.Arguments[0];
if (firstArgument.GetLeadingTrivia().Any(SyntaxKind.PragmaWarningDirectiveTrivia))
{
return;
}

var firstArgumentLineSpan = firstArgument.GetLineSpan();
if (!firstArgumentLineSpan.IsValid)
{
return;
}

var openParenLineSpan = argumentListSyntax.OpenParenToken.GetLineSpan();
if (!openParenLineSpan.IsValid)
{
return;
}

if (openParenLineSpan.EndLinePosition.Line != firstArgumentLineSpan.StartLinePosition.Line &&
openParenLineSpan.EndLinePosition.Line != (firstArgumentLineSpan.StartLinePosition.Line - 1))
var firstArgument = argumentListSyntax.Arguments.First();
if (SpanMultipleLines(argumentListSyntax.OpenParenToken, firstArgument))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, firstArgument.GetLocation()));
}
Expand All @@ -328,26 +262,8 @@ private static void AnalyzeArgumentList(SyntaxNodeAnalysisContext context, Argum
return;
}

var firstArgument = argumentListSyntax.Arguments[0];
if (firstArgument.GetLeadingTrivia().Any(SyntaxKind.PragmaWarningDirectiveTrivia))
{
return;
}

var firstArgumentLineSpan = firstArgument.GetLineSpan();
if (!firstArgumentLineSpan.IsValid)
{
return;
}

var openParenLineSpan = openParenToken.GetLineSpan();
if (!openParenLineSpan.IsValid)
{
return;
}

if (openParenLineSpan.EndLinePosition.Line != firstArgumentLineSpan.StartLinePosition.Line &&
openParenLineSpan.EndLinePosition.Line != (firstArgumentLineSpan.StartLinePosition.Line - 1))
var firstArgument = argumentListSyntax.Arguments.First();
if (SpanMultipleLines(openParenToken, firstArgument))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, firstArgument.GetLocation()));
}
Expand All @@ -363,22 +279,8 @@ private static void AnalyzeBracketParametersList(SyntaxNodeAnalysisContext conte
return;
}

var firstParameter = parameterListSyntax.Parameters[0];

var firstParameterLineSpan = firstParameter.GetLineSpan();
if (!firstParameterLineSpan.IsValid)
{
return;
}

var openBracketLineSpan = openBracketToken.GetLineSpan();
if (!openBracketLineSpan.IsValid)
{
return;
}

if (openBracketLineSpan.EndLinePosition.Line != firstParameterLineSpan.StartLinePosition.Line &&
openBracketLineSpan.EndLinePosition.Line != (firstParameterLineSpan.StartLinePosition.Line - 1))
var firstParameter = parameterListSyntax.Parameters.First();
if (SpanMultipleLines(openBracketToken, firstParameter))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, firstParameter.GetLocation()));
}
Expand All @@ -394,40 +296,29 @@ private static void AnalyzeParametersList(SyntaxNodeAnalysisContext context, Par
return;
}

var firstParameter = parameterListSyntax.Parameters[0];
int firstParameterLine;

if (firstParameter.HasLeadingTrivia && firstParameter.GetLeadingTrivia().All(trivia => IsValidTrivia(trivia)))
{
firstParameterLine = firstParameter.SyntaxTree.GetLineSpan(firstParameter.FullSpan).StartLinePosition.Line;
}
else
{
firstParameterLine = firstParameter.GetLineSpan().StartLinePosition.Line;
}

var parenLine = parameterListSyntax.OpenParenToken.GetLineSpan().EndLinePosition.Line;

if ((firstParameterLine - parenLine) > 1)
var firstParameter = parameterListSyntax.Parameters.First();
if (SpanMultipleLines(parameterListSyntax.OpenParenToken, firstParameter))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, firstParameter.GetLocation()));
}
}

private static bool IsValidTrivia(SyntaxTrivia trivia)
private static bool SpanMultipleLines(SyntaxToken startToken, SyntaxNode endNode)
{
switch (trivia.Kind())
return EndOfLineTriviaCount(startToken.TrailingTrivia) + EndOfLineTriviaCount(endNode.GetLeadingTrivia()) > 1;

static int EndOfLineTriviaCount(SyntaxTriviaList list)
{
case SyntaxKind.IfDirectiveTrivia:
case SyntaxKind.ElseDirectiveTrivia:
case SyntaxKind.ElifDirectiveTrivia:
case SyntaxKind.EndIfDirectiveTrivia:
case SyntaxKind.DisabledTextTrivia:
case SyntaxKind.WhitespaceTrivia:
return true;

default:
return false;
int count = 0;
foreach (var trivia in list)
{
if (trivia.IsKind(SyntaxKind.EndOfLineTrivia))
{
count++;
}
}

return count;
}
}
}
Expand Down