private static void AnalyzeBody(SyntaxNodeAnalysisContext context, BlockSyntax body)
        {
            ExpressionSyntax expression = UseExpressionBodiedMemberRefactoring.GetReturnExpression(body);

            if (expression != null)
            {
                AnalyzeExpression(context, body, expression);
            }
        }
        private void AnalyzeConstructorDeclaration(SyntaxNodeAnalysisContext context)
        {
            var declaration = (ConstructorDeclarationSyntax)context.Node;

            if (declaration.ExpressionBody == null)
            {
                BlockSyntax body = declaration.Body;

                ExpressionSyntax expression = UseExpressionBodiedMemberRefactoring.GetExpression(body);

                if (expression != null)
                {
                    AnalyzeExpression(context, body, expression);
                }
            }
        }
        private void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
        {
            var method = (MethodDeclarationSyntax)context.Node;

            if (method.ExpressionBody == null)
            {
                BlockSyntax body = method.Body;

                ExpressionSyntax expression = UseExpressionBodiedMemberRefactoring.GetExpression(body);

                if (expression != null)
                {
                    AnalyzeExpression(context, body, expression);
                }
            }
        }
        private void AnalyzeLocalFunctionStatement(SyntaxNodeAnalysisContext context)
        {
            var localFunctionStatement = (LocalFunctionStatementSyntax)context.Node;

            if (localFunctionStatement.ExpressionBody == null)
            {
                BlockSyntax body = localFunctionStatement.Body;

                ExpressionSyntax expression = UseExpressionBodiedMemberRefactoring.GetExpression(body);

                if (expression != null)
                {
                    AnalyzeExpression(context, body, expression);
                }
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out SyntaxNode node, predicate: f => CSharpFacts.CanHaveExpressionBody(f.Kind())))
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Use expression-bodied member",
                cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, node, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.UseExpressionBodiedMember));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
예제 #6
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            SyntaxNode node = root
                              .FindNode(context.Span, getInnermostNodeForTie: true)?
                              .FirstAncestorOrSelf <SyntaxNode>(f => Predicate(f));

            Debug.Assert(node != null, "");

            if (node != null)
            {
                CodeAction codeAction = CodeAction.Create(
                    "Use expression-bodied member",
                    cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, node, cancellationToken),
                    DiagnosticIdentifiers.UseExpressionBodiedMember + EquivalenceKeySuffix);

                context.RegisterCodeFix(codeAction, context.Diagnostics);
            }
        }
예제 #7
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            MemberDeclarationSyntax declaration = root
                                                  .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                  .FirstAncestorOrSelf <MemberDeclarationSyntax>();

            if (declaration == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Use expression-bodied member",
                cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, declaration, cancellationToken),
                DiagnosticIdentifiers.UseExpressionBodiedMember + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        private void AnalyzeAccessorDeclaration(SyntaxNodeAnalysisContext context)
        {
            var accessor = (AccessorDeclarationSyntax)context.Node;

            if (accessor.ExpressionBody == null &&
                !accessor.AttributeLists.Any())
            {
                BlockSyntax body = accessor.Body;

                ExpressionSyntax expression = UseExpressionBodiedMemberRefactoring.GetExpression(body);

                if (expression != null &&
                    expression.IsSingleLine())
                {
                    var accessorList = accessor.Parent as AccessorListSyntax;

                    if (accessorList != null)
                    {
                        SyntaxList <AccessorDeclarationSyntax> accessors = accessorList.Accessors;

                        if (accessors.Count == 1 &&
                            accessors.First().IsKind(SyntaxKind.GetAccessorDeclaration))
                        {
                            if (accessorList.DescendantTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                            {
                                ReportDiagnostic(context, accessorList, expression);
                                context.ReportToken(FadeOutDescriptor, accessor.Keyword);
                                context.ReportBraces(FadeOutDescriptor, body);
                            }

                            return;
                        }
                    }

                    if (accessor.DescendantTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        ReportDiagnostic(context, body, expression);
                    }
                }
            }
        }