private static IEnumerable <SwitchSectionSyntax> CreateSwitchSections(IfStatementSyntax ifStatement) { foreach (SyntaxNode node in IfElseChain.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))); } } }
private static HashSet <AwaitExpressionSyntax> GetAwaitExpressionsFromIfStatement( IfStatementSyntax ifStatement, bool endsWithElse) { HashSet <AwaitExpressionSyntax> awaitExpressions = null; foreach (IfStatementOrElseClause ifOrElse in IfElseChain.GetChain(ifStatement)) { if (ifOrElse.IsElse && !endsWithElse) { return(null); } AwaitExpressionSyntax awaitExpression = GetLastReturnAwaitExpressionOfDefault(ifOrElse.Statement); if (awaitExpression != null) { (awaitExpressions ?? (awaitExpressions = new HashSet <AwaitExpressionSyntax>())).Add(awaitExpression); } else { return(null); } } return(awaitExpressions); }
private static IEnumerable <SwitchSectionSyntax> CreateSwitchSections(IfStatementSyntax ifStatement) { foreach (IfStatementOrElseClause ifOrElse in IfElseChain.GetChain(ifStatement)) { if (ifOrElse.IsIf) { ifStatement = ifOrElse.AsIf(); 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 { yield return(DefaultSwitchSection(AddBreakStatementIfNecessary(ifOrElse.Statement))); } } }
public static BracesAnalysisResult AnalyzeBraces(IfStatementSyntax ifStatement) { if (ifStatement == null) { throw new ArgumentNullException(nameof(ifStatement)); } bool anyHasEmbedded = false; bool anyHasBlock = false; bool allSupportsEmbedded = true; int cnt = 0; foreach (IfStatementOrElseClause ifOrElse in IfElseChain.GetChain(ifStatement)) { cnt++; StatementSyntax statement = ifOrElse.Statement; 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) { return(BracesAnalysisResult.AddBraces); } } if (cnt > 1 && allSupportsEmbedded && anyHasBlock) { if (anyHasEmbedded) { return(BracesAnalysisResult.AddBraces | BracesAnalysisResult.RemoveBraces); } else { return(BracesAnalysisResult.RemoveBraces); } } return(BracesAnalysisResult.None); }
private static IEnumerable <IfStatementSyntax> GetIfStatements(IfStatementSyntax ifStatement) { foreach (IfStatementOrElseClause ifOrElse in IfElseChain.GetChain(ifStatement)) { if (ifOrElse.IsIf) { yield return(ifOrElse.AsIf()); } } }
private static IEnumerable <BlockSyntax> GetBlockStatements(IfStatementSyntax ifStatement) { foreach (IfStatementOrElseClause ifOrElse in IfElseChain.GetChain(ifStatement)) { StatementSyntax statement = ifOrElse.Statement; if (statement?.IsKind(SyntaxKind.Block) == true) { yield return((BlockSyntax)statement); } } }
private static IEnumerable <StatementSyntax> GetEmbeddedStatements(IfStatementSyntax topmostIf) { foreach (IfStatementOrElseClause ifOrElse in IfElseChain.GetChain(topmostIf)) { StatementSyntax statement = ifOrElse.Statement; if (statement?.IsKind(SyntaxKind.Block) == false) { yield return(statement); } } }
public static async Task ComputeRefactoringAsync(RefactoringContext context, IfStatementSyntax ifStatement) { if (IfElseChain.IsTopmostIf(ifStatement)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (IfElseChain.GetChain(ifStatement) .Where(f => f.IsKind(SyntaxKind.IfStatement)) .All(f => IsValidIf((IfStatementSyntax)f, semanticModel, context.CancellationToken))) { string title = (IfElseChain.IsPartOfChain(ifStatement)) ? "Replace if-else with switch" : "Replace if with switch"; context.RegisterRefactoring( title, cancellationToken => RefactorAsync(context.Document, ifStatement, cancellationToken)); } } }
private static IEnumerable <StatementSyntax> GetEmbeddedStatements(IfStatementSyntax topmostIf) { foreach (SyntaxNode node in IfElseChain.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; } } } }