From d68e26d9ec76c36e7756109efbd7ba36076adfac Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Mon, 21 Aug 2023 20:28:47 +0300 Subject: [PATCH] reproduce flow with failing test --- .../Tips/SanityTests.cs | 14 +++++++++++--- .../Utilities/VariableNameExtractor.cs | 19 ++++++------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs index f5e788ff..c5ae5835 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs @@ -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 List { get; set; } }"; diff --git a/src/FluentAssertions.Analyzers/Utilities/VariableNameExtractor.cs b/src/FluentAssertions.Analyzers/Utilities/VariableNameExtractor.cs index 7a4d3667..1efc4ee0 100644 --- a/src/FluentAssertions.Analyzers/Utilities/VariableNameExtractor.cs +++ b/src/FluentAssertions.Analyzers/Utilities/VariableNameExtractor.cs @@ -1,6 +1,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis; +using System.Collections.Generic; namespace FluentAssertions.Analyzers { @@ -8,8 +9,10 @@ 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 PropertiesAccessed { get; } = new List(); public VariableNameExtractor(SemanticModel semanticModel = null) { @@ -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); } }