Skip to content

Commit ada7d92

Browse files
authored
Merge pull request #3863 from MattFromRVA/SA1514_Fix
Fix SA1514: Do not report when documenting types declared in the global namespace
2 parents 627c705 + 86f274a commit ada7d92

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1514UnitTests.cs

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,5 +1151,205 @@ public enum TestEnum
11511151

11521152
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
11531153
}
1154+
1155+
[Fact]
1156+
[WorkItem(3849, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3849")]
1157+
public async Task TestClassInGlobalNamespaceAsync()
1158+
{
1159+
var testCode = @"
1160+
/// <summary>
1161+
/// X.
1162+
/// </summary>
1163+
public class TestClass
1164+
{
1165+
}
1166+
";
1167+
1168+
var expected = DiagnosticResult.EmptyDiagnosticResults;
1169+
1170+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
1171+
}
1172+
1173+
[Fact]
1174+
[WorkItem(3849, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3849")]
1175+
public async Task TestClassInGlobalNamespaceWithoutNewlineAsync()
1176+
{
1177+
var testCode = @"/// <summary>
1178+
/// X.
1179+
/// </summary>
1180+
public class TestClass
1181+
{
1182+
}
1183+
";
1184+
1185+
var expected = DiagnosticResult.EmptyDiagnosticResults;
1186+
1187+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
1188+
}
1189+
1190+
[Fact]
1191+
[WorkItem(3849, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3849")]
1192+
public async Task TestClassInNamespaceAsync()
1193+
{
1194+
var testCode = @"
1195+
namespace TestNamespace
1196+
{
1197+
/// <summary>
1198+
/// X.
1199+
/// </summary>
1200+
public class TestClass
1201+
{
1202+
}
1203+
}
1204+
";
1205+
1206+
var expected = DiagnosticResult.EmptyDiagnosticResults;
1207+
1208+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
1209+
}
1210+
1211+
[Fact]
1212+
[WorkItem(3849, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3849")]
1213+
public async Task TestClassInNamespaceWithCommentAsync()
1214+
{
1215+
var testCode = @"
1216+
namespace TestNamespace
1217+
{
1218+
// Normal comment
1219+
{|#0:///|} <summary>
1220+
/// X.
1221+
/// </summary>
1222+
public class TestClass
1223+
{
1224+
}
1225+
}
1226+
";
1227+
1228+
var fixedCode = @"
1229+
namespace TestNamespace
1230+
{
1231+
// Normal comment
1232+
1233+
/// <summary>
1234+
/// X.
1235+
/// </summary>
1236+
public class TestClass
1237+
{
1238+
}
1239+
}
1240+
";
1241+
1242+
var expected = new[]
1243+
{
1244+
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
1245+
};
1246+
1247+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
1248+
}
1249+
1250+
[Fact]
1251+
[WorkItem(3849, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3849")]
1252+
public async Task TestClassInGlobalNamespaceWithCommentAsync()
1253+
{
1254+
var testCode = @"
1255+
// Normal comment
1256+
{|#0:///|} <summary>
1257+
/// X.
1258+
/// </summary>
1259+
public class TestClass
1260+
{
1261+
}
1262+
";
1263+
1264+
var fixedCode = @"
1265+
// Normal comment
1266+
1267+
/// <summary>
1268+
/// X.
1269+
/// </summary>
1270+
public class TestClass
1271+
{
1272+
}
1273+
";
1274+
1275+
var expected = new[]
1276+
{
1277+
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
1278+
};
1279+
1280+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
1281+
}
1282+
1283+
[Fact]
1284+
[WorkItem(3849, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3849")]
1285+
public async Task TestClassInGlobalNamespaceWithPreprocessorDirectiveAsync()
1286+
{
1287+
var testCode = @"
1288+
#if DEBUG
1289+
#endif
1290+
{|#0:///|} <summary>
1291+
/// X.
1292+
/// </summary>
1293+
public class TestClass
1294+
{
1295+
}
1296+
";
1297+
1298+
var fixedCode = @"
1299+
#if DEBUG
1300+
#endif
1301+
1302+
/// <summary>
1303+
/// X.
1304+
/// </summary>
1305+
public class TestClass
1306+
{
1307+
}
1308+
";
1309+
1310+
var expected = new[]
1311+
{
1312+
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
1313+
};
1314+
1315+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
1316+
}
1317+
1318+
[Fact]
1319+
[WorkItem(3849, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3849")]
1320+
public async Task TestClassInGlobalNamespaceWithMultilineCommentAsync()
1321+
{
1322+
var testCode = @"
1323+
/* Normal
1324+
* multiline
1325+
* comment */
1326+
{|#0:///|} <summary>
1327+
/// X.
1328+
/// </summary>
1329+
public class TestClass
1330+
{
1331+
}
1332+
";
1333+
1334+
var fixedCode = @"
1335+
/* Normal
1336+
* multiline
1337+
* comment */
1338+
1339+
/// <summary>
1340+
/// X.
1341+
/// </summary>
1342+
public class TestClass
1343+
{
1344+
}
1345+
";
1346+
1347+
var expected = new[]
1348+
{
1349+
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
1350+
};
1351+
1352+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
1353+
}
11541354
}
11551355
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1514ElementDocumentationHeaderMustBePrecededByBlankLine.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ private static void HandleDeclaration(SyntaxNodeAnalysisContext context)
170170
// no leading blank line necessary at start of scope.
171171
return;
172172
}
173+
174+
// Logic to handle global namespace case
175+
if (prevToken.IsKind(SyntaxKind.None))
176+
{
177+
// Node is the first element in the global namespace
178+
return;
179+
}
173180
}
174181

175182
context.ReportDiagnostic(Diagnostic.Create(Descriptor, GetDiagnosticLocation(documentationHeader)));

0 commit comments

Comments
 (0)