0

So, I have a MethodDeclarationSyntax node that I'm passing to a CSharpSyntaxWalker, with the following overrides

        public override void VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
    {
        LiteralExpressionCollector literalCollector = new LiteralExpressionCollector();
        literalCollector.Visit(node.ArgumentList);
        if (literalCollector.Literals.Count > 0)
            Creations.Add(node, literalCollector.Literals);
    }

        public override void VisitAssignmentExpression(AssignmentExpressionSyntax node)
    {
        LiteralExpressionCollector literalCollector = new LiteralExpressionCollector();
        var assigment = node.ChildNodes().FirstOrDefault(l => l is LiteralExpressionSyntax);
        if(assigment != null)
            literalCollector.Visit(assigment);
        if (literalCollector.Literals.Count > 0)
            Assigments.Add(node, literalCollector.Literals);

    }

It catches all ObjectCreationExpressionSyntax in the following form:

ResolveBomForMaterialInput rbfmiInput = new ResolveBomForMaterialInput()

But not the following:

FlowStructureScenario flowScenario = null;
flowScenario = new FlowStructureScenario("F", "F:3");

Any idea of why this is happening? I don't think it matters but I'm using SyntaxWalkerDepth.Token as depth.

Currently using the version: Microsoft.CodeAnalysis 1.3.2

Daniel Mendonça
  • 391
  • 1
  • 3
  • 14

1 Answers1

1

The issue is that your code in VisitAssignmentExpression stops the syntax walk as soon as it encounters an assignment expression. If you want to continue walking its child nodes, you can add base.VisitAssignmentExpression(node); to the method.

svick
  • 236,525
  • 50
  • 385
  • 514
  • You're totally right, that's what happens. I don't really understand why the second case is considered na assigment if the first isn't. Is it because the variable was declared already and has no type before the name? Thank you – Daniel Mendonça Mar 14 '17 at 08:47
  • This where the syntax visualizer mentioned by Jason Malinkwski becomes useful. Using it, you can see that the first case is a `LocalDeclarationStatement` containing `VariableDeclaration` containing one `VariableDeclarator`, while the second case is `ExpressionStatement` containing `SimpleAssignmentExpression`. – svick Mar 14 '17 at 17:59