Skip to content

Commit

Permalink
reproduce flow with failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Meir017 committed Aug 21, 2023
1 parent 59d35c3 commit d68e26d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
14 changes: 11 additions & 3 deletions src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,20 @@ public class TestClass
{
public static void Main()
{
var x = new TestType();
x.List.Any().Should().BeTrue();
var x = new TestType1();
x.Prop1.Prop2.List.Any().Should().BeTrue();
}
}
public class TestType
public class TestType1
{
public TestType2 Prop1 { get; set; }
}
public class TestType2
{
public TestType3 Prop2 { get; set; }
}
public class TestType3
{
public List<int> List { get; set; }
}";
Expand Down
19 changes: 6 additions & 13 deletions src/FluentAssertions.Analyzers/Utilities/VariableNameExtractor.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis;
using System.Collections.Generic;

namespace FluentAssertions.Analyzers
{
public class VariableNameExtractor : CSharpSyntaxWalker
{
private readonly SemanticModel _semanticModel;

public string VariableName { get; private set; }
public IdentifierNameSyntax VariableIdentifierName { get; private set; }
public string VariableName => VariableIdentifierName?.Identifier.Text;
public IdentifierNameSyntax VariableIdentifierName => PropertiesAccessed.Count > 0 ? PropertiesAccessed[0] : null;

public List<IdentifierNameSyntax> PropertiesAccessed { get; } = new List<IdentifierNameSyntax>();

public VariableNameExtractor(SemanticModel semanticModel = null)
{
Expand All @@ -20,17 +23,7 @@ public override void VisitIdentifierName(IdentifierNameSyntax node)
{
if (IsVariable(node))
{
VariableName = node.Identifier.Text;
VariableIdentifierName = node;
}
}

public override void Visit(SyntaxNode node)
{
// the first identifier encountered will be the one at the bottom of the syntax tree
if (VariableName == null)
{
base.Visit(node);
PropertiesAccessed.Add(node);
}
}

Expand Down

0 comments on commit d68e26d

Please sign in to comment.