Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Roslyn's ".WithFooToken()" calls superfluous?

I'm trying out Roslyn's code-generation capabilities using LinqPad to run fragments. LinqPad's .Dump() extension method renders a formatted view of the object to the Result pane.

The code generated by http://roslynquoter.azurewebsites.net/ includes a lot of code that doesn't seem to do much other than add bloat. The following code outputs return null; whether the .WithFooToken(...) calls are commented out or not.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

var syn =
    SyntaxFactory.ReturnStatement(
        SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)
//          .WithToken(SyntaxFactory.Token(SyntaxKind.NullKeyword))
    )
//  .WithReturnKeyword(
//      SyntaxFactory.Token(SyntaxKind.ReturnKeyword)
//  )
//  .WithSemicolonToken(
//      SyntaxFactory.Token(SyntaxKind.SemicolonToken)
//  )
;
syn.NormalizeWhitespace().ToFullString().Dump();

Are such calls optional for all languages or only for languages which have sensible defaults built into their Roslyn support libraries?

like image 990
Emyr Avatar asked Feb 23 '16 16:02

Emyr


2 Answers

@svick's answer is correct. I just forgot to re-enable that code because Roslyn Scripting wasn't available.

Now that Scripting is public, I have re-enabled that code and updated the website: http://roslynquoter.azurewebsites.net/

Try out the website right now, and redundant calls like you mention above should be gone (they'll still appear when they're needed to modify trivia/comments/etc).

like image 133
Kirill Osenkov Avatar answered Oct 13 '22 01:10

Kirill Osenkov


Yes, in simple cases, those calls are not necessary. The code of RoslynQuoter has the RemoveRedundantModifyingCalls option, which I believe is supposed to do exactly what you want. But it's not implemented, the only usage is commented out:

private void AddModifyingCall(ApiCall apiCall, MethodCall methodCall)
{
    // TODO: this needs scripting
    ////if (RemoveRedundantModifyingCalls)
    ////{
    ////    var before = Evaluate(apiCall, UseDefaultFormatting);
    ////    apiCall.Add(methodCall);
    ////    var after = Evaluate(apiCall, UseDefaultFormatting);
    ////    if (before == after)
    ////    {
    ////        apiCall.Remove(methodCall);
    ////    }
    ////}

    apiCall.Add(methodCall);
    return;
}
like image 45
svick Avatar answered Oct 13 '22 01:10

svick