From 1a9dcd36c959929d925399893f56dfd4e35435f3 Mon Sep 17 00:00:00 2001 From: NextTurn <45985406+NextTurn@users.noreply.github.com> Date: Sat, 2 Feb 2019 00:00:00 +0800 Subject: [PATCH 1/2] SA1114: Allow all valid trivia --- ...A1114ParameterListMustFollowDeclaration.cs | 154 +++--------------- 1 file changed, 19 insertions(+), 135 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1114ParameterListMustFollowDeclaration.cs b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1114ParameterListMustFollowDeclaration.cs index 7f22d28f7..02069de88 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1114ParameterListMustFollowDeclaration.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1114ParameterListMustFollowDeclaration.cs @@ -195,22 +195,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 +213,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 +230,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 +247,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 +264,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 +281,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 +298,20 @@ 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; + return list.Count(trivia => trivia.IsKind(SyntaxKind.EndOfLineTrivia)); } } } From 1c1193a51e9be5b613963d83d97ea5a53ca01519 Mon Sep 17 00:00:00 2001 From: NextTurn <45985406+NextTurn@users.noreply.github.com> Date: Thu, 5 Mar 2020 00:00:00 +0800 Subject: [PATCH 2/2] Avoid boxing --- .../SA1114ParameterListMustFollowDeclaration.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1114ParameterListMustFollowDeclaration.cs b/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1114ParameterListMustFollowDeclaration.cs index 02069de88..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; /// @@ -311,7 +309,16 @@ private static bool SpanMultipleLines(SyntaxToken startToken, SyntaxNode endNode static int EndOfLineTriviaCount(SyntaxTriviaList list) { - return list.Count(trivia => trivia.IsKind(SyntaxKind.EndOfLineTrivia)); + int count = 0; + foreach (var trivia in list) + { + if (trivia.IsKind(SyntaxKind.EndOfLineTrivia)) + { + count++; + } + } + + return count; } } }