Exemplo n.º 1
0
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeAction(syntaxContext =>
            {
                PropertyBag properties = OptionsHelper.GetProperties(syntaxContext.Options);

                if (!properties.GetProperty(
                        OptionsHelper.BuildDefaultEnabledProperty(ExplicitThisAnalyzer.AnalyzerName)))
                {
                    // Analyzer is entirely disabled
                    return;
                }

                var node = syntaxContext.Node as MemberAccessExpressionSyntax;

                if (node != null)
                {
                    if (node.Expression != null &&
                        node.Expression.Kind() == SyntaxKind.ThisExpression &&
                        IsPrivateField(node, syntaxContext.SemanticModel, syntaxContext.CancellationToken))
                    {
                        syntaxContext.ReportDiagnostic(Diagnostic.Create(s_rule, node.GetLocation(), node.Name));
                    }
                }
            }, SyntaxKind.SimpleMemberAccessExpression);
        }
        private static bool RuleEnabled(SyntaxNodeAnalysisContext syntaxContext)
        {
            PropertyBag properties = OptionsHelper.GetProperties(syntaxContext.Options);

            return(properties.GetProperty(
                       OptionsHelper.BuildDefaultEnabledProperty(ProvideExplicitVariableTypeAnalyzer.AnalyzerName)));
        }
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterCompilationStartAction(compilationContext =>
            {
                PropertyBag properties = OptionsHelper.GetProperties(compilationContext.Options);

                if (!properties.GetProperty(OptionsHelper.BuildDefaultEnabledProperty(AnalyzerName)))
                {
                    // Analyzer is entirely disabled
                    return;
                }

                if (properties.GetProperty(OptimizeNamespaceImportsOptions.PlaceImportsOutsideNamespaceDeclaration))
                {
                    context.RegisterSyntaxNodeAction(LookForUsingsInsideNamespace, SyntaxKind.NamespaceDeclaration);
                }
            });
        }
Exemplo n.º 4
0
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterCompilationStartAction(compilationContext =>
            {
                PropertyBag properties = OptionsHelper.GetProperties(compilationContext.Options);

                if (!properties.GetProperty(
                        OptionsHelper.BuildDefaultEnabledProperty(OptimizeNamespaceImportsAnalyzer.AnalyzerName)))
                {
                    // Analyzer is entirely disabled
                    return;
                }

                if (properties.GetProperty(OptimizeNamespaceImportsOptions.RemoveUnnecessaryImports))
                {
                    context.RegisterSemanticModelAction(LookForUnusedImports);
                }
            });
        }
        private void OnCompilationStart(CompilationStartAnalysisContext context)
        {
            PropertyBag properties = OptionsHelper.GetProperties(context.Options);

            if (!properties.GetProperty(
                    OptionsHelper.BuildDefaultEnabledProperty(UnwrittenWritableFieldAnalyzer.AnalyzerName)))
            {
                return;
            }

            _candidateReadonlyFields = new HashSet <IFieldSymbol>();
            _writtenFields           = new HashSet <IFieldSymbol>();

            _internalsVisibleToAttribute = context.Compilation.GetTypeByMetadataName(
                "System.Runtime.CompilerServices.InternalsVisibleToAttribute");

            context.RegisterSymbolAction(LocateCandidateReadonlyFields, SymbolKind.Field);
            context.RegisterSyntaxNodeAction(CheckForAssignment, s_compoundAssignmentExpressionKinds);
            context.RegisterSyntaxNodeAction(CheckForRefOrOutParameter, SyntaxKind.Argument);
            context.RegisterSyntaxNodeAction(CheckForExternMethodWithRefParameters, SyntaxKind.MethodDeclaration);
            context.RegisterSyntaxNodeAction(CheckForExternIndexer, SyntaxKind.IndexerDeclaration);
            context.RegisterSyntaxNodeAction(CheckForInvocations, SyntaxKind.InvocationExpression);
            context.RegisterCompilationEndAction(ReportUnwrittenFields);
        }