Skip to content

Commit 1c3c14d

Browse files
authored
Merge pull request #2653 from vweijsters/fix-2649
Fixed issue in FileHeaderHelpers with incomplete multiline comments
2 parents f01be7a + 1247ff6 commit 1c3c14d

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1633UnitTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
77
using System.Threading.Tasks;
88
using Microsoft.CodeAnalysis.CodeFixes;
99
using StyleCop.Analyzers.DocumentationRules;
10+
using TestHelper;
1011
using Xunit;
1112

1213
/// <summary>
@@ -344,6 +345,43 @@ public async Task TestValidFileHeaderWithLeadingBlankLinesAsync(string prefix)
344345
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
345346
}
346347

348+
/// <summary>
349+
/// Verifies that incomplete multiline comment at the start of the file is handled correctly.
350+
/// </summary>
351+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
352+
[Fact]
353+
[WorkItem(2649, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2649")]
354+
public async Task TestIncompleteMultilineCommentAsync()
355+
{
356+
this.useNoXmlSettings = true;
357+
358+
var testCode = @"/*
359+
* copyright (c) FooCorp. All rights reserved.
360+
";
361+
362+
var fixedCode = @"// copyright (c) FooCorp. All rights reserved.
363+
364+
/*
365+
* copyright (c) FooCorp. All rights reserved.
366+
";
367+
368+
DiagnosticResult[] expectedDiagnostics =
369+
{
370+
this.CSharpCompilerError("CS1035").WithMessage("End-of-file found, '*/' expected").WithLocation(1, 1),
371+
this.CSharpDiagnostic(FileHeaderAnalyzers.SA1633DescriptorMissing).WithLocation(1, 1),
372+
};
373+
374+
// The fixed code will still have the incomplete comment, as there is no certainty that the incomplete comment was intended as file header.
375+
DiagnosticResult[] expectedFixedDiagnostics =
376+
{
377+
this.CSharpCompilerError("CS1035").WithMessage("End-of-file found, '*/' expected").WithLocation(3, 1),
378+
};
379+
380+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);
381+
await this.VerifyCSharpDiagnosticAsync(fixedCode, expectedFixedDiagnostics, CancellationToken.None).ConfigureAwait(false);
382+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2).ConfigureAwait(false);
383+
}
384+
347385
/// <inheritdoc/>
348386
protected override string GetSettings()
349387
{

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/FileHeaderHelpers.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ internal static FileHeader ParseFileHeader(SyntaxNode root)
6565

6666
var startIndex = triviaString.IndexOf("/*", StringComparison.Ordinal) + 2;
6767
var endIndex = triviaString.LastIndexOf("*/", StringComparison.Ordinal);
68+
if (endIndex == -1)
69+
{
70+
// While editing, it is possible to have a multiline comment trivia that does not contain the closing '*/' yet.
71+
return FileHeader.MissingFileHeader;
72+
}
73+
6874
var commentContext = triviaString.Substring(startIndex, endIndex - startIndex).Trim();
6975

7076
var triviaStringParts = commentContext.Replace("\r\n", "\n").Split('\n');

0 commit comments

Comments
 (0)