public static StatementSyntax Rewrite(
                SwitchStatementSyntax switchStatement,
                SemanticModel model,
                ITypeSymbol declaratorToRemoveTypeOpt,
                SyntaxKind nodeToGenerate,
                bool shouldMoveNextStatementToSwitchExpression,
                bool generateDeclaration
                )
            {
                if (switchStatement.ContainsDirectives)
                {
                    // Do not rewrite statements with preprocessor directives
                    return(switchStatement);
                }

                var rewriter = new Rewriter(
                    isAllThrowStatements: nodeToGenerate == SyntaxKind.ThrowStatement
                    );

                // Rewrite the switch statement as a switch expression.
                var switchExpression = rewriter.RewriteSwitchStatement(
                    switchStatement,
                    model,
                    allowMoveNextStatementToSwitchExpression: shouldMoveNextStatementToSwitchExpression
                    );

                // Generate the final statement to wrap the switch expression, e.g. a "return" or an assignment.
                return(rewriter.GetFinalStatement(
                           switchExpression,
                           switchStatement.SwitchKeyword.LeadingTrivia,
                           declaratorToRemoveTypeOpt,
                           nodeToGenerate,
                           generateDeclaration
                           ));
            }
コード例 #2
0
            public static StatementSyntax Rewrite(
                SwitchStatementSyntax switchStatement,
                SyntaxKind nodeToGenerate, bool shouldMoveNextStatementToSwitchExpression, bool generateDeclaration)
            {
                var rewriter = new Rewriter(isAllThrowStatements: nodeToGenerate == SyntaxKind.ThrowStatement);

                // Rewrite the switch statement as a switch expression.
                var switchExpression = rewriter.RewriteSwitchStatement(switchStatement,
                                                                       allowMoveNextStatementToSwitchExpression: shouldMoveNextStatementToSwitchExpression);

                // Generate the final statement to wrap the switch expression, e.g. a "return" or an assignment.
                return(rewriter.GetFinalStatement(switchExpression,
                                                  switchStatement.SwitchKeyword.LeadingTrivia, nodeToGenerate, generateDeclaration));
            }
            public static StatementSyntax Rewrite(
                SwitchStatementSyntax switchStatement, SemanticModel semanticModel, SyntaxEditor editor,
                SyntaxKind nodeToGenerate, bool shouldMoveNextStatementToSwitchExpression)
            {
                var rewriter = new Rewriter(isAllThrowStatements: nodeToGenerate == SyntaxKind.ThrowStatement);

                // Rewrite the switch statement as a switch expression.
                var switchExpression = rewriter.RewriteSwitchStatement(switchStatement,
                                                                       allowMoveNextStatementToSwitchExpression: shouldMoveNextStatementToSwitchExpression);

                // Only on simple assignments we attempt to remove variable declarators.
                var isSimpleAssignment  = nodeToGenerate == SyntaxKind.SimpleAssignmentExpression;
                var generateDeclaration = isSimpleAssignment && rewriter.TryRemoveVariableDeclarators(switchStatement, semanticModel, editor);

                // Generate the final statement to wrap the switch expression, e.g. a "return" or an assignment.
                return(rewriter.GetFinalStatement(switchExpression,
                                                  switchStatement.SwitchKeyword.LeadingTrivia, nodeToGenerate, generateDeclaration));
            }