public static IEnumerable <SuppressorInfo> Create(DiagnosticSuppressor suppressor) { foreach (var descriptor in suppressor.SupportedSuppressions.Distinct(Comparer)) { yield return(new SuppressorInfo(suppressor, descriptor)); } }
private SuppressorInfo(DiagnosticSuppressor suppressor, SuppressionDescriptor descriptor) : base(suppressor.GetType(), descriptor.Id) { this.Suppressor = suppressor; this.Descriptor = descriptor; var name = descriptor.Id + ".md"; this.HelpLinkUri = $"https://github.com/nunit/nunit.analyzers/tree/master/documentation/{name}"; this.Stub = CreateStub(suppressor, descriptor); }
internal DiagnosticSuppressorBenchmark() { analyzer = new DiagnosticReporter(); suppressor = new TDiagnosticSuppressor(); analyzers = ImmutableArray.Create <DiagnosticAnalyzer>(analyzer, suppressor); source = null !; compilation = null !; syntaxTree = null !; }
public static async Task EnsureSuppressed(DiagnosticSuppressor suppressor, SuppressionDescriptor?suppressionDescriptor, string testCode) { if (suppressionDescriptor != null) { Assert.That(suppressor.SupportedSuppressions, Does.Contain(suppressionDescriptor), "Supported Suppression"); } Compilation compilation = TestHelpers.CreateCompilation(testCode); ImmutableArray <Diagnostic> compilationErrors = compilation.GetDiagnostics(); ImmutableArray <Diagnostic> nonHiddenErrors = compilationErrors.Where(d => d.Severity != DiagnosticSeverity.Hidden) .ToImmutableArray(); ImmutableArray <Diagnostic> suppressibleErrors = nonHiddenErrors.Where(d => suppressor.SupportedSuppressions.Any(s => s.SuppressedDiagnosticId == d.Id)) .ToImmutableArray(); Assert.That(nonHiddenErrors, Is.EquivalentTo(suppressibleErrors), "Non suppressible errors"); Assert.That(suppressibleErrors, Is.Not.Empty, "No errors to suppress"); var withAnalyzer = compilation.WithAnalyzers(ImmutableArray.Create <DiagnosticAnalyzer>(suppressor)); ImmutableArray <Diagnostic> analyzerErrors = await withAnalyzer.GetAllDiagnosticsAsync() .ConfigureAwait(false); nonHiddenErrors = analyzerErrors.Where(d => d.Severity != DiagnosticSeverity.Hidden) .ToImmutableArray(); Assert.That(nonHiddenErrors, Is.Not.Empty); Assert.Multiple(() => { foreach (var error in nonHiddenErrors) { if (suppressionDescriptor is null) { Assert.That(error.IsSuppressed, Is.False, "IsSuppressed: " + error); } else { Assert.That(error.IsSuppressed, Is.True, "IsSuppressed: " + error); if (error.IsSuppressed) { AssertProgrammaticSuppression(error, suppressionDescriptor); } } } }); }
private static string CreateStub(DiagnosticSuppressor suppressor, SuppressionDescriptor descriptor) { var builder = new StringBuilder(); builder.Append("| Code | "); builder.Append($"[{suppressor.GetType().Name}]({CodeFile.Find(suppressor.GetType()).Uri})"); var text = builder.ToString(); var stub = $@"# {descriptor.Id} ## {EscapeTags(descriptor.Justification)} | Topic | Value | :-- | :-- | Id | {descriptor.Id} | Severity | Info | Enabled | True | Category | Suppressor | Code | [<TYPENAME>](<URL>) ## Description {EscapeTags(descriptor.Justification)} ## Motivation ADD MOTIVATION HERE ## How to fix violations ADD HOW TO FIX VIOLATIONS HERE <!-- start generated config severity --> ## Configure severity The rule has no severity, but can be disabled. ### Via ruleset file To disable the rule for a project, you need to add a [ruleset file](https://github.com/nunit/nunit.analyzers/blob/master/src/nunit.analyzers/DiagnosticSuppressors/NUnit.Analyzers.Suppressions.ruleset) ```xml <?xml version=""1.0"" encoding=""utf-8""?> <RuleSet Name=""NUnit.Analyzer Suppressions"" Description=""DiagnosticSuppression Rules"" ToolsVersion=""12.0""> <Rules AnalyzerId=""DiagnosticSuppressors"" RuleNamespace=""NUnit.NUnitAnalyzers""> <Rule Id=""NUnit3001"" Action=""Info"" /> <!-- Possible Null Reference --> <Rule Id=""NUnit3002"" Action=""Info"" /> <!-- NonNullableField is Uninitialized --> </Rules> </RuleSet> ``` and add it to the project like: ```xml <PropertyGroup> <CodeAnalysisRuleSet>NUnit.Analyzers.Suppressions.ruleset</CodeAnalysisRuleSet> </PropertyGroup> ``` For more info about rulesets see [MSDN](https://msdn.microsoft.com/en-us/library/dd264949.aspx). ### Via .editorconfig file This is currently not working. Waiting for [Roslyn](https://github.com/dotnet/roslyn/issues/49727) ```ini # {descriptor.Id}: {descriptor.Justification.ToString(CultureInfo.InvariantCulture)} dotnet_diagnostic.{descriptor.Id}.severity = none ``` <!-- end generated config severity --> "; return(Replace(stub, "| Code | [<TYPENAME>](<URL>)", text)); }
public void SetSupportedDiagnostics(DiagnosticSuppressor suppressor, ImmutableArray <LocationFactory> locationFactories) { supportedDiagnostics = suppressor.SupportedSuppressions .Select(static descriptor => descriptor.SuppressedDiagnosticId)
public static Task EnsureNotSuppressed(DiagnosticSuppressor suppressor, string testCode) { return(EnsureSuppressed(suppressor, null, testCode)); }