Skip to content

Commit

Permalink
Merge pull request #3836 from bjornhellander/feature/sa1004-param-mod…
Browse files Browse the repository at this point in the history
…ifier-3817

Update SA1004 to allow doc comments to start with the in, out and ref keywords
  • Loading branch information
sharwell authored May 22, 2024
2 parents 5bdaf0a + 2a93406 commit ac00d09
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,35 @@

namespace StyleCop.Analyzers.Test.CSharp12.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp11.SpacingRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1004DocumentationLinesMustBeginWithSingleSpace,
StyleCop.Analyzers.SpacingRules.SA1004CodeFixProvider>;

public partial class SA1004CSharp12UnitTests : SA1004CSharp11UnitTests
{
[Fact]
[WorkItem(3817, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3817")]
public async Task TestParameterModifierReadOnlyFirstOnLineAsync()
{
string testCode = $@"
/// <summary>
/// Description of some remarks that refer to a method: <see cref=""SomeMethod(int, int, ref
/// readonly string)""/>.
/// </summary>
public class TypeName
{{
public void SomeMethod(int x, int y, ref readonly string z)
{{
throw new System.Exception();
}}
}}";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.SpacingRules
{
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.SpacingRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
Expand All @@ -19,6 +20,20 @@ namespace StyleCop.Analyzers.Test.SpacingRules
/// </summary>
public class SA1004UnitTests
{
public static IEnumerable<object[]> ParameterModifiers
{
get
{
yield return new[] { "out" };
yield return new[] { "ref" };

if (LightupHelpers.SupportsCSharp72)
{
yield return new[] { "in" };
}
}
}

[Fact]
public async Task TestFixedExampleAsync()
{
Expand Down Expand Up @@ -213,5 +228,33 @@ private void Method1(int x, int y)

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(ParameterModifiers))]
[WorkItem(3817, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3817")]
public async Task TestParameterModifierFirstOnLineAsync(string keyword)
{
string testCode = $@"
/// <summary>
/// Description of some remarks that refer to a method: <see cref=""SomeMethod(int, int,
/// {keyword} string)""/>.
/// </summary>
public class TypeName
{{
public void SomeMethod(int x, int y, {keyword} string z)
{{
throw new System.Exception();
}}
}}";

var languageVersion = (LightupHelpers.SupportsCSharp8, LightupHelpers.SupportsCSharp72) switch
{
// Make sure to use C# 7.2 if supported, unless we are going to default to something greater
(false, true) => LanguageVersionEx.CSharp7_2,
_ => (LanguageVersion?)null,
};

await VerifyCSharpDiagnosticAsync(languageVersion, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.SpacingRules
{
using System;
Expand Down Expand Up @@ -112,6 +110,10 @@ private static void HandleDocumentationCommentExteriorTrivia(SyntaxTreeAnalysisC
case SyntaxKind.XmlCommentEndToken:
case SyntaxKind.XmlCDataStartToken:
case SyntaxKind.XmlCDataEndToken:
case SyntaxKind.InKeyword:
case SyntaxKind.OutKeyword:
case SyntaxKind.RefKeyword:
case SyntaxKind.ReadOnlyKeyword:
if (!token.HasLeadingTrivia)
{
break;
Expand Down

0 comments on commit ac00d09

Please sign in to comment.