Skip to content

Commit 7e57a0b

Browse files
authored
Fix MA0048 in case when first type is not first node (#785)
1 parent c939d72 commit 7e57a0b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Meziantou.Analyzer/Rules/FileNameMustMatchTypeNameAnalyzer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
8686
var isFirstType = true;
8787
foreach (var node in root.DescendantNodesAndSelf(descendIntoChildren: node => !IsTypeDeclaration(node)))
8888
{
89+
if (!IsTypeDeclaration(node))
90+
continue;
91+
8992
if (node.SpanStart < symbolNode.SpanStart)
9093
{
9194
isFirstType = false;

tests/Meziantou.Analyzer.Test/Rules/FileNameMustMatchTypeNameAnalyzerTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,36 @@ struct Bar {}
247247
.ValidateAsync();
248248
}
249249

250+
[Fact]
251+
public async Task MatchOnlyFirstType_TypeWithBlockScopedNamespaceDeclaration()
252+
{
253+
await CreateProjectBuilder()
254+
.WithSourceCode(fileName: "Test0.cs", """
255+
namespace Sample
256+
{
257+
struct [||]Foo {}
258+
struct Bar {}
259+
}
260+
""")
261+
.AddAnalyzerConfiguration("MA0048.only_validate_first_type", "true")
262+
.ValidateAsync();
263+
}
264+
265+
#if CSHARP10_OR_GREATER
266+
[Fact]
267+
public async Task MatchOnlyFirstType_TypeWithFileScopedNamespaceDeclaration()
268+
{
269+
await CreateProjectBuilder()
270+
.WithSourceCode(fileName: "Test0.cs", """
271+
namespace Sample;
272+
struct [||]Foo {}
273+
struct Bar {}
274+
""")
275+
.AddAnalyzerConfiguration("MA0048.only_validate_first_type", "true")
276+
.ValidateAsync();
277+
}
278+
#endif
279+
250280
[Theory]
251281
[InlineData("Sample")]
252282
[InlineData("T:MyNamespace.Sample")]

0 commit comments

Comments
 (0)