Skip to content

Commit

Permalink
Fix the issue with "Sequence contains no elements" on top-level state…
Browse files Browse the repository at this point in the history
…ments (#9)

* Fix the issue with "Sequence contains no elements" on top-level statements

* Add test PropertiesNotAssigned_InFileWithTopLevelStatements_AddsDiagnosticWithPropertyNames

Co-authored-by: Andreas Gullberg Larsen <[email protected]>
  • Loading branch information
Pankraty and angularsen authored Oct 1, 2022
1 parent 86388f1 commit 820c79c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
33 changes: 31 additions & 2 deletions AssignAll/AssignAll.Test/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ private static DiagnosticResult GetMissingAssignmentDiagnosticResult(string crea
return expected;
}

private static DiagnosticResult GetMissingAssignmentDiagnosticResult(int line = 9, int column = 23, params string[] unassignedMemberNames)
{
return GetMissingAssignmentDiagnosticResult("Foo", line, column, 0, unassignedMemberNames);
}

private static DiagnosticResult GetMissingAssignmentDiagnosticResult(params string[] unassignedMemberNames)
{
// Most code snippets in the tests are identical up to the object initializer
Expand Down Expand Up @@ -91,7 +96,7 @@ private static void Main()
// Commented assignments after opening brace.
// PropCommented1 = 1,
// Assigned property, OK'ed by analyzer
// Assigned property, OK by analyzer
PropAssigned = 1,
// Commented assignments just before closing brace
Expand Down Expand Up @@ -545,7 +550,7 @@ private static void Main(string[] args)
// AssignAll enable
var foo = new Foo
{
// ProtInt and PropString not assigned, diagnostic error
// PropInt and PropString not assigned, diagnostic error
};
}
Expand All @@ -561,6 +566,30 @@ private class Foo
VerifyCSharpDiagnostic(testContent, expected);
}

[Fact]
public void PropertiesNotAssigned_InFileWithTopLevelStatements_AddsDiagnosticWithPropertyNames()
{
var testContent = @"
// AssignAll enable
var foo = new Foo
{
// PropInt and PropString not assigned, diagnostic error
};
// Add methods and nested types available to top level statements via a partial Program class.
public static partial class Program
{
private class Foo
{
public int PropInt { get; set; }
public string PropString { get; set; }
}
}
";
DiagnosticResult expected = GetMissingAssignmentDiagnosticResult(line: 3, column: 11, "PropInt", "PropString");
VerifyCSharpDiagnostic(testContent, expected);
}

[Fact]
public void PropertiesNotAssigned_NoCommentToEnableAnalyzer_AddsNoDiagnostics()
{
Expand Down
2 changes: 1 addition & 1 deletion AssignAll/AssignAll/AssignAllAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void RegisterObjectInitializerAnalyzerOnCodeBlockStart(CodeBlockStartAna

private RegionsToAnalyze GetOrSetCachedRegionsToAnalyzeInFile(SyntaxNode codeBlock)
{
SyntaxNode rootNode = codeBlock.Ancestors().Last();
SyntaxNode rootNode = codeBlock.Ancestors().LastOrDefault() ?? codeBlock;
SyntaxReference rootNodeRef = rootNode.GetReference();

if (_rootNodeToAnalyzerTextSpans.TryGetValue(rootNodeRef, out RegionsToAnalyze regionsToAnalyze))
Expand Down

0 comments on commit 820c79c

Please sign in to comment.