-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement DOC209 (Use 'see href' correctly)
Closes #13
- Loading branch information
Showing
8 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
...nAnalyzers/DocumentationAnalyzers.Test.CSharp7/PortabilityRules/DOC209CSharp7UnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT license. See LICENSE in the project root for license information. | ||
|
||
namespace DocumentationAnalyzers.Test.CSharp7.PortabilityRules | ||
{ | ||
using DocumentationAnalyzers.Test.PortabilityRules; | ||
|
||
public class DOC209CSharp7UnitTests : DOC209UnitTests | ||
{ | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
DocumentationAnalyzers/DocumentationAnalyzers.Test/PortabilityRules/DOC209UnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT license. See LICENSE in the project root for license information. | ||
|
||
namespace DocumentationAnalyzers.Test.PortabilityRules | ||
{ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Verify = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixVerifier<DocumentationAnalyzers.PortabilityRules.DOC209UseSeeHrefCorrectly, Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider, Microsoft.CodeAnalysis.Testing.Verifiers.XUnitVerifier>; | ||
|
||
public class DOC209UnitTests | ||
{ | ||
[Fact] | ||
public async Task TestAbsoluteUriAsync() | ||
{ | ||
var testCode = $@" | ||
/// <summary> | ||
/// <see href=""https://github.com""/> | ||
/// <see href=""https://github.com""></see> | ||
/// </summary> | ||
/// <seealso href=""https://github.com""/> | ||
/// <seealso href=""https://github.com""></see> | ||
class TestClass | ||
{{ | ||
}} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestRelativeUriAsync() | ||
{ | ||
var testCode = $@" | ||
/// <summary> | ||
/// <see href=""docs/index.md""/> | ||
/// <see href=""docs/index.md""></see> | ||
/// </summary> | ||
/// <seealso href=""docs/index.md""/> | ||
/// <seealso href=""docs/index.md""></see> | ||
class TestClass | ||
{{ | ||
}} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestEscapesAllowedAsync() | ||
{ | ||
var testCode = @" | ||
/// <summary> | ||
/// <see href=""https://github.com""/> | ||
/// </summary> | ||
class TestClass | ||
{ | ||
} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestEmptyAndFullElementsValidatedAsync() | ||
{ | ||
var testCode = @" | ||
/// <summary> | ||
/// <see [|href|]=""https://""/> | ||
/// <see [|href|]=""https://""></see> | ||
/// </summary> | ||
/// <seealso [|href|]=""https://""/> | ||
/// <seealso [|href|]=""https://""></see> | ||
class TestClass | ||
{ | ||
} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestOtherAttributesIgnoredAsync() | ||
{ | ||
var testCode = @" | ||
/// <summary> | ||
/// <see Href=""https://""/> | ||
/// <see x:href=""https://""/> | ||
/// <see name=""https://""/> | ||
/// <see cref=""System.String""/> | ||
/// </summary> | ||
class TestClass | ||
{ | ||
} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestOtherElementsIgnoredAsync() | ||
{ | ||
var testCode = @" | ||
/// <summary> | ||
/// <p:see href=""https://""/> | ||
/// </summary> | ||
/// <p:seealso href=""https://""/> | ||
class TestClass | ||
{ | ||
} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
DocumentationAnalyzers/DocumentationAnalyzers/PortabilityRules/DOC209UseSeeHrefCorrectly.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT license. See LICENSE in the project root for license information. | ||
|
||
namespace DocumentationAnalyzers.PortabilityRules | ||
{ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using DocumentationAnalyzers.Helpers; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
[NoCodeFix("Cannot convert an invalid URI to one intended by the author.")] | ||
internal class DOC209UseSeeHrefCorrectly : DiagnosticAnalyzer | ||
{ | ||
/// <summary> | ||
/// The ID for diagnostics produced by the <see cref="DOC209UseSeeHrefCorrectly"/> analyzer. | ||
/// </summary> | ||
public const string DiagnosticId = "DOC209"; | ||
private const string HelpLink = "https://github.com/DotNetAnalyzers/DocumentationAnalyzers/blob/master/docs/DOC209.md"; | ||
|
||
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(PortabilityResources.DOC209Title), PortabilityResources.ResourceManager, typeof(PortabilityResources)); | ||
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(PortabilityResources.DOC209MessageFormat), PortabilityResources.ResourceManager, typeof(PortabilityResources)); | ||
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(PortabilityResources.DOC209Description), PortabilityResources.ResourceManager, typeof(PortabilityResources)); | ||
|
||
private static readonly DiagnosticDescriptor Descriptor = | ||
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.PortabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink); | ||
|
||
/// <inheritdoc/> | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } | ||
= ImmutableArray.Create(Descriptor); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
|
||
context.RegisterSyntaxNodeAction(HandleXmlNodeSyntax, SyntaxKind.XmlElement, SyntaxKind.XmlEmptyElement); | ||
} | ||
|
||
private static void HandleXmlNodeSyntax(SyntaxNodeAnalysisContext context) | ||
{ | ||
var xmlNodeSyntax = (XmlNodeSyntax)context.Node; | ||
var name = xmlNodeSyntax.GetName(); | ||
if (name is null || name.Prefix != null) | ||
{ | ||
return; | ||
} | ||
|
||
if (name.LocalName.ValueText != XmlCommentHelper.SeeXmlTag | ||
&& name.LocalName.ValueText != XmlCommentHelper.SeeAlsoXmlTag) | ||
{ | ||
return; | ||
} | ||
|
||
SyntaxList<XmlAttributeSyntax> attributes; | ||
if (xmlNodeSyntax is XmlEmptyElementSyntax xmlEmptyElement) | ||
{ | ||
attributes = xmlEmptyElement.Attributes; | ||
} | ||
else | ||
{ | ||
attributes = ((XmlElementSyntax)xmlNodeSyntax).StartTag.Attributes; | ||
} | ||
|
||
foreach (var attribute in attributes) | ||
{ | ||
if (attribute.Name is null || attribute.Name.Prefix != null) | ||
{ | ||
continue; | ||
} | ||
|
||
if (attribute.Name.LocalName.ValueText != XmlCommentHelper.HrefArgumentName) | ||
{ | ||
continue; | ||
} | ||
|
||
var text = ((XmlTextAttributeSyntax)attribute).TextTokens; | ||
string valueText; | ||
if (text.Count == 1) | ||
{ | ||
valueText = text[0].ValueText; | ||
} | ||
else | ||
{ | ||
valueText = string.Join(string.Empty, text.Select(textToken => textToken.ValueText)); | ||
} | ||
|
||
if (Uri.TryCreate(valueText, System.UriKind.RelativeOrAbsolute, out _)) | ||
{ | ||
continue; | ||
} | ||
|
||
context.ReportDiagnostic(Diagnostic.Create(Descriptor, attribute.Name.LocalName.GetLocation())); | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ntationAnalyzers/DocumentationAnalyzers/PortabilityRules/PortabilityResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# DOC209 | ||
|
||
<table> | ||
<tr> | ||
<td>TypeName</td> | ||
<td>DOC209UseSeeHrefCorrectly</td> | ||
</tr> | ||
<tr> | ||
<td>CheckId</td> | ||
<td>DOC209</td> | ||
</tr> | ||
<tr> | ||
<td>Category</td> | ||
<td>Portability Rules</td> | ||
</tr> | ||
</table> | ||
|
||
## Cause | ||
|
||
The documentation contains a `<see href="..."/>` or `<seealso href="..."/>` element with an unrecognized URI. | ||
|
||
## Rule description | ||
|
||
*TODO* | ||
|
||
## How to fix violations | ||
|
||
*TODO* | ||
|
||
## How to suppress violations | ||
|
||
*TODO* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters