Beispiel #1
0
 private static void __Execute(atom.Trace context, int level, FieldDeclarationSyntax data, string file, bool isShowPrivate)
 {
     if (__IsEnabled(data, isShowPrivate))
     {
         context.
         SetComment(__GetType(data, data.Declaration.Type?.ToString()), HINT.DATA_TYPE).
         SetUrl(file, __GetLine(data.GetLocation()), __GetPosition(data.GetLocation())).
         SetValue(data.Declaration.Variables.First()?.Initializer?.Value?.ToString()).
         Send(NAME.SOURCE.PREVIEW, NAME.TYPE.VARIABLE, level, data.Declaration.Variables.First()?.Identifier.ValueText);
     }
 }
Beispiel #2
0
        public void InternalConstructorSetsAllData()
        {
            FieldDeclarationSyntax field = GetNode <FieldDeclarationSyntax>("class Test { int field1, field2; }");
            const int index = 1;
            VariableDeclaratorSyntax decl          = field.Declaration.Variables[index];
            SemanticModel            semanticModel = Compilation.CurrentCompilation.GetSemanticModel(field.SyntaxTree);
            IFieldSymbol             symbol        = (semanticModel.GetDeclaredSymbol(decl) as IFieldSymbol) !;

            Data.FieldData data     = new(symbol, Compilation);
            Location       location = field.GetLocation();

            Assert.True(
                data.SemanticModel is not null &&
                data.SemanticModel.SyntaxTree.IsEquivalentTo(semanticModel.SyntaxTree) &&
                data.Symbol is not null &&
                SymbolEqualityComparer.Default.Equals(data.Symbol, symbol) &&
                data.Location is not null &&
                data.Location == location &&
                data.Declaration is not null &&
                data.Declaration.IsEquivalentTo(field) &&
                data.ParentCompilation is not null &&
                data.ParentCompilation == Compilation &&
                data.Variable is not null &&
                data.Variable.IsEquivalentTo(decl) &&
                data.Index == index
                );
        }
        private void Analyze(SyntaxNodeAnalysisContext context)
        {
            if (Helper.IsGeneratedCode(context))
            {
                return;
            }

            FieldDeclarationSyntax fieldDeclaration = (FieldDeclarationSyntax)context.Node;

            // ignore struct
            if (fieldDeclaration.Parent.Kind() == SyntaxKind.StructDeclaration)
            {
                return;
            }

            if (fieldDeclaration.Modifiers.Any(SyntaxKind.PublicKeyword))
            {
                // ignore the const
                if (fieldDeclaration.Modifiers.Any(SyntaxKind.ConstKeyword))
                {
                    return;
                }

                // ignore the static
                if (fieldDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword))
                {
                    return;
                }
                Diagnostic diagnostic = Diagnostic.Create(Rule, fieldDeclaration.GetLocation());
                context.ReportDiagnostic(diagnostic);
            }
        }
        private void OnField(SyntaxNodeAnalysisContext context)
        {
            FieldDeclarationSyntax fieldDeclarationSyntax = (FieldDeclarationSyntax)context.Node;

            if (fieldDeclarationSyntax.Modifiers.Any(SyntaxKind.ProtectedKeyword))
            {
                context.ReportDiagnostic(Diagnostic.Create(_rule, fieldDeclarationSyntax.GetLocation()));
            }
        }
        private static void CheckForDiagnostics(SyntaxNodeAnalysisContext context, FieldDeclarationSyntax fieldNode, IFieldSymbol fieldSymbol)
        {
            var isStatic   = fieldSymbol.IsStatic;
            var isPublic   = fieldSymbol.DeclaredAccessibility.HasFlag(Accessibility.Public);
            var isReadOnly = fieldSymbol.IsReadOnly;

            if (!isStatic || !isPublic || !isReadOnly)
            {
                context.ReportDiagnostic(Diagnostic.Create(
                                             mustBePublicStaticAndReadonlyRule, fieldNode.GetLocation()));
            }
        }
Beispiel #6
0
 public override void VisitFieldDeclaration(FieldDeclarationSyntax node)
 {
     foreach (var modifier in node.Modifiers)
     {
         InsertLLOCMap(modifier.GetLocation());
     }
     InsertLLOCMap(node.GetLocation());
     foreach (var variable in node.Declaration.Variables)
     {
         InsertLLOCMap(variable.GetLocation());
     }
     base.VisitFieldDeclaration(node);
 }
Beispiel #7
0
        private void AnalyzeFields(SyntaxNodeAnalysisContext context)
        {
            FieldDeclarationSyntax fieldDeclarationSyntax = context.Node as FieldDeclarationSyntax;

            if (fieldDeclarationSyntax != null)
            {
                foreach (var variable in fieldDeclarationSyntax.Declaration.Variables)
                {
                    Diagnose(variable.Identifier.ValueText,
                             fieldDeclarationSyntax.GetLocation(), context.ReportDiagnostic);
                }
            }
        }