コード例 #1
0
 private DescriptorInfo(DiagnosticAnalyzer analyzer, DiagnosticDescriptor descriptor)
 {
     this.Analyzer          = analyzer;
     this.Descriptor        = descriptor;
     this.DocumentationFile = new MarkdownFile(Path.Combine(DocumentsDirectory.FullName, descriptor.Id + ".md"));
     this.AnalyzerFile      = CodeFile.Find(analyzer.GetType());
 }
コード例 #2
0
            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));
            }
コード例 #3
0
            private static string CreateStub(DiagnosticAnalyzer analyzer, DiagnosticDescriptor descriptor)
            {
                var builder = new StringBuilder();

                builder.Append($"|{(builder.Length == 0 ? " Code     " : "          ")}| ");
                builder.Append($"[{analyzer.GetType().Name}]({CodeFile.Find(analyzer.GetType()).Uri})");

                var text = builder.ToString();
                var stub = $@"# {descriptor.Id}

## {EscapeTags(descriptor.Title)}

| Topic    | Value
| :--      | :--
| Id       | {descriptor.Id}
| Severity | {descriptor.DefaultSeverity}
| Enabled  | {(descriptor.IsEnabledByDefault ? "True" : "False")}
| Category | {descriptor.Category}
| Code     | [<TYPENAME>](<URL>)

## Description

{EscapeTags(descriptor.Description)}

## Motivation

ADD MOTIVATION HERE

## How to fix violations

ADD HOW TO FIX VIOLATIONS HERE

<!-- start generated config severity -->
## Configure severity

### Via ruleset file

Configure the severity per project, for more info see [MSDN](https://msdn.microsoft.com/en-us/library/dd264949.aspx).

### Via .editorconfig file

```ini
# {descriptor.Id}: {descriptor.Title.ToString(CultureInfo.InvariantCulture)}
dotnet_diagnostic.{descriptor.Id}.severity = chosenSeverity
```

where `chosenSeverity` can be one of `none`, `silent`, `suggestion`, `warning`, or `error`.

### Via #pragma directive

```csharp
#pragma warning disable {descriptor.Id} // {descriptor.Title.ToString(CultureInfo.InvariantCulture)}
Code violating the rule here
#pragma warning restore {descriptor.Id} // {descriptor.Title.ToString(CultureInfo.InvariantCulture)}
```

Or put this at the top of the file to disable all instances.

```csharp
#pragma warning disable {descriptor.Id} // {descriptor.Title.ToString(CultureInfo.InvariantCulture)}
```

### Via attribute `[SuppressMessage]`

```csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage(""{descriptor.Category}"",
    ""{descriptor.Id}:{descriptor.Title.ToString(CultureInfo.InvariantCulture)}"",
    Justification = ""Reason..."")]
```
<!-- end generated config severity -->
";

                return(Replace(stub, "| Code     | [<TYPENAME>](<URL>)", text));
            }
コード例 #4
0
 protected BaseInfo(Type analyzerType, string id)
 {
     this.Id = id;
     this.DocumentationFile = new MarkdownFile(Path.Combine(DocumentsDirectory.FullName, id + ".md"));
     this.AnalyzerFile      = CodeFile.Find(analyzerType);
 }
コード例 #5
0
            private static string CreateStub(DiagnosticDescriptor descriptor)
            {
                var stub = $@"# {descriptor.Id}
## {descriptor.Title.ToString(CultureInfo.InvariantCulture)}

| Topic    | Value
| :--      | :--
| Id       | {descriptor.Id}
| Severity | {descriptor.DefaultSeverity}
| Enabled  | {(descriptor.IsEnabledByDefault ? "True" : "False")}
| Category | {descriptor.Category}
| Code     | [<TYPENAME>](<URL>)


## Description

{descriptor.Description.ToString(CultureInfo.InvariantCulture)}

## Motivation

ADD MOTIVATION HERE

## How to fix violations

ADD HOW TO FIX VIOLATIONS HERE

<!-- start generated config severity -->
## Configure severity

### Via ruleset file.

Configure the severity per project, for more info see [MSDN](https://msdn.microsoft.com/en-us/library/dd264949.aspx).

### Via #pragma directive.
```C#
#pragma warning disable {descriptor.Id} // {descriptor.Title.ToString(CultureInfo.InvariantCulture)}
Code violating the rule here
#pragma warning restore {descriptor.Id} // {descriptor.Title.ToString(CultureInfo.InvariantCulture)}
```

Or put this at the top of the file to disable all instances.
```C#
#pragma warning disable {descriptor.Id} // {descriptor.Title.ToString(CultureInfo.InvariantCulture)}
```

### Via attribute `[SuppressMessage]`.

```C#
[System.Diagnostics.CodeAnalysis.SuppressMessage(""{descriptor.Category}"", 
    ""{descriptor.Id}:{descriptor.Title.ToString(CultureInfo.InvariantCulture)}"", 
    Justification = ""Reason..."")]
```
<!-- end generated config severity -->";

                var builder = StringBuilderPool.Borrow();

                foreach (var analyzer in Analyzers.Where(x => x.SupportedDiagnostics.Any(d => d.Id == descriptor.Id)))
                {
                    _ = builder.Append($"|{(builder.Length == 0 ? " Code     " : "          ")}| ")
                        .AppendLine($"[{analyzer.GetType().Name}]({CodeFile.Find(analyzer.GetType()).Uri})");
                }

                var text = builder.Return();

                return(stub.Replace("| Code     | [<TYPENAME>](<URL>)\r\n", text, StringComparison.Ordinal)
                       .Replace("| Code     | [<TYPENAME>](<URL>)\n", text, StringComparison.Ordinal));
            }
コード例 #6
0
        private static string CreateStub(DescriptorInfo descriptorInfo)
        {
            var descriptor = descriptorInfo.Descriptor;
            var stub       = $@"# {descriptor.Id}
## {descriptor.Title.ToString(CultureInfo.InvariantCulture)}

<!-- start generated table -->
<table>
  <tr>
    <td>CheckId</td>
    <td>{descriptor.Id}</td>
  </tr>
  <tr>
    <td>Severity</td>
    <td>{descriptor.DefaultSeverity.ToString()}</td>
  </tr>
  <tr>
    <td>Enabled</td>
    <td>{(descriptor.IsEnabledByDefault ? "True" : "False")}</td>
  </tr>
  <tr>
    <td>Category</td>
    <td>{descriptor.Category}</td>
  </tr>
  <tr>
    <td>Code</td>
    <td><a href=""<URL>""><TYPENAME></a></td>
  </tr>
</table>
<!-- end generated table -->

## Description

{descriptor.Description.ToString(CultureInfo.InvariantCulture)}

## Motivation

ADD MOTIVATION HERE

## How to fix violations

ADD HOW TO FIX VIOLATIONS HERE

<!-- start generated config severity -->
## Configure severity

### Via ruleset file.

Configure the severity per project, for more info see [MSDN](https://msdn.microsoft.com/en-us/library/dd264949.aspx).

### Via #pragma directive.
```C#
#pragma warning disable {descriptor.Id} // {descriptor.Title.ToString(CultureInfo.InvariantCulture)}
Code violating the rule here
#pragma warning restore {descriptor.Id} // {descriptor.Title.ToString(CultureInfo.InvariantCulture)}
```

Or put this at the top of the file to disable all instances.
```C#
#pragma warning disable {descriptor.Id} // {descriptor.Title.ToString(CultureInfo.InvariantCulture)}
```

### Via attribute `[SuppressMessage]`.

```C#
[System.Diagnostics.CodeAnalysis.SuppressMessage(""{descriptor.Category}"", 
    ""{descriptor.Id}:{descriptor.Title.ToString(CultureInfo.InvariantCulture)}"", 
    Justification = ""Reason..."")]
```
<!-- end generated config severity -->";

            if (Analyzers.Count(x => x.SupportedDiagnostics.Any(d => d.Id == descriptor.Id)) == 1)
            {
                return(stub.AssertReplace("<TYPENAME>", descriptorInfo.Analyzer.GetType().Name)
                       .AssertReplace("<URL>", descriptorInfo.AnalyzerFile.Uri));
            }

            var builder = StringBuilderPool.Borrow();

            foreach (var analyzer in Analyzers.Where(x => x.SupportedDiagnostics.Any(d => d.Id == descriptor.Id)))
            {
                _ = builder.AppendLine("  <tr>")
                    .AppendLine($"    <td>{(builder.Length <= "  <tr>\r\n".Length ? "Code" : string.Empty)}</td>")
                    .AppendLine($"    <td><a href=\"{CodeFile.Find(analyzer.GetType()).Uri}\">{analyzer.GetType().Name}</a></td>")
                    .AppendLine("  </tr>");
            }

            var text = builder.Return();

            return(stub.Replace("  <tr>\r\n    <td>Code</td>\r\n    <td><a href=\"<URL>\"><TYPENAME></a></td>\r\n  </tr>\r\n", text)
                   .Replace("  <tr>\n    <td>Code</td>\n    <td><a href=\"<URL>\"><TYPENAME></a></td>\n  </tr>\n", text));
        }