private static ImmutableArray <IfRefactoring> Analyze( ExpressionStatementSyntax expressionStatement, IfStatementSyntax ifStatement, IfAnalysisOptions options) { SimpleAssignmentStatement assignment; if (SimpleAssignmentStatement.TryCreate(expressionStatement, out assignment)) { ElseClauseSyntax elseClause = ifStatement.Else; if (elseClause?.Statement?.IsKind(SyntaxKind.IfStatement) == false) { SimpleAssignmentStatement assignment1; if (SimpleAssignmentStatement.TryCreate(ifStatement.GetSingleStatementOrDefault(), out assignment1)) { SimpleAssignmentStatement assignment2; if (SimpleAssignmentStatement.TryCreate(elseClause.GetSingleStatementOrDefault(), out assignment2) && assignment1.Left.IsEquivalentTo(assignment2.Left, topLevel: false) && assignment1.Left.IsEquivalentTo(assignment.Left, topLevel: false) && options.CheckSpanDirectives(ifStatement.Parent, TextSpan.FromBounds(expressionStatement.SpanStart, ifStatement.Span.End))) { return(new AssignmentAndIfElseToAssignmentWithConditionalExpression(expressionStatement, assignment.Right, ifStatement, assignment1.Right, assignment2.Right).ToImmutableArray()); } } } } return(ImmutableArray <IfRefactoring> .Empty); }
private static ImmutableArray <IfRefactoring> Analyze( LocalDeclarationStatementSyntax localDeclarationStatement, IfStatementSyntax ifStatement, IfAnalysisOptions options) { VariableDeclaratorSyntax declarator = localDeclarationStatement .Declaration? .Variables .SingleOrDefault(throwException: false); if (declarator != null) { ElseClauseSyntax elseClause = ifStatement.Else; if (elseClause?.Statement?.IsKind(SyntaxKind.IfStatement) == false) { SimpleAssignmentStatement assignment1; if (SimpleAssignmentStatement.TryCreate(ifStatement.GetSingleStatementOrDefault(), out assignment1)) { SimpleAssignmentStatement assignment2; if (SimpleAssignmentStatement.TryCreate(elseClause.GetSingleStatementOrDefault(), out assignment2) && assignment1.Left.IsKind(SyntaxKind.IdentifierName) && assignment2.Left.IsKind(SyntaxKind.IdentifierName)) { string identifier1 = ((IdentifierNameSyntax)assignment1.Left).Identifier.ValueText; string identifier2 = ((IdentifierNameSyntax)assignment2.Left).Identifier.ValueText; if (string.Equals(identifier1, identifier2, StringComparison.Ordinal) && string.Equals(identifier1, declarator.Identifier.ValueText, StringComparison.Ordinal) && options.CheckSpanDirectives(ifStatement.Parent, TextSpan.FromBounds(localDeclarationStatement.SpanStart, ifStatement.Span.End))) { return(new LocalDeclarationAndIfElseAssignmentWithConditionalExpression(localDeclarationStatement, ifStatement, assignment1.Right, assignment2.Right).ToImmutableArray()); } } } } } return(ImmutableArray <IfRefactoring> .Empty); }
public static bool CanRefactor( IfStatementSyntax ifStatement, SemanticModel semanticModel, CancellationToken cancellationToken = default(CancellationToken)) { if (ifStatement.IsTopmostIf()) { ElseClauseSyntax elseClause = ifStatement.Else; if (elseClause != null) { ExpressionSyntax condition = ifStatement.Condition; if (condition != null) { ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(condition, cancellationToken); if (typeSymbol?.IsBoolean() == true) { AssignmentExpressionSyntax trueExpression = GetSimpleAssignmentExpression(ifStatement.GetSingleStatementOrDefault()); ExpressionSyntax trueRight = trueExpression?.Right; if (trueRight?.IsBooleanLiteralExpression() == true) { AssignmentExpressionSyntax falseExpression = GetSimpleAssignmentExpression(elseClause.GetSingleStatementOrDefault()); ExpressionSyntax falseRight = falseExpression?.Right; if (falseRight?.IsBooleanLiteralExpression() == true) { var trueBooleanLiteral = (LiteralExpressionSyntax)trueRight; var falseBooleanLiteral = (LiteralExpressionSyntax)falseRight; if (trueBooleanLiteral.IsKind(SyntaxKind.TrueLiteralExpression) != falseBooleanLiteral.IsKind(SyntaxKind.TrueLiteralExpression) && trueExpression.Left?.IsEquivalentTo(falseExpression.Left, topLevel: false) == true) { return(true); } } } } } } } return(false); }
public static ImmutableArray <IfRefactoring> Analyze( IfStatementSyntax ifStatement, IfAnalysisOptions options, SemanticModel semanticModel, CancellationToken cancellationToken = default(CancellationToken)) { if (ifStatement.IsTopmostIf()) { ExpressionSyntax condition = ifStatement.Condition; if (condition != null) { ElseClauseSyntax elseClause = ifStatement.Else; if (elseClause != null) { if (options.CheckSpanDirectives(ifStatement)) { StatementSyntax statement1 = ifStatement.GetSingleStatementOrDefault(); if (statement1 != null) { SyntaxKind kind1 = statement1.Kind(); if (kind1 == SyntaxKind.ExpressionStatement || kind1 == SyntaxKind.ReturnStatement || kind1 == SyntaxKind.YieldReturnStatement) { StatementSyntax statement2 = elseClause.GetSingleStatementOrDefault(); if (statement2?.IsKind(kind1) == true) { switch (kind1) { case SyntaxKind.ExpressionStatement: { return(Analyze(ifStatement, condition, (ExpressionStatementSyntax)statement1, (ExpressionStatementSyntax)statement2, semanticModel, cancellationToken, options)); } case SyntaxKind.ReturnStatement: { return(Analyze(ifStatement, condition, ((ReturnStatementSyntax)statement1).Expression, ((ReturnStatementSyntax)statement2).Expression, semanticModel, cancellationToken, options, isYield: false)); } case SyntaxKind.YieldReturnStatement: { return(Analyze(ifStatement, condition, ((YieldStatementSyntax)statement1).Expression, ((YieldStatementSyntax)statement2).Expression, semanticModel, cancellationToken, options, isYield: true)); } } } } } } } else { StatementSyntax nextStatement = ifStatement.NextStatement(); if (nextStatement?.IsKind(SyntaxKind.ReturnStatement) == true) { return(Analyze(ifStatement, (ReturnStatementSyntax)nextStatement, options, semanticModel, cancellationToken)); } } } } return(ImmutableArray <IfRefactoring> .Empty); }
private static ImmutableArray <IfRefactoring> Analyze( ExpressionStatementSyntax expressionStatement, IfStatementSyntax ifStatement, IfAnalysisOptions options) { SimpleAssignmentStatementInfo assignment = SyntaxInfo.SimpleAssignmentStatementInfo(expressionStatement); if (!assignment.Success) { return(Empty); } ElseClauseSyntax elseClause = ifStatement.Else; if (elseClause?.Statement?.IsKind(SyntaxKind.IfStatement) != false) { return(Empty); } SimpleAssignmentStatementInfo assignment1 = SyntaxInfo.SimpleAssignmentStatementInfo(ifStatement.GetSingleStatementOrDefault()); if (!assignment1.Success) { return(Empty); } SimpleAssignmentStatementInfo assignment2 = SyntaxInfo.SimpleAssignmentStatementInfo(elseClause.GetSingleStatementOrDefault()); if (!assignment2.Success) { return(Empty); } if (!SyntaxComparer.AreEquivalent(assignment1.Left, assignment2.Left, assignment.Left)) { return(Empty); } if (!options.CheckSpanDirectives(ifStatement.Parent, TextSpan.FromBounds(expressionStatement.SpanStart, ifStatement.Span.End))) { return(Empty); } return(new AssignmentAndIfElseToAssignmentWithConditionalExpression(expressionStatement, assignment.Right, ifStatement, assignment1.Right, assignment2.Right).ToImmutableArray()); }
public static ImmutableArray <IfRefactoring> Analyze( IfStatementSyntax ifStatement, IfAnalysisOptions options, SemanticModel semanticModel, CancellationToken cancellationToken = default(CancellationToken)) { if (!ifStatement.IsTopmostIf()) { return(Empty); } ExpressionSyntax condition = ifStatement.Condition?.WalkDownParentheses(); if (condition == null) { return(Empty); } ElseClauseSyntax elseClause = ifStatement.Else; if (elseClause != null) { if (!options.CheckSpanDirectives(ifStatement)) { return(Empty); } StatementSyntax statement1 = ifStatement.GetSingleStatementOrDefault(); if (statement1 == null) { return(Empty); } SyntaxKind kind1 = statement1.Kind(); if (kind1.Is( SyntaxKind.ExpressionStatement, SyntaxKind.ReturnStatement, SyntaxKind.YieldReturnStatement)) { StatementSyntax statement2 = elseClause.GetSingleStatementOrDefault(); if (statement2?.Kind() == kind1) { switch (kind1) { case SyntaxKind.ExpressionStatement: { return(Analyze( ifStatement, condition, (ExpressionStatementSyntax)statement1, (ExpressionStatementSyntax)statement2, options, semanticModel, cancellationToken)); } case SyntaxKind.ReturnStatement: { return(Analyze( ifStatement, condition, ((ReturnStatementSyntax)statement1).Expression?.WalkDownParentheses(), ((ReturnStatementSyntax)statement2).Expression?.WalkDownParentheses(), options, isYield: false, semanticModel: semanticModel, cancellationToken: cancellationToken)); } case SyntaxKind.YieldReturnStatement: { return(Analyze( ifStatement, condition, ((YieldStatementSyntax)statement1).Expression?.WalkDownParentheses(), ((YieldStatementSyntax)statement2).Expression?.WalkDownParentheses(), options, isYield: true, semanticModel: semanticModel, cancellationToken: cancellationToken)); } } } } } else if (ifStatement.NextStatementOrDefault() is ReturnStatementSyntax returnStatement) { return(Analyze(ifStatement, returnStatement, options, semanticModel, cancellationToken)); } return(Empty); }
private static ImmutableArray <IfRefactoring> Analyze( LocalDeclarationStatementSyntax localDeclarationStatement, IfStatementSyntax ifStatement, IfAnalysisOptions options) { VariableDeclaratorSyntax declarator = localDeclarationStatement .Declaration? .Variables .SingleOrDefault(shouldthrow: false); if (declarator == null) { return(Empty); } ElseClauseSyntax elseClause = ifStatement.Else; if (elseClause?.Statement?.IsKind(SyntaxKind.IfStatement) != false) { return(Empty); } SimpleAssignmentStatementInfo assignment1 = SyntaxInfo.SimpleAssignmentStatementInfo(ifStatement.GetSingleStatementOrDefault()); if (!assignment1.Success) { return(Empty); } SimpleAssignmentStatementInfo assignment2 = SyntaxInfo.SimpleAssignmentStatementInfo(elseClause.GetSingleStatementOrDefault()); if (!assignment2.Success) { return(Empty); } if (!assignment1.Left.IsKind(SyntaxKind.IdentifierName)) { return(Empty); } if (!assignment2.Left.IsKind(SyntaxKind.IdentifierName)) { return(Empty); } string identifier1 = ((IdentifierNameSyntax)assignment1.Left).Identifier.ValueText; string identifier2 = ((IdentifierNameSyntax)assignment2.Left).Identifier.ValueText; if (!string.Equals(identifier1, identifier2, StringComparison.Ordinal)) { return(Empty); } if (!string.Equals(identifier1, declarator.Identifier.ValueText, StringComparison.Ordinal)) { return(Empty); } if (!options.CheckSpanDirectives(ifStatement.Parent, TextSpan.FromBounds(localDeclarationStatement.SpanStart, ifStatement.Span.End))) { return(Empty); } return(new LocalDeclarationAndIfElseAssignmentWithConditionalExpression(localDeclarationStatement, ifStatement, assignment1.Right, assignment2.Right).ToImmutableArray()); }