How would I make more of this analysis Roslyn and less text parsing?

Topics: APIs, C# Language Design, General
Oct 17, 2014 at 8:30 PM
Edited Oct 27, 2014 at 10:46 PM
F# version

C# version

it does what I want but seems more of it should be doable from Roslyn's analysis
Developer
Oct 17, 2014 at 9:56 PM
It looks like you are trying to find all single line ifs with return statements in the body? Instead of using regex you could check if the statement contains any end-of-line trivia.
var singleLineIfs = root.DescendantNodes()
  .Where(n =>
    n.IsKind(SyntaxKind.IfStatement) &&                  // Find all if statements in the syntax tree
    n.DescendantNodes()
      .Any(c => c.IsKind(SyntaxKind.ReturnStatement)) && // Check if this statement also contains a return statement
   !n.DescendantTokens()
      .Any(t => t.IsKind(SyntaxKind.EndOfLineTrivia)));  // Check if there are any newlines
In general if you care about whitespace or formatting you can look at tokens and trivia to figure it out instead of parsing the string using regex.
Developer
Oct 18, 2014 at 12:07 AM
You could also look at the start and end positions of the if statement and see if they are on the same line.
Oct 18, 2014 at 3:29 AM
one of the issues I was running into was that comment lines near the if would increase the line count, and the start marker iirc.

@nmgafter - getting line numbers seemed incredibly convoluted the way I finally figured out to do it.
Oct 20, 2014 at 6:52 PM
@jmarolf t.IsKind comes up as not defined.
however .Kind is there.

I need to try this more from C# since I'm flying blind using F# in linqpad.
Developer
Oct 20, 2014 at 8:52 PM
@ImaginaryDev

What version of roslyn are you using? IsKind is an extension method that should come with the Microsoft.CodeAnalysis namespace.
Oct 20, 2014 at 8:59 PM
Roslyn.Compilers.Common 1.2.20906.2
Roslyn.Compilers.CSharp 1.2.20906.2
Roslyn.Services.Common 1.2.20906.2
Roslyn.Services.CSharp 1.2.20906.2

I don't see a Microsoft.CodeAnalysis namespace available here, only:
Microsoft.CSharp.RuntimeHelpers
Microsoft.Runtime.Hosting
Microsoft.Runtime.Hosting.Interop
Developer
Oct 20, 2014 at 9:56 PM
@ImaginaryDev, I see, I would recommend upgrading to the latest nuget packages. There have been considerable changes since then.
Oct 20, 2014 at 10:46 PM
Alright, removed that, now I have no ISolution, or CommonSyntaxNode anywhere.
Developer
Oct 21, 2014 at 5:51 PM
ISolution was renamed to Solution. CommonSyntaxNode is now just SyntaxNode.
Oct 22, 2014 at 1:13 PM
Alright, I see those, but now there's no Solution.Load, is there somewhere I can see a list of renamed/moved things?
Oct 22, 2014 at 1:32 PM
Found how to do that guy, but no luck tracking down CodeIssue or CodeIssueKind
Developer
Oct 22, 2014 at 9:03 PM
CodeIssue was renamed to DiagnosticAnalyzer. I wasn't kidding when I said the apis have changed a lot :) If you have any other snippets you are having trouble converting to the new api, point me at them and I'll see what I can do.
Oct 22, 2014 at 9:16 PM
Would it be beneficial to have a table of the changes?
Developer
Oct 22, 2014 at 9:39 PM
I would estimate there have been thousands of API changes since we last had the Roslyn.* style namespaces. I don't think I could put together a comprehensive list but if there are any questions about how stuff works today I can definitly answer that :)
Oct 22, 2014 at 10:43 PM
Can't you do a api diff via Roslyn, from the VS13 preview and the VS14ctp4 version?
Developer
Oct 22, 2014 at 11:22 PM
Yes of course :) I'll track down the changeset for the 2013 CTP and do a diff. I'll reply back here with my results.
Oct 23, 2014 at 12:18 PM
@ImaginaryDev Here is another Example it was my first diagnostic and codefix
Oct 27, 2014 at 5:09 PM
translated all, compilation now succeeds

tried to do it in a .fsx file to get some intellisense and type metadata on mouse-overs.

Now
  • the .linq file fails to load the solution
    • Could not load file or assembly 'Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
  • the .fsx may be failing for the same reason but the exception is:
    • System.TypeLoadException: Could not load type 'Microsoft.Build.Construction.SolutionFile' from assembly 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Both file sources are here
Oct 27, 2014 at 8:08 PM
ImaginaryDev wrote:
F# version

C# version

it does what I want but seems more of it should be doable from Roslyn's analysis
Links seem to be dead now.
Oct 27, 2014 at 10:46 PM
dotnetchris wrote:
ImaginaryDev wrote:
F# version

C# version

it does what I want but seems more of it should be doable from Roslyn's analysis
Links seem to be dead now.
updated those links
Nov 6, 2014 at 5:49 PM
ImaginaryDev wrote:
translated all, compilation now succeeds

tried to do it in a .fsx file to get some intellisense and type metadata on mouse-overs.

Now
  • the .linq file fails to load the solution
    • Could not load file or assembly 'Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
  • the .fsx may be failing for the same reason but the exception is:
    • System.TypeLoadException: Could not load type 'Microsoft.Build.Construction.SolutionFile' from assembly 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Both file sources are here
any ideas on this one anyone?
Developer
Nov 6, 2014 at 7:57 PM
Which version of visual studio do you have installed? To use the solution loading features you need to have Visual Studio "14" installed so the correct version of MSBuild can be located.