Exemple #1
0
        public override void Initialize(AnalysisContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.EnableConcurrentExecution();
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);

            context.RegisterCompilationStartAction(context =>
            {
                if (!context.Compilation.TargetsAspNetCore())
                {
                    return;
                }

                // Load analyzer configuration defining the attribute types that should be mapped.
                var mappings = TypeMapLoader.LoadMappings(context.Options.AdditionalFiles)
                               .Where(m => m.OldName.EndsWith(AttributeSuffix, StringComparison.Ordinal));

                // If attribute type maps are present, register syntax node actions to analyze for those attributes
                if (mappings.Any())
                {
                    // Register actions for handling both C# and VB identifiers
                    context.RegisterSyntaxNodeAction(context => AnalyzeCSharpAttribute(context, mappings), CS.SyntaxKind.Attribute);
                    context.RegisterSyntaxNodeAction(context => AnalyzeVBAttribute(context, mappings), VB.SyntaxKind.Attribute);
                }
            });
        }
        /// <summary>
        /// Initializes the analyzer by registering analysis callback methods.
        /// </summary>
        /// <param name="context">The context to use for initialization.</param>
        public override void Initialize(AnalysisContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.EnableConcurrentExecution();
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);

            context.RegisterCompilationStartAction(context =>
            {
                // Load analyzer configuration defining the types that should be mapped.
                var mappings = TypeMapLoader.LoadMappings(context.Options.AdditionalFiles);

                // If type maps are present, register syntax node actions to analyze for those types
                if (mappings.Any())
                {
                    // Register actions for handling both C# and VB identifiers
                    context.RegisterSyntaxNodeAction(context => AnalyzeCSharpIdentifier(context, mappings), CS.SyntaxKind.IdentifierName);
                    context.RegisterSyntaxNodeAction(context => AnalyzeVBIdentifier(context, mappings), VB.SyntaxKind.IdentifierName);
                }
            });
        }