private static async Task <bool> CanBeSeparateStatementsAsync( Document document, ISyntaxFactsService syntaxFacts, IIfLikeStatementGenerator ifGenerator, SyntaxNode ifOrElseIf, CancellationToken cancellationToken ) { // In order to make separate statements, ifOrElseIf must be an if statement, not an else-if clause. if (ifGenerator.IsElseIfClause(ifOrElseIf, out _)) { return(false); } // If there is an else clause, we *could* in theory separate these and move the current else clause to the second // statement, but we won't. It would break the else-if chain in an odd way. We'll insert an else-if instead. if (ifGenerator.GetElseIfAndElseClauses(ifOrElseIf).Length > 0) { return(false); } var insideStatements = syntaxFacts.GetStatementContainerStatements(ifOrElseIf); if (insideStatements.Count == 0) { // Even though there are no statements inside, we still can't split this into separate statements // because it would change the semantics from short-circuiting to always evaluating the second condition, // breaking code like 'if (a == null || a.InstanceMethod())'. return(false); } else { // There are statements inside. We can split this into separate statements and leave out the 'else' if // control flow can't reach the end of these statements (otherwise, it would continue to the second 'if' // and in the case that both conditions are true, run the same statements twice). // This will typically look like a single return, break, continue or a throw statement. var semanticModel = await document .GetSemanticModelAsync(cancellationToken) .ConfigureAwait(false); var controlFlow = semanticModel.AnalyzeControlFlow( insideStatements[0], insideStatements[insideStatements.Count - 1] ); return(!controlFlow.EndPointIsReachable); } }
private static async Task <bool> CanStatementsBeMergedAsync( Document document, ISyntaxFactsService syntaxFacts, IBlockFactsService blockFacts, IIfLikeStatementGenerator ifGenerator, SyntaxNode firstStatement, SyntaxNode secondStatement, CancellationToken cancellationToken) { // We don't support cases where the previous if statement has any else-if or else clauses. In order for that // to be mergable, the control flow would have to quit from inside every branch, which is getting a little complex. if (!ifGenerator.IsIfOrElseIf(firstStatement) || ifGenerator.GetElseIfAndElseClauses(firstStatement).Length > 0) { return(false); } if (!ifGenerator.IsIfOrElseIf(secondStatement)) { return(false); } if (!ContainEquivalentStatements(syntaxFacts, blockFacts, firstStatement, secondStatement, out var insideStatements)) { return(false); } if (insideStatements.Count == 0) { // Even though there are no statements inside, we still can't merge these into one statement // because it would change the semantics from always evaluating the second condition to short-circuiting. return(false); } else { // There are statements inside. We can merge these into one statement if // control flow can't reach the end of these statements (otherwise, it would change from running // the second 'if' in the case that both conditions are true to only running the statements once). // This will typically look like a single return, break, continue or a throw statement. var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); var controlFlow = semanticModel.AnalyzeControlFlow(insideStatements[0], insideStatements[insideStatements.Count - 1]); return(!controlFlow.EndPointIsReachable); } }
private static async Task <bool> CanBeMergedAsync( Document document, ISyntaxFactsService syntaxFacts, IIfLikeStatementGenerator ifGenerator, SyntaxNode outerIfOrElseIf, SyntaxNode innerIfStatement, CancellationToken cancellationToken) { // We can only merge this with the outer if statement if any inner else-if and else clauses are equal // to else-if and else clauses following the outer if statement because we'll be removing the inner ones. // Example of what we can merge: // if (a) // { // if (b) // Console.WriteLine(); // else // Foo(); // } // else // { // Foo(); // } if (!System.Linq.ImmutableArrayExtensions.SequenceEqual( ifGenerator.GetElseIfAndElseClauses(outerIfOrElseIf), ifGenerator.GetElseIfAndElseClauses(innerIfStatement), (a, b) => IsElseIfOrElseClauseEquivalent(syntaxFacts, ifGenerator, a, b))) { return(false); } var statements = syntaxFacts.GetStatementContainerStatements(innerIfStatement.Parent); if (statements.Count == 1) { // There are no other statements below the inner if statement. Merging is OK. return(true); } else { // There are statements below the inner if statement. We can merge if // 1. there are equivalent statements below the outer 'if', and // 2. control flow can't reach the end of these statements (otherwise, it would continue // below the outer 'if' and run the same statements twice). // This will typically look like a single return, break, continue or a throw statement. // The opposite refactoring (SplitIntoNestedIfStatements) never generates this but we support it anyway. // Example: // if (a) // { // if (b) // Console.WriteLine(); // return; // } // return; // If we have an else-if, get the topmost if statement. var outerIfStatement = ifGenerator.GetRootIfStatement(outerIfOrElseIf); // A statement should always be in a statement container, but we'll do a defensive check anyway so that // we don't crash if the helper is missing some cases or there's a new language feature it didn't account for. Debug.Assert(syntaxFacts.GetStatementContainer(outerIfStatement) is object); if (syntaxFacts.GetStatementContainer(outerIfStatement) is not { } container) { return(false); } var outerStatements = syntaxFacts.GetStatementContainerStatements(container); var outerIfStatementIndex = outerStatements.IndexOf(outerIfStatement); var remainingStatements = statements.Skip(1); var remainingOuterStatements = outerStatements.Skip(outerIfStatementIndex + 1); if (!remainingStatements.SequenceEqual(remainingOuterStatements.Take(statements.Count - 1), syntaxFacts.AreEquivalent)) { return(false); } var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); var controlFlow = semanticModel.AnalyzeControlFlow(statements[0], statements[statements.Count - 1]); return(!controlFlow.EndPointIsReachable); } }