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?
@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).
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With