Exemplo n.º 1
0
        protected override TypeDeclarationSyntax GetTypeDeclaration(AttributeSyntax attribute)
        {
            var declaration = attribute.FirstAncestorOrSelf <MemberDeclarationSyntax>();

            if (declaration is ClassDeclarationSyntax classDeclaration)
            {
                return(classDeclaration);
            }
            else if (RecordDeclarationSyntaxWrapper.IsInstance(declaration))
            {
                return((RecordDeclarationSyntaxWrapper)declaration);
            }
            else
            {
                return(null);
            }
        }
        private static void Report(INamedTypeSymbol symbol, string message, SymbolAnalysisContext context)
        {
            foreach (var declaringSyntaxReference in symbol.DeclaringSyntaxReferences)
            {
                var node = declaringSyntaxReference.GetSyntax();
                if (node is ClassDeclarationSyntax classDeclaration)
                {
                    context.ReportDiagnosticIfNonGenerated(Diagnostic.Create(Rule, classDeclaration.Identifier.GetLocation(), "class", message));
                }

                if (RecordDeclarationSyntaxWrapper.IsInstance(node))
                {
                    var wrapper = (RecordDeclarationSyntaxWrapper)node;
                    context.ReportDiagnosticIfNonGenerated(Diagnostic.Create(Rule, wrapper.Identifier.GetLocation(), "record", message));
                }
            }
        }
        private static SyntaxNode HandleRecordDeclaration(RecordDeclarationSyntaxWrapper node)
        {
            SyntaxToken triviaToken = node.Keyword;

            if (triviaToken.IsMissing)
            {
                return(null);
            }

            SyntaxKind      defaultVisibility = IsNestedType(node) ? SyntaxKind.PrivateKeyword : SyntaxKind.InternalKeyword;
            SyntaxTokenList modifiers         = DeclarationModifiersHelper.AddModifier(node.Modifiers, ref triviaToken, defaultVisibility);

            return(node
                   .WithKeyword(triviaToken)
                   .WithModifiers(modifiers)
                   .SyntaxNode
                   .WithoutFormatting());
        }