Exemplo n.º 1
0
        private static Task <Document> ConvertSwitchExpressionToSwitchStatement(
            Document document,
            SwitchExpressionSyntax switchExpression,
            CancellationToken cancellationToken)
        {
            IEnumerable <SwitchSectionSyntax> sections = switchExpression.Arms.Select((arm, i) =>
            {
                SyntaxToken separator = switchExpression.Arms.GetSeparator(i);
                SyntaxToken semicolon = Token(SyntaxKind.SemicolonToken);

                if (!separator.IsMissing)
                {
                    semicolon = semicolon.WithTriviaFrom(separator);
                }

                PatternSyntax pattern = arm.Pattern;

                switch (pattern.Kind())
                {
                case SyntaxKind.ConstantPattern:
                    {
                        CaseSwitchLabelSyntax label = CaseSwitchLabel(
                            Token(SyntaxKind.CaseKeyword).WithLeadingTrivia(pattern.GetLeadingTrivia()),
                            ((ConstantPatternSyntax)pattern).Expression.WithoutLeadingTrivia(),
                            Token(SyntaxKind.ColonToken).WithTriviaFrom(arm.EqualsGreaterThanToken));

                        return(SwitchSection(label, CreateStatement(arm.Expression, semicolon)));
                    }

                case SyntaxKind.DiscardPattern:
                    {
                        DefaultSwitchLabelSyntax label = DefaultSwitchLabel(Token(SyntaxKind.DefaultKeyword), Token(SyntaxKind.ColonToken));

                        return(SwitchSection(label, CreateStatement(arm.Expression, semicolon)));
                    }

                default:
                    {
                        throw new InvalidOperationException();
                    }
                }
            });

            var returnStatement = (ReturnStatementSyntax)switchExpression.Parent;

            SwitchStatementSyntax switchStatement = SwitchStatement(
                switchExpression.SwitchKeyword.WithTriviaFrom(returnStatement.ReturnKeyword),
                OpenParenToken(),
                switchExpression.GoverningExpression,
                CloseParenToken().WithTrailingTrivia(switchExpression.SwitchKeyword.TrailingTrivia),
                switchExpression.OpenBraceToken,
                sections.ToSyntaxList(),
                switchExpression.CloseBraceToken);

            switchStatement = switchStatement.WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(returnStatement, switchStatement, cancellationToken));
Exemplo n.º 2
0
        private static Task <Document> ConvertSwitchExpressionToSwitchStatement(
            Document document,
            SwitchExpressionSyntax switchExpression,
            CancellationToken cancellationToken)
        {
            SeparatedSyntaxList <SwitchExpressionArmSyntax> arms = switchExpression.Arms;

            SyntaxToken[] separators = arms.GetSeparators().ToArray();

            IEnumerable <SwitchSectionSyntax> sections = arms.Select((arm, i) =>
            {
                PatternSyntax pattern       = arm.Pattern;
                ExpressionSyntax expression = arm.Expression;
                SyntaxToken semicolon       = Token(SyntaxKind.SemicolonToken);
                SyntaxToken separator       = default;

                if (i < separators.Length)
                {
                    separator = separators[i];
                }

                if (separator.IsKind(SyntaxKind.None) ||
                    separator.IsMissing)
                {
                    semicolon  = semicolon.WithTrailingTrivia(arm.GetTrailingTrivia());
                    expression = expression.WithoutTrailingTrivia();
                }
                else
                {
                    semicolon = semicolon.WithTriviaFrom(separator);
                }

                SyntaxKind kind         = pattern.Kind();
                SwitchLabelSyntax label = default;

                if (kind == SyntaxKind.ConstantPattern)
                {
                    label = CaseSwitchLabel(
                        Token(SyntaxKind.CaseKeyword).WithLeadingTrivia(pattern.GetLeadingTrivia()),
                        ((ConstantPatternSyntax)pattern).Expression.WithoutLeadingTrivia(),
                        Token(SyntaxKind.ColonToken).WithTriviaFrom(arm.EqualsGreaterThanToken));
                }
                else if (kind == SyntaxKind.DiscardPattern)
                {
                    label = DefaultSwitchLabel(Token(SyntaxKind.DefaultKeyword), Token(SyntaxKind.ColonToken));
                }
                else
                {
                    throw new InvalidOperationException();
                }

                StatementSyntax statement = CreateStatement(expression, semicolon);

                return(SwitchSection(label, statement));
            });

            var returnStatement = (ReturnStatementSyntax)switchExpression.Parent;

            SwitchStatementSyntax switchStatement = SwitchStatement(
                switchExpression.SwitchKeyword.WithTriviaFrom(returnStatement.ReturnKeyword),
                OpenParenToken(),
                switchExpression.GoverningExpression,
                CloseParenToken().WithTrailingTrivia(switchExpression.SwitchKeyword.TrailingTrivia),
                switchExpression.OpenBraceToken,
                sections.ToSyntaxList(),
                switchExpression.CloseBraceToken);

            switchStatement = switchStatement.WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(returnStatement, switchStatement, cancellationToken));