private static void FadeOut(SyntaxNodeAnalysisContext context, PropertyDeclarationSyntax property)
        {
            DiagnosticDescriptor descriptor = DiagnosticDescriptors.ReplacePropertyWithAutoImplementedPropertyFadeOut;

            if (property.ExpressionBody != null)
            {
                context.FadeOutNode(descriptor, property.ExpressionBody);
            }
            else
            {
                AccessorDeclarationSyntax getter = property.Getter();

                if (getter != null)
                {
                    context.FadeOutNode(descriptor, getter.Body);
                }

                AccessorDeclarationSyntax setter = property.Setter();

                if (setter != null)
                {
                    context.FadeOutNode(descriptor, setter.Body);
                }
            }
        }
Пример #2
0
        private static ISymbol GetFieldSymbol(PropertyDeclarationSyntax property, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            ArrowExpressionClauseSyntax expressionBody = property.ExpressionBody;

            if (expressionBody != null)
            {
                return(semanticModel.GetSymbol(expressionBody.Expression, cancellationToken));
            }
            else
            {
                AccessorDeclarationSyntax getter = property.Getter();

                BlockSyntax body = getter.Body;

                if (body != null)
                {
                    var returnStatement = (ReturnStatementSyntax)body.Statements[0];

                    return(semanticModel.GetSymbol(returnStatement.Expression, cancellationToken));
                }
                else
                {
                    return(semanticModel.GetSymbol(getter.ExpressionBody.Expression, cancellationToken));
                }
            }
        }
        private static SyntaxToken GetPropertyIdentifier(PropertyDeclarationSyntax propertyDeclaration)
        {
            if (propertyDeclaration.ExpressionBody != null)
            {
                ExpressionSyntax expression = propertyDeclaration.ExpressionBody?.Expression;

                if (expression?.IsKind(SyntaxKind.IdentifierName) == true)
                {
                    return(((IdentifierNameSyntax)expression).Identifier);
                }
            }
            else
            {
                AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

                if (getter?.Body != null)
                {
                    var returnStatement = (ReturnStatementSyntax)getter.Body.Statements[0];

                    return(((IdentifierNameSyntax)returnStatement.Expression).Identifier);
                }
            }

            return(propertyDeclaration.Identifier);
        }
Пример #4
0
        private static MemberDeclarationSyntax MakeAbstract(PropertyDeclarationSyntax propertyDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList();

            if (propertyDeclaration.ExpressionBody != null)
            {
                accessorList = accessorList
                               .AddAccessors(AutoGetAccessorDeclaration());
            }
            else
            {
                AccessorDeclarationSyntax getter = propertyDeclaration.Getter();
                if (getter != null)
                {
                    accessorList = accessorList.AddAccessors(getter
                                                             .WithBody(null)
                                                             .WithSemicolonToken(SemicolonToken()));
                }

                AccessorDeclarationSyntax setter = propertyDeclaration.Setter();
                if (setter != null)
                {
                    accessorList = accessorList.AddAccessors(setter
                                                             .WithBody(null)
                                                             .WithSemicolonToken(SemicolonToken()));
                }
            }

            propertyDeclaration = propertyDeclaration
                                  .WithExpressionBody(null)
                                  .WithSemicolonToken(default(SyntaxToken))
                                  .WithAccessorList(accessorList);

            return(UpdateModifiers(propertyDeclaration, propertyDeclaration.Modifiers));
        }
Пример #5
0
        public static Task <Document> RefactorAsync(
            Document document,
            PropertyDeclarationSyntax propertyDeclaration,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            AccessorListSyntax accessorList = AccessorList();

            if (propertyDeclaration.ExpressionBody != null)
            {
                accessorList = accessorList
                               .AddAccessors(AutoGetAccessorDeclaration());
            }
            else
            {
                AccessorDeclarationSyntax getter = propertyDeclaration.Getter();
                if (getter != null)
                {
                    if (SyntaxAccessibility.GetExplicitAccessibility(getter) == Accessibility.Private)
                    {
                        getter = SyntaxAccessibility.WithExplicitAccessibility(getter, Accessibility.Protected);
                    }

                    accessorList = accessorList.AddAccessors(getter
                                                             .WithBody(null)
                                                             .WithSemicolonToken(SemicolonToken()));
                }

                AccessorDeclarationSyntax setter = propertyDeclaration.Setter();
                if (setter != null)
                {
                    if (SyntaxAccessibility.GetExplicitAccessibility(setter) == Accessibility.Private)
                    {
                        setter = SyntaxAccessibility.WithExplicitAccessibility(setter, Accessibility.Protected);
                    }

                    accessorList = accessorList.AddAccessors(setter
                                                             .WithBody(null)
                                                             .WithSemicolonToken(SemicolonToken()));
                }
            }

            PropertyDeclarationSyntax newNode = propertyDeclaration
                                                .WithExpressionBody(null)
                                                .WithSemicolonToken(default(SyntaxToken))
                                                .WithAccessorList(accessorList)
                                                .InsertModifier(SyntaxKind.AbstractKeyword)
                                                .RemoveModifier(SyntaxKind.VirtualKeyword)
                                                .WithTriviaFrom(propertyDeclaration)
                                                .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(propertyDeclaration, newNode, cancellationToken));
        }
        private static ISymbol GetFieldSymbol(PropertyDeclarationSyntax property, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (property.ExpressionBody != null)
            {
                return(semanticModel.GetSymbol(property.ExpressionBody.Expression, cancellationToken));
            }
            else
            {
                var returnStatement = (ReturnStatementSyntax)property.Getter().Body.Statements[0];

                return(semanticModel.GetSymbol(returnStatement.Expression, cancellationToken));
            }
        }
        public static bool IsGetOnlyAutoProperty(this IPropertySymbol property, PropertyDeclarationSyntax propertyDeclaration)
        {
            Contract.Requires(property != null);
            Contract.Requires(propertyDeclaration != null);

            var getter = propertyDeclaration.Getter();

            if (getter == null)
            {
                return(false);
            }

            return(property.IsReadOnly && getter.SemicolonToken.IsKind(SyntaxKind.SemicolonToken));
        }
        private static MemberDeclarationSyntax MakeAbstract(PropertyDeclarationSyntax propertyDeclaration)
        {
            if (propertyDeclaration == null)
            {
                throw new ArgumentNullException(nameof(propertyDeclaration));
            }

            AccessorListSyntax accessorList = AccessorList();

            if (propertyDeclaration.ExpressionBody != null)
            {
                accessorList = accessorList
                               .AddAccessors(
                    AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                    .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
            }
            else
            {
                AccessorDeclarationSyntax getter = propertyDeclaration.Getter();
                if (getter != null)
                {
                    accessorList = accessorList.AddAccessors(getter
                                                             .WithBody(null)
                                                             .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
                }

                AccessorDeclarationSyntax setter = propertyDeclaration.Setter();
                if (setter != null)
                {
                    accessorList = accessorList.AddAccessors(setter
                                                             .WithBody(null)
                                                             .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
                }
            }

            SyntaxTokenList modifiers = propertyDeclaration.Modifiers;

            if (!modifiers.Contains(SyntaxKind.AbstractKeyword))
            {
                modifiers = modifiers.Add(Token(SyntaxKind.AbstractKeyword));
            }

            return(propertyDeclaration
                   .WithExpressionBody(null)
                   .WithSemicolonToken(Token(SyntaxKind.None))
                   .WithAccessorList(accessorList)
                   .WithModifiers(modifiers));
        }
        public static void Analyze(SyntaxNodeAnalysisContext context, PropertyDeclarationSyntax property)
        {
            IFieldSymbol fieldSymbol = GetBackingFieldSymbol(property, context.SemanticModel, context.CancellationToken);

            if (fieldSymbol != null)
            {
                var declarator = (VariableDeclaratorSyntax)fieldSymbol.DeclaringSyntaxReferences[0].GetSyntax(context.CancellationToken);

                if (declarator.SyntaxTree.Equals(property.SyntaxTree))
                {
                    IPropertySymbol propertySymbol = context
                                                     .SemanticModel
                                                     .GetDeclaredSymbol(property, context.CancellationToken);

                    if (propertySymbol != null &&
                        propertySymbol.IsStatic == fieldSymbol.IsStatic &&
                        propertySymbol.Type == fieldSymbol.Type &&
                        propertySymbol.ContainingType?.Equals(fieldSymbol.ContainingType) == true &&
                        CheckPreprocessorDirectives(property, declarator))
                    {
                        context.ReportDiagnostic(
                            DiagnosticDescriptors.UseAutoImplementedProperty,
                            property);

                        if (property.ExpressionBody != null)
                        {
                            context.ReportNode(FadeOutDescriptor, property.ExpressionBody);
                        }
                        else
                        {
                            AccessorDeclarationSyntax getter = property.Getter();

                            if (getter != null)
                            {
                                FadeOutBodyOrExpressionBody(context, getter);
                            }

                            AccessorDeclarationSyntax setter = property.Setter();

                            if (setter != null)
                            {
                                FadeOutBodyOrExpressionBody(context, setter);
                            }
                        }
                    }
                }
            }
        }
        private static bool CanPropertyBeAssignedFromConstructor(
            PropertyDeclarationSyntax propertyDeclaration,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            ISymbol symbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, cancellationToken);

            if (symbol == null)
            {
                return(false);
            }

            if (symbol.IsStatic)
            {
                return(false);
            }

            if (!propertyDeclaration.IsParentKind(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration))
            {
                return(false);
            }

            ArrowExpressionClauseSyntax expressionBody = propertyDeclaration.ExpressionBody;

            if (expressionBody != null)
            {
                ExpressionSyntax expression = expressionBody?.Expression;

                if (expression != null)
                {
                    return(GetBackingFieldSymbol(expression, semanticModel, cancellationToken) != null);
                }
            }
            else
            {
                AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

                if (getter != null)
                {
                    return(CanPropertyBeAssignedFromConstructor(getter, semanticModel, cancellationToken));
                }
            }

            return(false);
        }
Пример #11
0
        private static PropertyDeclarationSyntax ExpandPropertyAndAddBackingField(PropertyDeclarationSyntax propertyDeclaration, string name)
        {
            AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

            if (getter != null)
            {
                AccessorDeclarationSyntax newGetter = getter
                                                      .WithBody(
                    Block(
                        SingletonList <StatementSyntax>(
                            ReturnStatement(IdentifierName(name)))))
                                                      .WithSemicolonToken(Token(SyntaxKind.None));

                propertyDeclaration = propertyDeclaration
                                      .WithInitializer(null)
                                      .WithAccessorList(
                    propertyDeclaration.AccessorList.ReplaceNode(getter, newGetter));
            }

            AccessorDeclarationSyntax setter = propertyDeclaration.Setter();

            if (setter != null)
            {
                AccessorDeclarationSyntax newSetter = setter
                                                      .WithBody(
                    Block(
                        SingletonList <StatementSyntax>(
                            ExpressionStatement(
                                AssignmentExpression(
                                    SyntaxKind.SimpleAssignmentExpression,
                                    IdentifierName(name),
                                    IdentifierName("value"))))))
                                                      .WithSemicolonToken(Token(SyntaxKind.None));

                propertyDeclaration = propertyDeclaration
                                      .WithAccessorList(
                    propertyDeclaration.AccessorList.ReplaceNode(setter, newSetter));
            }

            AccessorListSyntax accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(propertyDeclaration.AccessorList)
                                              .WithCloseBraceToken(propertyDeclaration.AccessorList.CloseBraceToken.WithLeadingTrivia(SyntaxHelper.NewLine));

            return(propertyDeclaration
                   .WithAccessorList(accessorList));
        }
Пример #12
0
        public static MethodDeclarationSyntax ToMethodDeclaration(PropertyDeclarationSyntax property)
        {
            AccessorDeclarationSyntax getter = property.Getter();

            BlockSyntax getterBody = getter.Body;

            BlockSyntax methodBody;

            if (getterBody != null)
            {
                methodBody = Block(getterBody.Statements);
            }
            else
            {
                ArrowExpressionClauseSyntax getterExpressionBody = getter.ExpressionBody;

                if (getterExpressionBody != null)
                {
                    methodBody = Block(ReturnStatement(getterExpressionBody.Expression));
                }
                else
                {
                    methodBody = Block(ReturnStatement(property.Initializer.Value));
                }
            }

            methodBody = methodBody.WithTrailingTrivia(property.GetTrailingTrivia());

            MethodDeclarationSyntax method = MethodDeclaration(
                property.AttributeLists,
                property.Modifiers,
                property.Type,
                property.ExplicitInterfaceSpecifier,
                Identifier(GetMethodName(property)).WithLeadingTrivia(property.Identifier.LeadingTrivia),
                default(TypeParameterListSyntax),
                ParameterList().WithTrailingTrivia(property.Identifier.TrailingTrivia),
                default(SyntaxList <TypeParameterConstraintClauseSyntax>),
                methodBody,
                default(ArrowExpressionClauseSyntax));

            return(method
                   .WithLeadingTrivia(property.GetLeadingTrivia())
                   .WithFormatterAnnotation());
        }
        private static async Task <bool> CanPropertyBeAssignedFromConstructorAsync(
            RefactoringContext context,
            PropertyDeclarationSyntax propertyDeclaration)
        {
            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            ISymbol symbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken);

            if (symbol != null &&
                !symbol.IsStatic &&
                propertyDeclaration.Parent != null &&
                propertyDeclaration.Parent.IsKind(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration))
            {
                if (propertyDeclaration.ExpressionBody != null)
                {
                    ExpressionSyntax expression = propertyDeclaration.ExpressionBody?.Expression;

                    if (expression != null)
                    {
                        return(GetBackingFieldSymbol(expression, semanticModel, context.CancellationToken) != null);
                    }
                }
                else
                {
                    AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

                    if (getter != null)
                    {
                        if (getter.Body == null)
                        {
                            return(true);
                        }

                        if (getter.Body.Statements.Count == 1)
                        {
                            return(GetBackingFieldSymbol(getter.Body.Statements[0], semanticModel, context.CancellationToken) != null);
                        }
                    }
                }
            }

            return(false);
        }
        public static async Task <Document> RefactorAsync(
            Document document,
            PropertyDeclarationSyntax propertyDeclaration,
            bool prefixIdentifierWithUnderscore = true,
            CancellationToken cancellationToken = default)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            string fieldName = StringUtility.ToCamelCase(
                propertyDeclaration.Identifier.ValueText,
                prefixWithUnderscore: prefixIdentifierWithUnderscore);

            fieldName = NameGenerator.Default.EnsureUniqueName(
                fieldName,
                semanticModel,
                propertyDeclaration.SpanStart);

            FieldDeclarationSyntax fieldDeclaration = FieldDeclaration(
                (propertyDeclaration.Modifiers.Contains(SyntaxKind.StaticKeyword)) ? Modifiers.Private_Static() : Modifiers.Private(),
                propertyDeclaration.Type,
                fieldName,
                propertyDeclaration.Initializer)
                                                      .WithFormatterAnnotation();

            IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, cancellationToken);

            PropertyDeclarationSyntax newPropertyDeclaration = propertyDeclaration;

            AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

            if (getter != null)
            {
                AccessorDeclarationSyntax newGetter = getter.Update(
                    getter.AttributeLists,
                    getter.Modifiers,
                    getter.Keyword,
                    Block(ReturnStatement(IdentifierName(fieldName))),
                    default(SyntaxToken));

                newPropertyDeclaration = newPropertyDeclaration
                                         .ReplaceAccessor(getter, newGetter.RemoveWhitespace())
                                         .WithInitializer(null)
                                         .WithSemicolonToken(default);
        private static IFieldSymbol GetBackingFieldSymbol(
            PropertyDeclarationSyntax property,
            SemanticModel semanticModel,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ArrowExpressionClauseSyntax expressionBody = property.ExpressionBody;

            if (expressionBody != null)
            {
                NameSyntax identifier = GetIdentifierFromExpression(expressionBody.Expression);

                if (identifier != null)
                {
                    return(GetBackingFieldSymbol(identifier, semanticModel, cancellationToken));
                }
            }
            else
            {
                AccessorDeclarationSyntax getter = property.Getter();

                if (getter != null)
                {
                    AccessorDeclarationSyntax setter = property.Setter();

                    if (setter != null)
                    {
                        return(GetBackingFieldSymbol(getter, setter, semanticModel, cancellationToken));
                    }
                    else
                    {
                        NameSyntax identifier = GetIdentifierFromGetter(getter);

                        if (identifier != null)
                        {
                            return(GetBackingFieldSymbol(identifier, semanticModel, cancellationToken));
                        }
                    }
                }
            }

            return(null);
        }
        private static SyntaxToken GetPropertyIdentifier(PropertyDeclarationSyntax propertyDeclaration)
        {
            ArrowExpressionClauseSyntax expressionBody = propertyDeclaration.ExpressionBody;

            if (expressionBody != null)
            {
                ExpressionSyntax expression = expressionBody.Expression;

                if (expression?.Kind() == SyntaxKind.IdentifierName)
                {
                    return(((IdentifierNameSyntax)expression).Identifier);
                }
            }
            else
            {
                AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

                if (getter != null)
                {
                    BlockSyntax getterBody = getter.Body;

                    if (getterBody != null)
                    {
                        var returnStatement = (ReturnStatementSyntax)getterBody.Statements[0];

                        return(GetIdentifier(returnStatement.Expression));
                    }
                    else
                    {
                        ArrowExpressionClauseSyntax getterExpressionBody = getter.ExpressionBody;

                        if (getterExpressionBody != null)
                        {
                            return(GetIdentifier(getterExpressionBody.Expression));
                        }
                    }
                }
            }

            return(propertyDeclaration.Identifier);
        }
        private static bool CanBeAssignedFromConstructor(
            PropertyDeclarationSyntax propertyDeclaration,
            CodeRefactoringContext context,
            SemanticModel semanticModel)
        {
            ISymbol symbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken);

            if (symbol == null ||
                symbol.IsStatic ||
                propertyDeclaration.Parent == null ||
                !propertyDeclaration.Parent.IsAnyKind(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration))
            {
                return(false);
            }

            AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

            if (getter != null)
            {
                if (getter.Body == null)
                {
                    return(true);
                }

                if (getter.Body.Statements.Count == 1)
                {
                    return(GetBackingFieldSymbol(getter.Body.Statements[0], semanticModel, context.CancellationToken) != null);
                }
            }
            else
            {
                ExpressionSyntax expression = propertyDeclaration.ExpressionBody?.Expression;

                if (expression != null)
                {
                    return(GetBackingFieldSymbol(expression, semanticModel, context.CancellationToken) != null);
                }
            }

            return(false);
        }
Пример #18
0
        private static PropertyDeclarationSyntax ExpandAccessors(
            Document document,
            PropertyDeclarationSyntax propertyDeclaration,
            IPropertySymbol propertySymbol,
            string fieldName,
            SemanticModel semanticModel)
        {
            AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

            if (getter != null)
            {
                AccessorDeclarationSyntax newGetter = getter.Update(
                    getter.AttributeLists,
                    getter.Modifiers,
                    getter.Keyword,
                    Block(ReturnStatement(IdentifierName(fieldName))),
                    default(SyntaxToken));

                propertyDeclaration = propertyDeclaration
                                      .ReplaceNode(getter, newGetter.RemoveWhitespace())
                                      .WithInitializer(null)
                                      .WithSemicolonToken(default);
        private static IFieldSymbol GetBackingFieldSymbol(SyntaxNodeAnalysisContext context, PropertyDeclarationSyntax property)
        {
            if (property.ExpressionBody != null)
            {
                NameSyntax identifier = GetIdentifierFromExpression(property.ExpressionBody.Expression);

                if (identifier != null)
                {
                    return(GetBackingFieldSymbol(context, identifier));
                }
            }
            else
            {
                AccessorDeclarationSyntax getter = property.Getter();

                if (getter != null)
                {
                    AccessorDeclarationSyntax setter = property.Setter();

                    if (setter != null)
                    {
                        return(GetBackingFieldSymbol(context, getter, setter));
                    }
                    else
                    {
                        NameSyntax identifier = GetIdentifierFromGetter(getter);

                        if (identifier != null)
                        {
                            return(GetBackingFieldSymbol(context, identifier));
                        }
                    }
                }
            }

            return(null);
        }
Пример #20
0
        private static PropertyDeclarationSyntax ExpandPropertyAndAddBackingField(PropertyDeclarationSyntax propertyDeclaration, string name)
        {
            AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

            if (getter != null)
            {
                AccessorDeclarationSyntax newGetter = getter
                                                      .WithBody(Block(ReturnStatement(IdentifierName(name))))
                                                      .WithSemicolonToken(default(SyntaxToken));

                propertyDeclaration = propertyDeclaration
                                      .ReplaceNode(getter, newGetter)
                                      .WithInitializer(null)
                                      .WithSemicolonToken(default(SyntaxToken));
            }

            AccessorDeclarationSyntax setter = propertyDeclaration.Setter();

            if (setter != null)
            {
                AccessorDeclarationSyntax newSetter = setter
                                                      .WithBody(Block(
                                                                    SimpleAssignmentStatement(
                                                                        IdentifierName(name),
                                                                        IdentifierName("value"))))
                                                      .WithSemicolonToken(default(SyntaxToken));

                propertyDeclaration = propertyDeclaration.ReplaceNode(setter, newSetter);
            }

            AccessorListSyntax accessorList = propertyDeclaration.AccessorList
                                              .RemoveWhitespace()
                                              .WithCloseBraceToken(propertyDeclaration.AccessorList.CloseBraceToken.WithLeadingTrivia(NewLine()));

            return(propertyDeclaration
                   .WithAccessorList(accessorList));
        }
Пример #21
0
        private static async Task <bool> CanPropertyBeAssignedFromConstructorAsync(
            RefactoringContext context,
            PropertyDeclarationSyntax propertyDeclaration)
        {
            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            CancellationToken cancellationToken = context.CancellationToken;

            ISymbol symbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, cancellationToken);

            if (symbol?.IsStatic == false &&
                propertyDeclaration.IsParentKind(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration))
            {
                ArrowExpressionClauseSyntax expressionBody = propertyDeclaration.ExpressionBody;

                if (expressionBody != null)
                {
                    ExpressionSyntax expression = expressionBody?.Expression;

                    if (expression != null)
                    {
                        return(GetBackingFieldSymbol(expression, semanticModel, cancellationToken) != null);
                    }
                }
                else
                {
                    AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

                    if (getter != null)
                    {
                        return(CanPropertyBeAssignedFromConstructor(getter, semanticModel, cancellationToken));
                    }
                }
            }

            return(false);
        }
Пример #22
0
        public static async Task <Document> ConvertToMethodAsync(
            Document document,
            PropertyDeclarationSyntax propertyDeclaration,
            CancellationToken cancellationToken)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (propertyDeclaration == null)
            {
                throw new ArgumentNullException(nameof(propertyDeclaration));
            }

            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            MethodDeclarationSyntax methodDeclaration = MethodDeclaration(
                propertyDeclaration.AttributeLists,
                propertyDeclaration.Modifiers,
                propertyDeclaration.Type,
                propertyDeclaration.ExplicitInterfaceSpecifier,
                propertyDeclaration.Identifier.WithTrailingTrivia(),
                null,
                ParameterList(SeparatedList <ParameterSyntax>()),
                List <TypeParameterConstraintClauseSyntax>(),
                Block(propertyDeclaration.Getter().Body?.Statements),
                null);

            methodDeclaration = methodDeclaration
                                .WithTriviaFrom(propertyDeclaration)
                                .WithAdditionalAnnotations(Formatter.Annotation);

            SyntaxNode newRoot = oldRoot.ReplaceNode(propertyDeclaration, methodDeclaration);

            return(document.WithSyntaxRoot(newRoot));
        }
        private static MemberDeclarationSyntax MakeAbstract(PropertyDeclarationSyntax propertyDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList();

            if (propertyDeclaration.ExpressionBody != null)
            {
                accessorList = accessorList
                               .AddAccessors(
                    AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                    .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
            }
            else
            {
                AccessorDeclarationSyntax getter = propertyDeclaration.Getter();
                if (getter != null)
                {
                    accessorList = accessorList.AddAccessors(getter
                                                             .WithBody(null)
                                                             .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
                }

                AccessorDeclarationSyntax setter = propertyDeclaration.Setter();
                if (setter != null)
                {
                    accessorList = accessorList.AddAccessors(setter
                                                             .WithBody(null)
                                                             .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
                }
            }

            return(propertyDeclaration
                   .WithExpressionBody(null)
                   .WithSemicolonToken(Token(SyntaxKind.None))
                   .WithAccessorList(accessorList)
                   .WithModifiers(CreateModifiers(propertyDeclaration.Modifiers)));
        }