コード例 #1
0
        private static void HandleAttributeList(SyntaxNodeAnalysisContext context)
        {
            var diagnosticProperties          = ImmutableDictionary.CreateBuilder <string, string>();
            AttributeListSyntax attributeList = (AttributeListSyntax)context.Node;
            bool violation = false;

            if (attributeList.Parent.IsKind(SyntaxKind.Parameter) || attributeList.Parent.IsKind(SyntaxKind.TypeParameter))
            {
                // no analysis required for parameters or type (generic) parameters
                return;
            }

            var attributeListLineSpan = attributeList.GetLineSpan();

            var prevToken = attributeList.OpenBracketToken.GetPreviousToken();

            if (!prevToken.IsMissingOrDefault())
            {
                var prevTokenLineSpan = prevToken.GetLineSpan();
                if (prevTokenLineSpan.EndLinePosition.Line == attributeListLineSpan.EndLinePosition.Line)
                {
                    diagnosticProperties.Add(FixWithNewLineBeforeKey, string.Empty);
                    violation = true;
                }
            }

            var nextToken = attributeList.CloseBracketToken.GetNextToken();

            if (!nextToken.IsMissingOrDefault())
            {
                var nextTokenLineSpan = nextToken.GetLineSpan();

                // do not report for trailing attribute lists, to prevent unnecessary diagnostics and issues with the code fix
                if ((nextTokenLineSpan.EndLinePosition.Line == attributeListLineSpan.EndLinePosition.Line) && !nextToken.Parent.IsKind(SyntaxKind.AttributeList))
                {
                    diagnosticProperties.Add(FixWithNewLineAfterKey, string.Empty);
                    violation = true;
                }
            }

            if (violation)
            {
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, attributeList.OpenBracketToken.GetLocation(), diagnosticProperties.ToImmutable()));
            }
        }