File: Analyzers\CodeAnalysisResult.cs
Web Access
Project: src\src\sdk\src\Dotnet.Format\dotnet-format\dotnet-format.csproj (dotnet-format)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;

namespace Microsoft.CodeAnalysis.Tools.Analyzers
{
    internal class CodeAnalysisResult
    {
        private readonly Dictionary<Project, List<Diagnostic>> _dictionary = new();
        private readonly ImmutableHashSet<string> _diagnostics;
        private readonly ImmutableHashSet<string> _excludeDiagnostics;

        public CodeAnalysisResult(ImmutableHashSet<string> diagnostics, ImmutableHashSet<string> excludeDiagnostics)
        {
            _diagnostics = diagnostics;
            _excludeDiagnostics = excludeDiagnostics;
        }

        internal void AddDiagnostic(Project project, Diagnostic diagnostic)
        {
            // Ignore excluded diagnostics
            if (!_excludeDiagnostics.IsEmpty && _excludeDiagnostics.Contains(diagnostic.Id))
            {
                return;
            }

            if (!_diagnostics.IsEmpty && !_diagnostics.Contains(diagnostic.Id))
            {
                return;
            }

            if (!_dictionary.ContainsKey(project))
            {
                _dictionary.Add(project, new List<Diagnostic>() { diagnostic });
            }
            else
            {
                _dictionary[project].Add(diagnostic);
            }
        }

        public IReadOnlyDictionary<Project, List<Diagnostic>> Diagnostics
            => _dictionary;
    }
}