Skip to content

Commit

Permalink
feat: Better displays known async replacements in Sharpen Results
Browse files Browse the repository at this point in the history
  • Loading branch information
ironcev committed Oct 7, 2018
1 parent 019689f commit 0220013
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
32 changes: 8 additions & 24 deletions src/Sharpen.Engine/DisplayText.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#if DEBUG
using System;
#endif
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand All @@ -17,28 +14,15 @@ public static string For(SyntaxNode syntaxNode)
case SyntaxKind.ConstructorDeclaration: return For((ConstructorDeclarationSyntax)syntaxNode);
case SyntaxKind.PropertyDeclaration: return For((PropertyDeclarationSyntax)syntaxNode);
case SyntaxKind.IndexerDeclaration: return For((IndexerDeclarationSyntax)syntaxNode);
case SyntaxKind.ReturnStatement: return syntaxNode.ToString();
case SyntaxKind.Parameter: return syntaxNode.ToString();
case SyntaxKind.Argument: return syntaxNode.ToString();
case SyntaxKind.InvocationExpression: return syntaxNode.ToString();
case SyntaxKind.LocalFunctionStatement: return For((LocalFunctionStatementSyntax)syntaxNode);
case SyntaxKind.ObjectCreationExpression: return syntaxNode.ToString();
case SyntaxKind.SimpleMemberAccessExpression: return syntaxNode.ToString();
// In general, we want to have a proper customized representation for each
// of the known nodes. Still, since sometimes we do display arbitrary nodes
// we will need the fallback to generic display in the release build.
// During debugging, let it crash so that we can extend the list with additional
// known cases, even if we will choose that they have a generic display.
#if DEBUG
default:
throw new ArgumentOutOfRangeException
(
nameof(syntaxNode),
$"Getting the display text for the syntax node of type {syntaxNode.GetType().Name} is currently not supported."
);
#else
case SyntaxKind.ReturnStatement:
case SyntaxKind.Parameter:
case SyntaxKind.Argument:
case SyntaxKind.InvocationExpression:
case SyntaxKind.ObjectCreationExpression:
case SyntaxKind.SimpleMemberAccessExpression:
case SyntaxKind.EqualsValueClause: return syntaxNode.ToString();
default: return syntaxNode.ToString();
#endif
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public IEnumerable<AnalysisResult> Analyze(SyntaxTree syntaxTree, SemanticModel
analysisContext,
syntaxTree.FilePath,
GetStartingSyntaxNode(node).GetFirstToken(),
node
node.FirstAncestorOrSelf<StatementSyntax>() ?? node
));

bool InvokedMemberHasAsynchronousEquivalentThatCanBeAwaited(SyntaxNode invocation)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ReSharper disable All

using System;
using System.Threading;
using static System.Threading.Thread;

Expand All @@ -12,5 +13,14 @@ void CannotBeAwaited()
Thread.Sleep(0);
Sleep(0);
}

async void CannotBeAwaitedSoFarBecauseOfLambda()
{
Action a = () =>
{
Thread.Sleep(0);
Sleep(0);
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ async void CanBeAwaited()
var task = new Task<int>(() => 0);
var result = task.Result;
result = task.Result + 100;
Method(task.Result);
SomeMethod(task.Result);
}

void Method(int i) { }
void SomeMethod(int i) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ReSharper disable All

// Expected number of suggestions: 1
// Expected number of suggestions: 3

using System.Threading.Tasks;

Expand All @@ -12,6 +12,9 @@ async void CanBeAwaited()
{
var task = new Task(() => {});
task.Wait(0);
task.Wait(1 + 2 + 2);
int wait = 100;
task.Wait(wait);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ReSharper disable All

// Expected number of suggestions: 2
// Expected number of suggestions: 3

using System.Threading.Tasks;
using static System.Threading.Tasks.Task;
Expand All @@ -13,6 +13,7 @@ async void CanBeAwaited()
{
Task.WaitAll(null);
WaitAll(null);
System.Threading.Tasks.Task.WaitAll(new Task[0]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ReSharper disable All

// Expected number of suggestions: 2
// Expected number of suggestions: 4

using System.Threading.Tasks;
using static System.Threading.Tasks.Task;
Expand All @@ -13,6 +13,8 @@ async void CanBeAwaited()
{
Task.WaitAny(null);
WaitAny(null);
System.Threading.Tasks.Task.WaitAny(new Task[0]);
int value = WaitAny(null, 1000);
}
}
}

0 comments on commit 0220013

Please sign in to comment.