diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1114ParameterListMustFollowDeclaration.cs b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1114ParameterListMustFollowDeclaration.cs index 7f22d28f7..3f1222394 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1114ParameterListMustFollowDeclaration.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1114ParameterListMustFollowDeclaration.cs @@ -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; /// @@ -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())); } @@ -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())); } @@ -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())); } @@ -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())); } @@ -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())); } @@ -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())); } @@ -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; } } }