-
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.
Merge pull request #67 from sharwell/verify-langword
Implement DOC207 (Use 'see langword' correctly)
- Loading branch information
Showing
8 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
...nAnalyzers/DocumentationAnalyzers.Test.CSharp7/PortabilityRules/DOC207CSharp7UnitTests.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 DOC207CSharp7UnitTests : DOC207UnitTests | ||
{ | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
DocumentationAnalyzers/DocumentationAnalyzers.Test/PortabilityRules/DOC207UnitTests.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,125 @@ | ||
// 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.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Xunit; | ||
using Verify = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixVerifier<DocumentationAnalyzers.PortabilityRules.DOC207UseSeeLangwordCorrectly, Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider, Microsoft.CodeAnalysis.Testing.Verifiers.XUnitVerifier>; | ||
|
||
public class DOC207UnitTests | ||
{ | ||
public static IEnumerable<object[]> Keywords | ||
{ | ||
get | ||
{ | ||
foreach (var keywordKind in SyntaxFacts.GetKeywordKinds()) | ||
{ | ||
yield return new[] { SyntaxFacts.GetText(keywordKind) }; | ||
} | ||
} | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Keywords))] | ||
public async Task TestRecognizedKeywordsAsync(string keyword) | ||
{ | ||
var testCode = $@" | ||
/// <summary> | ||
/// <see langword=""{keyword}""/> | ||
/// <see langword=""{keyword}""></see> | ||
/// </summary> | ||
class TestClass | ||
{{ | ||
}} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestSpacesNotAllowedAsync() | ||
{ | ||
var testCode = @" | ||
/// <summary> | ||
/// <see langword=""null""/> | ||
/// <see [|langword|]="" null""/> | ||
/// <see [|langword|]=""null ""/> | ||
/// <see [|langword|]="" null ""/> | ||
/// </summary> | ||
class TestClass | ||
{ | ||
} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestEscapesAllowedAsync() | ||
{ | ||
var testCode = @" | ||
/// <summary> | ||
/// <see langword=""null""/> | ||
/// </summary> | ||
class TestClass | ||
{ | ||
} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestEmptyAndFullElementsValidatedAsync() | ||
{ | ||
var testCode = @" | ||
/// <summary> | ||
/// <see [|langword|]=""not a keyword""/> | ||
/// <see [|langword|]=""not a keyword""></see> | ||
/// </summary> | ||
class TestClass | ||
{ | ||
} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestOtherAttributesIgnoredAsync() | ||
{ | ||
var testCode = @" | ||
/// <summary> | ||
/// <see Langword=""not a keyword""/> | ||
/// <see x:langword=""not a keyword""/> | ||
/// <see name=""not a keyword""/> | ||
/// <see cref=""System.String""/> | ||
/// </summary> | ||
class TestClass | ||
{ | ||
} | ||
"; | ||
|
||
await Verify.VerifyAnalyzerAsync(testCode); | ||
} | ||
|
||
[Fact] | ||
public async Task TestOtherElementsIgnoredAsync() | ||
{ | ||
var testCode = @" | ||
/// <summary> | ||
/// <p:see langword=""not a keyword""/> | ||
/// </summary> | ||
/// | ||
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
100 changes: 100 additions & 0 deletions
100
...ntationAnalyzers/DocumentationAnalyzers/PortabilityRules/DOC207UseSeeLangwordCorrectly.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,100 @@ | ||
// 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.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("https://github.com/DotNetAnalyzers/DocumentationAnalyzers/issues/66")] | ||
internal class DOC207UseSeeLangwordCorrectly : DiagnosticAnalyzer | ||
{ | ||
/// <summary> | ||
/// The ID for diagnostics produced by the <see cref="DOC207UseSeeLangwordCorrectly"/> analyzer. | ||
/// </summary> | ||
public const string DiagnosticId = "DOC207"; | ||
private const string HelpLink = "https://github.com/DotNetAnalyzers/DocumentationAnalyzers/blob/master/docs/DOC207.md"; | ||
|
||
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(PortabilityResources.DOC207Title), PortabilityResources.ResourceManager, typeof(PortabilityResources)); | ||
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(PortabilityResources.DOC207MessageFormat), PortabilityResources.ResourceManager, typeof(PortabilityResources)); | ||
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(PortabilityResources.DOC207Description), 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) | ||
{ | ||
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.LangwordArgumentName) | ||
{ | ||
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 (SyntaxFacts.GetKeywordKind(valueText) != SyntaxKind.None | ||
|| SyntaxFacts.GetContextualKeywordKind(valueText) != SyntaxKind.None) | ||
{ | ||
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 @@ | ||
# DOC207 | ||
|
||
<table> | ||
<tr> | ||
<td>TypeName</td> | ||
<td>DOC207UseSeeLangwordCorrectly</td> | ||
</tr> | ||
<tr> | ||
<td>CheckId</td> | ||
<td>DOC207</td> | ||
</tr> | ||
<tr> | ||
<td>Category</td> | ||
<td>Portability Rules</td> | ||
</tr> | ||
</table> | ||
|
||
## Cause | ||
|
||
The documentation contains a `<see langword="..."/>` element with an unrecognized keyword. | ||
|
||
## 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