Implement with block using temporary variable
This commit is contained in:
Родитель
92b5f82ab8
Коммит
3ee3be8c62
|
@ -16,6 +16,7 @@ namespace RefactoringEssentials.CSharp.Converter
|
||||||
{
|
{
|
||||||
SemanticModel semanticModel;
|
SemanticModel semanticModel;
|
||||||
NodesVisitor nodesVisitor;
|
NodesVisitor nodesVisitor;
|
||||||
|
public const string WithBlockTempVariableName = "withBlock";
|
||||||
|
|
||||||
public bool IsIterator { get; set; }
|
public bool IsIterator { get; set; }
|
||||||
|
|
||||||
|
@ -255,6 +256,18 @@ namespace RefactoringEssentials.CSharp.Converter
|
||||||
throw new NotSupportedException();
|
throw new NotSupportedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override SyntaxList<StatementSyntax> VisitWithBlock(VBSyntax.WithBlockSyntax node)
|
||||||
|
{
|
||||||
|
var withExpression = (ExpressionSyntax) node.WithStatement.Expression.Accept(nodesVisitor);
|
||||||
|
var variableDeclaratorSyntax = SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(WithBlockTempVariableName), null, SyntaxFactory.EqualsValueClause(withExpression));
|
||||||
|
var declaration = SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(
|
||||||
|
SyntaxFactory.IdentifierName("var"),
|
||||||
|
SyntaxFactory.SingletonSeparatedList(variableDeclaratorSyntax)));
|
||||||
|
var statements = node.Statements.SelectMany(s => s.Accept(this));
|
||||||
|
return SingleStatement(SyntaxFactory.Block(new[] {declaration}.Concat(statements).ToArray()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private bool ConvertToSwitch(ExpressionSyntax expr, SyntaxList<VBSyntax.CaseBlockSyntax> caseBlocks, out SwitchStatementSyntax switchStatement)
|
private bool ConvertToSwitch(ExpressionSyntax expr, SyntaxList<VBSyntax.CaseBlockSyntax> caseBlocks, out SwitchStatementSyntax switchStatement)
|
||||||
{
|
{
|
||||||
switchStatement = null;
|
switchStatement = null;
|
||||||
|
|
|
@ -821,8 +821,16 @@ namespace RefactoringEssentials.CSharp.Converter
|
||||||
|
|
||||||
var left = (ExpressionSyntax) node.Expression?.Accept(this);
|
var left = (ExpressionSyntax) node.Expression?.Accept(this);
|
||||||
if (left == null)
|
if (left == null)
|
||||||
|
{
|
||||||
|
if (!node.Parent.Parent.IsKind(VBasic.SyntaxKind.WithBlock))
|
||||||
|
{
|
||||||
return SyntaxFactory.MemberBindingExpression(simpleNameSyntax);
|
return SyntaxFactory.MemberBindingExpression(simpleNameSyntax);
|
||||||
else if (node.Expression.IsKind(VBasic.SyntaxKind.GlobalName))
|
}
|
||||||
|
|
||||||
|
left = SyntaxFactory.IdentifierName(MethodBodyVisitor.WithBlockTempVariableName);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.Expression.IsKind(VBasic.SyntaxKind.GlobalName))
|
||||||
{
|
{
|
||||||
return SyntaxFactory.AliasQualifiedName((IdentifierNameSyntax) left, simpleNameSyntax);
|
return SyntaxFactory.AliasQualifiedName((IdentifierNameSyntax) left, simpleNameSyntax);
|
||||||
}
|
}
|
||||||
|
|
|
@ -215,6 +215,34 @@ class TestClass
|
||||||
}");
|
}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WithBlock()
|
||||||
|
{
|
||||||
|
TestConversionVisualBasicToCSharp(@"Class TestClass
|
||||||
|
Private Sub TestMethod()
|
||||||
|
With New StringBuilder
|
||||||
|
.Capacity = 20
|
||||||
|
.Length = 0
|
||||||
|
End With
|
||||||
|
End Sub
|
||||||
|
End Class", @"using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.VisualBasic;
|
||||||
|
|
||||||
|
class TestClass
|
||||||
|
{
|
||||||
|
private void TestMethod()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var withBlock = new StringBuilder();
|
||||||
|
withBlock.Capacity = 20;
|
||||||
|
withBlock.Length = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ArrayInitializationStatement()
|
public void ArrayInitializationStatement()
|
||||||
{
|
{
|
||||||
|
|
Загрузка…
Ссылка в новой задаче