private static IEnumerable <SwitchSectionSyntax> CreateSwitchSections(IfStatementSyntax ifStatement) { foreach (SyntaxNode node in IfElseAnalysis.GetChain(ifStatement)) { if (node.IsKind(SyntaxKind.IfStatement)) { ifStatement = (IfStatementSyntax)node; var condition = ifStatement.Condition as BinaryExpressionSyntax; List <SwitchLabelSyntax> labels = CreateSwitchLabels(condition, new List <SwitchLabelSyntax>()); labels.Reverse(); SwitchSectionSyntax section = SwitchSection( List(labels), AddBreakStatementIfNecessary(ifStatement.Statement)); yield return(section); } else { var elseClause = (ElseClauseSyntax)node; yield return(DefaultSwitchSection(AddBreakStatementIfNecessary(elseClause.Statement))); } } }
internal IfElseAnalysisResult(IfStatementSyntax ifStatement) { if (ifStatement == null) { throw new ArgumentNullException(nameof(ifStatement)); } bool anyHasEmbedded = false; bool anyHasBlock = false; bool allSupportsEmbedded = true; int cnt = 0; foreach (SyntaxNode node in IfElseAnalysis.GetChain(ifStatement)) { cnt++; StatementSyntax statement = GetStatement(node); if (!anyHasEmbedded && !statement.IsKind(SyntaxKind.Block)) { anyHasEmbedded = true; } if (!anyHasBlock && statement.IsKind(SyntaxKind.Block)) { anyHasBlock = true; } if (allSupportsEmbedded && !SupportsEmbedded(statement)) { allSupportsEmbedded = false; } if (cnt > 1 && anyHasEmbedded && !allSupportsEmbedded) { AddBraces = true; return; } } if (cnt > 1 && allSupportsEmbedded && anyHasBlock) { RemoveBraces = true; if (anyHasEmbedded) { AddBraces = true; } } }
private static bool CanBeReplacedWithSwitch(IfStatementSyntax ifStatement) { foreach (SyntaxNode node in IfElseAnalysis.GetChain(ifStatement)) { if (node.IsKind(SyntaxKind.IfStatement)) { ifStatement = (IfStatementSyntax)node; var condition = ifStatement.Condition as BinaryExpressionSyntax; if (condition == null || !IsValidCondition(condition, null)) { return(false); } } } return(true); }
private static IEnumerable <StatementSyntax> GetEmbeddedStatements(IfStatementSyntax topmostIf) { foreach (SyntaxNode node in IfElseAnalysis.GetChain(topmostIf)) { switch (node.Kind()) { case SyntaxKind.IfStatement: { var ifStatement = (IfStatementSyntax)node; StatementSyntax statement = ifStatement.Statement; if (statement?.IsKind(SyntaxKind.Block) == false) { yield return(statement); } break; } case SyntaxKind.ElseClause: { var elseClause = (ElseClauseSyntax)node; StatementSyntax statement = elseClause.Statement; if (statement?.IsKind(SyntaxKind.Block) == false) { yield return(statement); } break; } } } }