Skip to content

Commit 02ec78e

Browse files
committed
Add test for edge case where the preprocessor directive name is missing
1 parent 427e26a commit 02ec78e

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1006UnitTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ more invalid text
106106
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
107107
}
108108

109+
[Fact]
110+
public async Task TestMissingDirectiveNameOnLastLineAsync()
111+
{
112+
string testCode = @"
113+
class ClassName
114+
{
115+
}
116+
# ";
117+
118+
DiagnosticResult expected = this.CSharpCompilerError("CS1024").WithMessage("Preprocessor directive expected").WithLocation(5, 1);
119+
120+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
121+
}
122+
109123
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
110124
{
111125
yield return new SA1006PreprocessorKeywordsMustNotBePrecededBySpace();

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1006PreprocessorKeywordsMustNotBePrecededBySpace.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.SpacingRules
88
using Microsoft.CodeAnalysis;
99
using Microsoft.CodeAnalysis.CSharp;
1010
using Microsoft.CodeAnalysis.Diagnostics;
11+
using StyleCop.Analyzers.Helpers;
1112

1213
/// <summary>
1314
/// A C# preprocessor-type keyword is preceded by space.
@@ -82,7 +83,7 @@ private static void HandleHashToken(SyntaxTreeAnalysisContext context, SyntaxTok
8283
}
8384

8485
SyntaxToken targetToken = token.GetNextToken(includeDirectives: true);
85-
if (targetToken.IsMissing)
86+
if (targetToken.IsMissingOrDefault())
8687
{
8788
return;
8889
}

0 commit comments

Comments
 (0)