Exemple #1
0
        private static ConversionOperatorDeclarationSyntax UseExpressionBodyIfDesired(
            CodeGenerationOptions options,
            ConversionOperatorDeclarationSyntax declaration,
            ParseOptions parseOptions
            )
        {
            if (declaration.ExpressionBody == null)
            {
                var expressionBodyPreference =
                    options.Options.GetOption(
                        CSharpCodeStyleOptions.PreferExpressionBodiedOperators
                        ).Value;

                if (
                    declaration.Body.TryConvertToArrowExpressionBody(
                        declaration.Kind(),
                        parseOptions,
                        expressionBodyPreference,
                        out var expressionBody,
                        out var semicolonToken
                        )
                    )
                {
                    return(declaration
                           .WithBody(null)
                           .WithExpressionBody(expressionBody)
                           .WithSemicolonToken(semicolonToken));
                }
            }

            return(declaration);
        }
Exemple #2
0
        public static ConversionOperatorDeclarationSyntax ToStatementBody(ConversionOperatorDeclarationSyntax method)
        {
            var body = method.ExpressionBody.Expression.WithLeadingTrivia(SyntaxFactory.Space);

            return(method.WithBody(SyntaxFactory.Block((StatementSyntax)SyntaxFactory.ReturnStatement(body)))
                   .WithExpressionBody(null)
                   .WithSemicolonToken(SyntaxFactory.MissingToken(SyntaxKind.SemicolonToken))
                   .WithTrailingTrivia(method.GetTrailingTrivia()));
        }
Exemple #3
0
        public override SyntaxNode VisitConversionOperatorDeclaration(ConversionOperatorDeclarationSyntax node)
        {
            if (node.Body == null)
            {
                return(node);
            }

            BlockSyntax block = (BlockSyntax)SyntaxFactory.ParseStatement(GetDefaultMessage());

            return(node.WithBody(block));
        }
        private static ConversionOperatorDeclarationSyntax UseExpressionBodyIfDesired(
            CSharpCodeGenerationOptions options, ConversionOperatorDeclarationSyntax declaration)
        {
            if (declaration.ExpressionBody == null)
            {
                if (declaration.Body?.TryConvertToArrowExpressionBody(
                        declaration.Kind(), options.Preferences.LanguageVersion, options.Preferences.PreferExpressionBodiedOperators,
                        out var expressionBody, out var semicolonToken) == true)
                {
                    return(declaration.WithBody(null)
                           .WithExpressionBody(expressionBody)
                           .WithSemicolonToken(semicolonToken));
                }
            }

            return(declaration);
        }
        private async Task <Document> HandleConversionOperatorDeclaration(ConversionOperatorDeclarationSyntax declaration, Document document, CancellationToken cancellationToken)
        {
            var returnStatement = SyntaxFactory.ReturnStatement(
                returnKeyword: SyntaxFactory.Token(SyntaxKind.ReturnKeyword),
                expression: declaration.ExpressionBody.Expression,
                semicolonToken: declaration.SemicolonToken);

            var newDeclaration = declaration
                                 .WithBody(SyntaxFactory.Block(returnStatement))
                                 .WithExpressionBody(null)
                                 .WithSemicolonToken(default(SyntaxToken))
                                 .WithAdditionalAnnotations(Formatter.Annotation);

            var oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            var newRoot = oldRoot.ReplaceNode(declaration, newDeclaration);

            return(document.WithSyntaxRoot(newRoot));
        }
        private static ConversionOperatorDeclarationSyntax UseExpressionBodyIfDesired(
            Workspace workspace, ConversionOperatorDeclarationSyntax declaration, ParseOptions options)
        {
            if (declaration.ExpressionBody == null)
            {
                var preferExpressionBody = workspace.Options.GetOption(CSharpCodeStyleOptions.PreferExpressionBodiedOperators).Value;
                if (preferExpressionBody)
                {
                    if (declaration.Body.TryConvertToExpressionBody(
                            options, out var expressionBody, out var semicolonToken))
                    {
                        return declaration.WithBody(null)
                                          .WithExpressionBody(expressionBody)
                                          .WithSemicolonToken(semicolonToken);
                    }
                }
            }

            return declaration;
        }
        private async Task<Document> HandleConversionOperatorDeclaration(ConversionOperatorDeclarationSyntax declaration, Document document, CancellationToken cancellationToken)
        {
            var returnStatement = SyntaxFactory.ReturnStatement(
                returnKeyword: SyntaxFactory.Token(SyntaxKind.ReturnKeyword),
                expression: declaration.ExpressionBody.Expression,
                semicolonToken: declaration.SemicolonToken);

            var newDeclaration = declaration
                .WithBody(SyntaxFactory.Block(returnStatement))
                .WithExpressionBody(null)
                .WithSemicolonToken(default(SyntaxToken))
                .WithAdditionalAnnotations(Formatter.Annotation);

            var oldRoot = await document.GetSyntaxRootAsync(cancellationToken);
            var newRoot = oldRoot.ReplaceNode(declaration, newDeclaration);

            return document.WithSyntaxRoot(newRoot);
        }
        private static ConversionOperatorDeclarationSyntax UseExpressionBodyIfDesired(
            Workspace workspace, ConversionOperatorDeclarationSyntax declaration, ParseOptions options)
        {
            if (declaration.ExpressionBody == null)
            {
                var preferExpressionBody = workspace.Options.GetOption(CSharpCodeStyleOptions.PreferExpressionBodiedOperators).Value;
                if (preferExpressionBody)
                {
                    var expressionBody = declaration.Body.TryConvertToExpressionBody(options);
                    if (expressionBody != null)
                    {
                        return declaration.WithBody(null)
                                          .WithExpressionBody(expressionBody)
                                          .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
                    }
                }
            }

            return declaration;
        }