Skip to content

Commit

Permalink
fix: Duplicate Analyzer diagnostics (#3059)
Browse files Browse the repository at this point in the history
Fixes duplicate Analyzer diagnostics

Co-authored-by: Rouke Broersma <[email protected]>
  • Loading branch information
lynkx and rouke-broersma authored Oct 11, 2024
1 parent b6c9e9d commit e9f3763
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,45 @@ public void GetDiagnosticOptions_WithWarningsNotAsErrors()
diagOptions.ShouldContain(new KeyValuePair<string, ReportDiagnostic>("EXTEXP0002", ReportDiagnostic.Warn));
}

[DataTestMethod]
[DataRow("NoWarn", ReportDiagnostic.Suppress)]
[DataRow("WarningsAsErrors", ReportDiagnostic.Error)]
[DataRow("WarningsNotAsErrors", ReportDiagnostic.Warn)]
public void GetDiagnosticOptions_DealWithDuplicate(string property, ReportDiagnostic reportDiagnostic)
{
// Arrange
var analyzerResult = Mock.Of<IAnalyzerResult>();
Mock.Get(analyzerResult)
.SetupGet(g => g.Properties)
.Returns(new Dictionary<string, string> { { property, "EXTEX0001;EXTEX0001" } });

// Act
var diagOptions = IAnalyzerResultExtensions.GetDiagnosticOptions(analyzerResult);

// Assert
diagOptions.ShouldHaveSingleItem();
diagOptions.ShouldContain(new KeyValuePair<string, ReportDiagnostic>("EXTEX0001", reportDiagnostic));
}

[TestMethod]
public void GetDiagnosticOptions_DealWithDuplicateConflicts()
{
// Arrange
var analyzerResult = Mock.Of<IAnalyzerResult>();
Mock.Get(analyzerResult)
.SetupGet(g => g.Properties)
.Returns(new Dictionary<string, string> { { "NoWarn", "EXTEX0001;EXTEX0001" },{ "WarningsAsErrors", "EXTEX0001;EXTEX0001" },
{ "WarningsNotAsErrors", "EXTEX0001;EXTEX0001" }});

// Act
var diagOptions = IAnalyzerResultExtensions.GetDiagnosticOptions(analyzerResult);

// Assert
diagOptions.ShouldHaveSingleItem();
diagOptions.ShouldContain(new KeyValuePair<string, ReportDiagnostic>("EXTEX0001", ReportDiagnostic.Suppress));
}


[TestMethod]
public void GetDiagnosticOptions_DealWithConflicts()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,13 @@ internal static ImmutableDictionary<string, ReportDiagnostic> GetDiagnosticOptio
this IAnalyzerResult analyzerResult)
{
var noWarnString = analyzerResult.GetPropertyOrDefault("NoWarn");
var noWarn = noWarnString is not null
? noWarnString.Split(";").Where(x => !string.IsNullOrWhiteSpace(x))
.ToDictionary(x => x, _ => ReportDiagnostic.Suppress)
: new Dictionary<string, ReportDiagnostic>();
var noWarn = ParseDiagnostics(noWarnString).ToDictionary(x => x, _ => ReportDiagnostic.Suppress);

var warningsAsErrorsString = analyzerResult.GetPropertyOrDefault("WarningsAsErrors");
var warningsAsErrors = warningsAsErrorsString is not null
? warningsAsErrorsString.Split(";").Where(x => !string.IsNullOrWhiteSpace(x))
.ToDictionary(x => x, _ => ReportDiagnostic.Error)
: new Dictionary<string, ReportDiagnostic>();
var warningsAsErrors = ParseDiagnostics(warningsAsErrorsString).ToDictionary(x => x, _ => ReportDiagnostic.Error);

var warningsNotAsErrorsString = analyzerResult.GetPropertyOrDefault("WarningsNotAsErrors");
var warningsNotAsErrors = warningsNotAsErrorsString is not null
? warningsNotAsErrorsString.Split(";").Where(x => !string.IsNullOrWhiteSpace(x))
.ToDictionary(x => x, _ => ReportDiagnostic.Warn)
: new Dictionary<string, ReportDiagnostic>();
var warningsNotAsErrors = ParseDiagnostics(warningsNotAsErrorsString).ToDictionary(x => x, _ => ReportDiagnostic.Warn);

// merge settings,
var diagnosticOptions = new Dictionary<string, ReportDiagnostic>(warningsAsErrors);
Expand All @@ -234,6 +225,19 @@ internal static ImmutableDictionary<string, ReportDiagnostic> GetDiagnosticOptio
return diagnosticOptions.ToImmutableDictionary();
}

private static IEnumerable<string> ParseDiagnostics(string diagnostics)
{
if(string.IsNullOrWhiteSpace(diagnostics))
{
return [];
}

return diagnostics
.Split(";")
.Distinct()
.Where(x => !string.IsNullOrWhiteSpace(x));
}

internal static int GetWarningLevel(this IAnalyzerResult analyzerResult) =>
int.Parse(analyzerResult.GetPropertyOrDefault("WarningLevel", "4"));

Expand Down

0 comments on commit e9f3763

Please sign in to comment.