33
44namespace StyleCop . Analyzers . ReadabilityRules
55{
6+ using System ;
67 using System . Collections . Immutable ;
8+ using System . Linq ;
79 using Microsoft . CodeAnalysis ;
810 using Microsoft . CodeAnalysis . CSharp ;
911 using Microsoft . CodeAnalysis . CSharp . Syntax ;
@@ -52,17 +54,9 @@ internal class SA1114ParameterListMustFollowDeclaration : DiagnosticAnalyzer
5254 private static readonly DiagnosticDescriptor Descriptor =
5355 new DiagnosticDescriptor ( DiagnosticId , Title , MessageFormat , AnalyzerCategory . ReadabilityRules , DiagnosticSeverity . Warning , AnalyzerConstants . EnabledByDefault , Description , HelpLink ) ;
5456
55- private static readonly ImmutableArray < DiagnosticDescriptor > SupportedDiagnosticsValue =
56- ImmutableArray . Create ( Descriptor ) ;
57-
5857 /// <inheritdoc/>
59- public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics
60- {
61- get
62- {
63- return SupportedDiagnosticsValue ;
64- }
65- }
58+ public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics { get ; } =
59+ ImmutableArray . Create ( Descriptor ) ;
6660
6761 /// <inheritdoc/>
6862 public override void Initialize ( AnalysisContext context )
@@ -72,9 +66,8 @@ public override void Initialize(AnalysisContext context)
7266
7367 private static void HandleCompilationStart ( CompilationStartAnalysisContext context )
7468 {
75- context . RegisterSyntaxNodeActionHonorExclusions ( HandleMethodDeclaration , SyntaxKind . MethodDeclaration ) ;
69+ context . RegisterSyntaxNodeActionHonorExclusions ( HandleBaseMethodDeclaration , SyntaxKind . MethodDeclaration , SyntaxKind . ConstructorDeclaration , SyntaxKind . OperatorDeclaration ) ;
7670 context . RegisterSyntaxNodeActionHonorExclusions ( HandleMethodInvocation , SyntaxKind . InvocationExpression ) ;
77- context . RegisterSyntaxNodeActionHonorExclusions ( HandleConstructorDeclaration , SyntaxKind . ConstructorDeclaration ) ;
7871 context . RegisterSyntaxNodeActionHonorExclusions ( HandleObjectCreation , SyntaxKind . ObjectCreationExpression ) ;
7972 context . RegisterSyntaxNodeActionHonorExclusions ( HandleIndexerDeclaration , SyntaxKind . IndexerDeclaration ) ;
8073 context . RegisterSyntaxNodeActionHonorExclusions ( HandleArrayCreation , SyntaxKind . ArrayCreationExpression ) ;
@@ -85,13 +78,6 @@ private static void HandleCompilationStart(CompilationStartAnalysisContext conte
8578 context . RegisterSyntaxNodeActionHonorExclusions ( HandleAnonymousMethod , SyntaxKind . AnonymousMethodExpression ) ;
8679 context . RegisterSyntaxNodeActionHonorExclusions ( HandleLambdaExpression , SyntaxKind . ParenthesizedLambdaExpression ) ;
8780 context . RegisterSyntaxNodeActionHonorExclusions ( HandleConversionOperatorDeclaration , SyntaxKind . ConversionOperatorDeclaration ) ;
88- context . RegisterSyntaxNodeActionHonorExclusions ( HandleOperatorDeclaration , SyntaxKind . OperatorDeclaration ) ;
89- }
90-
91- private static void HandleOperatorDeclaration ( SyntaxNodeAnalysisContext context )
92- {
93- var operatorDeclaration = ( OperatorDeclarationSyntax ) context . Node ;
94- AnalyzeParametersList ( context , operatorDeclaration . ParameterList ) ;
9581 }
9682
9783 private static void HandleConversionOperatorDeclaration ( SyntaxNodeAnalysisContext context )
@@ -163,21 +149,15 @@ private static void HandleObjectCreation(SyntaxNodeAnalysisContext context)
163149 }
164150 }
165151
166- private static void HandleConstructorDeclaration ( SyntaxNodeAnalysisContext context )
167- {
168- var constructorDeclaration = ( ConstructorDeclarationSyntax ) context . Node ;
169- AnalyzeParametersList ( context , constructorDeclaration . ParameterList ) ;
170- }
171-
172152 private static void HandleMethodInvocation ( SyntaxNodeAnalysisContext context )
173153 {
174154 var invocationExpression = ( InvocationExpressionSyntax ) context . Node ;
175155 AnalyzeArgumentList ( context , invocationExpression . ArgumentList ) ;
176156 }
177157
178- private static void HandleMethodDeclaration ( SyntaxNodeAnalysisContext context )
158+ private static void HandleBaseMethodDeclaration ( SyntaxNodeAnalysisContext context )
179159 {
180- var methodDeclaration = ( MethodDeclarationSyntax ) context . Node ;
160+ var methodDeclaration = ( BaseMethodDeclarationSyntax ) context . Node ;
181161
182162 AnalyzeParametersList ( context , methodDeclaration . ParameterList ) ;
183163 }
@@ -399,24 +379,40 @@ private static void AnalyzeParametersList(SyntaxNodeAnalysisContext context, Par
399379 }
400380
401381 var firstParameter = parameterListSyntax . Parameters [ 0 ] ;
382+ int firstParameterLine ;
402383
403- var firstParameterLineSpan = firstParameter . GetLineSpan ( ) ;
404- if ( ! firstParameterLineSpan . IsValid )
384+ if ( firstParameter . HasLeadingTrivia && firstParameter . GetLeadingTrivia ( ) . All ( trivia => IsValidTrivia ( trivia ) ) )
405385 {
406- return ;
386+ firstParameterLine = firstParameter . SyntaxTree . GetLineSpan ( firstParameter . FullSpan ) . StartLinePosition . Line ;
407387 }
408-
409- var openParenLineSpan = parameterListSyntax . OpenParenToken . GetLineSpan ( ) ;
410- if ( ! openParenLineSpan . IsValid )
388+ else
411389 {
412- return ;
390+ firstParameterLine = firstParameter . GetLineSpan ( ) . StartLinePosition . Line ;
413391 }
414392
415- if ( openParenLineSpan . EndLinePosition . Line != firstParameterLineSpan . StartLinePosition . Line &&
416- openParenLineSpan . EndLinePosition . Line != ( firstParameterLineSpan . StartLinePosition . Line - 1 ) )
393+ var parenLine = parameterListSyntax . OpenParenToken . GetLineSpan ( ) . EndLinePosition . Line ;
394+
395+ if ( ( firstParameterLine - parenLine ) > 1 )
417396 {
418397 context . ReportDiagnostic ( Diagnostic . Create ( Descriptor , firstParameter . GetLocation ( ) ) ) ;
419398 }
420399 }
400+
401+ private static bool IsValidTrivia ( SyntaxTrivia trivia )
402+ {
403+ switch ( trivia . Kind ( ) )
404+ {
405+ case SyntaxKind . IfDirectiveTrivia :
406+ case SyntaxKind . ElseDirectiveTrivia :
407+ case SyntaxKind . ElifDirectiveTrivia :
408+ case SyntaxKind . EndIfDirectiveTrivia :
409+ case SyntaxKind . DisabledTextTrivia :
410+ case SyntaxKind . WhitespaceTrivia :
411+ return true ;
412+
413+ default :
414+ return false ;
415+ }
416+ }
421417 }
422418}
0 commit comments