Skip to content

Commit

Permalink
Code cleanup per code review
Browse files Browse the repository at this point in the history
* Use GetOriginalSemanticModel instead of duplicating logic
* Move local function to separate helper method
  • Loading branch information
sharwell committed Dec 6, 2017
1 parent 245e47b commit 4fb240d
Showing 1 changed file with 39 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1016,54 +1016,12 @@ private static bool TryReplaceWithAlias(this ExpressionSyntax node, SemanticMode
// might be a speculative node (not fully rooted in a tree), we use the original semantic model to find the
// equivalent node in the original tree, and from there determine if the tree has any using alias
// directives.
var originalModel = semanticModel;
while (originalModel.IsSpeculativeSemanticModel)
{
originalModel = originalModel.ParentModel;
}
var originalModel = semanticModel.GetOriginalSemanticModel();

// Perf: We are only using the syntax tree root in a fast-path syntax check. If the root is not readily
// available, it is fine to continue through the normal algorithm.
if (originalModel.SyntaxTree.TryGetRoot(out var root))
{
bool HasUsingAliasDirective(SyntaxNode syntax)
{
SyntaxList<UsingDirectiveSyntax> usings;
SyntaxList<MemberDeclarationSyntax> members;
if (syntax.IsKind(SyntaxKind.NamespaceDeclaration, out NamespaceDeclarationSyntax namespaceDeclaration))
{
usings = namespaceDeclaration.Usings;
members = namespaceDeclaration.Members;
}
else if (syntax.IsKind(SyntaxKind.CompilationUnit, out CompilationUnitSyntax compilationUnit))
{
usings = compilationUnit.Usings;
members = compilationUnit.Members;
}
else
{
return false;
}

foreach (var usingDirective in usings)
{
if (usingDirective.Alias != null)
{
return true;
}
}

foreach (var member in members)
{
if (HasUsingAliasDirective(member))
{
return true;
}
}

return false;
}

if (!HasUsingAliasDirective(root))
{
return false;
Expand Down Expand Up @@ -1169,6 +1127,44 @@ bool HasUsingAliasDirective(SyntaxNode syntax)
return false;
}

private static bool HasUsingAliasDirective(SyntaxNode syntax)
{
SyntaxList<UsingDirectiveSyntax> usings;
SyntaxList<MemberDeclarationSyntax> members;
if (syntax.IsKind(SyntaxKind.NamespaceDeclaration, out NamespaceDeclarationSyntax namespaceDeclaration))
{
usings = namespaceDeclaration.Usings;
members = namespaceDeclaration.Members;
}
else if (syntax.IsKind(SyntaxKind.CompilationUnit, out CompilationUnitSyntax compilationUnit))
{
usings = compilationUnit.Usings;
members = compilationUnit.Members;
}
else
{
return false;
}

foreach (var usingDirective in usings)
{
if (usingDirective.Alias != null)
{
return true;
}
}

foreach (var member in members)
{
if (HasUsingAliasDirective(member))
{
return true;
}
}

return false;
}

// We must verify that the alias actually binds back to the thing it's aliasing.
// It's possible there's another symbol with the same name as the alias that binds
// first
Expand Down

0 comments on commit 4fb240d

Please sign in to comment.