Syntax.ParseCompilationUnit(" ").GetLeadingTrivia().Count should be greater than zero


  • I expected Syntax.ParseCompilationUnit(" ").GetLeadingTrivia().Count to be greater than zero but I actually got zero. The leading trivia is on the EOF token but CompilationUnitSyntax is not returning it. CompilationUnitSyntax's traversal to find the first token is failing when the first token is the EOF token.


    martedì 21 febbraio 2012 06:18


  • This is certainly a bug. For the above CompilationUnitSyntax, .HasLeadingTrivia returns true and .GetLeadingTrivia().Count returns 0 [although the trivia can be accessed using .DescendentTrivia()]. .HasLeadingTrivia and .GetLeadingTrivia() mustn't disagree.

    I tried the (below) repro against our latest internal build and can confirm that the issue does not repro there. So this should be fixed in the next public release...

    var node = Syntax.ParseCompilationUnit("  ");
    Console.WriteLine("HasLeadingTrivia: " + node.HasLeadingTrivia);
    Console.WriteLine("GetLeadingTrivia().Count: " + node.GetLeadingTrivia().Count);
    foreach (var tr in node.GetLeadingTrivia())
        Console.WriteLine("    Leading: '" + tr.GetText() + "'");
    Console.WriteLine("DescendentTrivia().Count(): " + node.DescendentTrivia().Count());
    foreach (var tr in node.DescendentTrivia())
        Console.WriteLine("    Descendent Leading: '" + tr.GetText() + "'");

    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team

    • Proposto come risposta svick martedì 21 febbraio 2012 21:24
    • Contrassegnato come risposta kingces95 mercoledì 22 febbraio 2012 23:44
    martedì 21 febbraio 2012 21:22