示例#1
0
        public static void Analyze(SyntaxNodeAnalysisContext context, AccessorListSyntax accessorList)
        {
            if (accessorList.Accessors.Any(f => f.Body != null))
            {
                if (accessorList.IsSingleLine(includeExteriorTrivia: false))
                {
                    context.ReportDiagnostic(DiagnosticDescriptor, accessorList.GetLocation());
                }
                else
                {
                    foreach (AccessorDeclarationSyntax accessor in accessorList.Accessors)
                    {
                        if (ShouldBeFormatted(accessor))
                        {
                            context.ReportDiagnostic(DiagnosticDescriptor, accessor.GetLocation());
                        }
                    }
                }
            }
            else if (accessorList.IsParentKind(SyntaxKind.PropertyDeclaration) &&
                     accessorList.Accessors.All(f => f.AttributeLists.Count == 0) &&
                     !accessorList.IsSingleLine(includeExteriorTrivia: false))
            {
                var propertyDeclaration = (PropertyDeclarationSyntax)accessorList.Parent;

                if (!propertyDeclaration.Identifier.IsMissing &&
                    !accessorList.CloseBraceToken.IsMissing)
                {
                    TextSpan span = TextSpan.FromBounds(
                        propertyDeclaration.Identifier.Span.End,
                        accessorList.CloseBraceToken.Span.Start);

                    if (propertyDeclaration
                        .DescendantTrivia(span)
                        .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        context.ReportDiagnostic(DiagnosticDescriptor, accessorList.GetLocation());
                    }
                }
            }
        }