Skip to content

Commit 4940ff7

Browse files
committed
Add suppressor support for dotnet-format
1 parent cc47659 commit 4940ff7

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Diff for: src/BuiltInTools/dotnet-format/Analyzers/AnalyzerFormatter.cs

+7
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,13 @@ internal static async Task<ImmutableDictionary<ProjectId, ImmutableArray<Diagnos
315315
.Where(analyzer => DoesAnalyzerSupportLanguage(analyzer, project.Language));
316316
foreach (var analyzer in filteredAnalyzer)
317317
{
318+
// Allow suppressors unconditionally
319+
if (analyzer is DiagnosticSuppressor suppressor)
320+
{
321+
analyzers.Add(suppressor);
322+
continue;
323+
}
324+
318325
// Filter by excluded diagnostics
319326
if (!excludeDiagnostics.IsEmpty &&
320327
analyzer.SupportedDiagnostics.All(descriptor => excludeDiagnostics.Contains(descriptor.Id)))

Diff for: src/BuiltInTools/dotnet-format/Analyzers/CodeStyleInformationProvider.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,28 @@ public ImmutableDictionary<ProjectId, AnalyzersAndFixers> GetAnalyzersAndFixers(
3333
.Select(path => new AnalyzerFileReference(path, analyzerAssemblyLoader));
3434

3535
var analyzersByLanguage = new Dictionary<string, AnalyzersAndFixers>();
36+
37+
// We need AnalyzerReferenceInformationProvider to load all local project suppressors
38+
var referenceProvider = new AnalyzerReferenceInformationProvider();
39+
var projectsReferenceAnalyzersAndFixers = referenceProvider.GetAnalyzersAndFixers(workspace, solution, formatOptions, logger);
40+
3641
return solution.Projects
3742
.ToImmutableDictionary(
3843
project => project.Id,
3944
project =>
4045
{
4146
if (!analyzersByLanguage.TryGetValue(project.Language, out var analyzersAndFixers))
4247
{
43-
var analyzers = references.SelectMany(reference => reference.GetAnalyzers(project.Language)).ToImmutableArray();
48+
var analyzers = references.SelectMany(reference => reference.GetAnalyzers(project.Language));
4449
var codeFixes = AnalyzerFinderHelpers.LoadFixers(references.Select(reference => reference.GetAssembly()), project.Language);
45-
analyzersAndFixers = new AnalyzersAndFixers(analyzers, codeFixes);
50+
51+
// Add local project suppressors to global analyzers
52+
if (projectsReferenceAnalyzersAndFixers.TryGetValue(project.Id, out var referenceAnalyzersAndFixers))
53+
{
54+
analyzers = referenceAnalyzersAndFixers.Analyzers.OfType<DiagnosticSuppressor>().Aggregate(analyzers, (current, suppressor) => current.Append(suppressor));
55+
}
56+
57+
analyzersAndFixers = new AnalyzersAndFixers(analyzers.ToImmutableArray(), codeFixes);
4658
analyzersByLanguage.Add(project.Language, analyzersAndFixers);
4759
}
4860

0 commit comments

Comments
 (0)