提交 13b156b1 编写于 作者: V Victor Z

Moved common logic to extension method.

上级 e0658e34
......@@ -194,18 +194,9 @@ private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context, INamedTypeSymb
// rewrite things.
var outArgumentScope = GetOutArgumentScope(argumentExpression);
if (enclosingBlockOfLocalStatement != outArgumentScope)
if (!outLocalSymbol.CanSafelyMoveLocalToBlock(enclosingBlockOfLocalStatement, outArgumentScope))
{
var localFunctionOrMethodDeclaration = enclosingBlockOfLocalStatement.AncestorsAndSelf()
.FirstOrDefault(node => node.IsKind(SyntaxKind.LocalFunctionStatement, SyntaxKind.MethodDeclaration));
var localFunctionStatement = outArgumentScope.FirstAncestorOrSelf<LocalFunctionStatementSyntax>();
if (localFunctionOrMethodDeclaration != localFunctionStatement &&
HasTypeParameterWithName(localFunctionOrMethodDeclaration, outLocalSymbol.Type.Name) &&
HasTypeParameterWithName(localFunctionStatement, outLocalSymbol.Type.Name))
{
return;
}
return;
}
// Make sure that variable is not accessed outside of that scope.
......@@ -447,23 +438,5 @@ private SyntaxNode GetOutArgumentScope(SyntaxNode argumentExpression)
// No accesses detected
return false;
}
private static bool HasTypeParameterWithName(SyntaxNode node, string name)
{
SeparatedSyntaxList<TypeParameterSyntax>? typeParameters;
switch (node)
{
case MethodDeclarationSyntax methodDeclaration:
typeParameters = methodDeclaration.TypeParameterList?.Parameters;
break;
case LocalFunctionStatementSyntax localFunctionStatement:
typeParameters = localFunctionStatement.TypeParameterList?.Parameters;
break;
default:
return false;
}
return typeParameters.HasValue && typeParameters.Value.Any(x => x.Identifier.ValueText == name);
}
}
}
......@@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MoveDeclarationNearReference;
......@@ -57,40 +58,6 @@ protected override SyntaxToken GetIdentifierOfVariableDeclarator(VariableDeclara
}
protected override bool CanMoveToBlock(ILocalSymbol localSymbol, SyntaxNode currentBlock, SyntaxNode destinationBlock)
{
if (currentBlock != destinationBlock)
{
var localFunctionOrMethodDeclaration = currentBlock.AncestorsAndSelf()
.FirstOrDefault(node => node.IsKind(SyntaxKind.LocalFunctionStatement) || node.IsKind(SyntaxKind.MethodDeclaration));
var localFunctionStatement = destinationBlock.FirstAncestorOrSelf<LocalFunctionStatementSyntax>();
if (localFunctionOrMethodDeclaration != localFunctionStatement &&
HasTypeParameterWithName(localFunctionOrMethodDeclaration, localSymbol.Type.Name) &&
HasTypeParameterWithName(localFunctionStatement, localSymbol.Type.Name))
{
return false;
}
}
return true;
bool HasTypeParameterWithName(SyntaxNode node, string name)
{
SeparatedSyntaxList<TypeParameterSyntax>? typeParameters;
switch (node)
{
case MethodDeclarationSyntax methodDeclaration:
typeParameters = methodDeclaration.TypeParameterList?.Parameters;
break;
case LocalFunctionStatementSyntax localFunctionStatement:
typeParameters = localFunctionStatement.TypeParameterList?.Parameters;
break;
default:
return false;
}
return typeParameters.HasValue && typeParameters.Value.Any(typeParameter => typeParameter.Identifier.ValueText == name);
}
}
=> localSymbol.CanSafelyMoveLocalToBlock(currentBlock, destinationBlock);
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Microsoft.CodeAnalysis.CSharp.Extensions
{
internal static class ILocalSymbolExtensions
{
public static bool CanSafelyMoveLocalToBlock(this ILocalSymbol localSymbol, SyntaxNode currentBlock, SyntaxNode destinationBlock)
{
if (currentBlock != destinationBlock)
{
var localFunctionOrMethodDeclaration = currentBlock.AncestorsAndSelf()
.FirstOrDefault(node => node.IsKind(SyntaxKind.LocalFunctionStatement) || node.IsKind(SyntaxKind.MethodDeclaration));
var localFunctionStatement = destinationBlock.FirstAncestorOrSelf<LocalFunctionStatementSyntax>();
if (localFunctionOrMethodDeclaration != localFunctionStatement &&
HasTypeParameterWithName(localFunctionOrMethodDeclaration, localSymbol.Type.Name) &&
HasTypeParameterWithName(localFunctionStatement, localSymbol.Type.Name))
{
return false;
}
}
return true;
bool HasTypeParameterWithName(SyntaxNode node, string name)
{
SeparatedSyntaxList<TypeParameterSyntax>? typeParameters;
switch (node)
{
case MethodDeclarationSyntax methodDeclaration:
typeParameters = methodDeclaration.TypeParameterList?.Parameters;
break;
case LocalFunctionStatementSyntax localFunctionStatement:
typeParameters = localFunctionStatement.TypeParameterList?.Parameters;
break;
default:
return false;
}
return typeParameters.HasValue && typeParameters.Value.Any(typeParameter => typeParameter.Identifier.ValueText == name);
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册