Ejemplo n.º 1
0
        internal static bool AssignsValueToBackingField(AccessorDeclarationSyntax setter, out AssignmentExpressionSyntax assignment)
        {
            using (var pooled = AssignmentWalker.Create(setter))
            {
                foreach (var a in pooled.Item.Assignments)
                {
                    if ((a.Right as IdentifierNameSyntax)?.Identifier.ValueText != "value")
                    {
                        continue;
                    }

                    if (a.Left is IdentifierNameSyntax)
                    {
                        assignment = a;
                        return(true);
                    }

                    var memberAccess = a.Left as MemberAccessExpressionSyntax;
                    if (memberAccess?.Expression is ThisExpressionSyntax &&
                        memberAccess.Name is IdentifierNameSyntax)
                    {
                        assignment = a;
                        return(true);
                    }
                }
            }

            assignment = null;
            return(false);
        }
Ejemplo n.º 2
0
        internal static bool TryGetBackingField(PropertyDeclarationSyntax property, out IdentifierNameSyntax field, out FieldDeclarationSyntax fieldDeclaration)
        {
            field            = null;
            fieldDeclaration = null;
            if (property == null)
            {
                return(false);
            }

            AccessorDeclarationSyntax setter;

            if (property.TryGetSetAccessorDeclaration(out setter) &&
                setter.Body != null)
            {
                using (var pooled = AssignmentWalker.Create(setter))
                {
                    if (pooled.Item.Assignments.Count != 1)
                    {
                        return(false);
                    }

                    var left = pooled.Item.Assignments[0].Left;
                    field = left as IdentifierNameSyntax;
                    if (field == null)
                    {
                        var memberAccess = left as MemberAccessExpressionSyntax;
                        if (!(memberAccess?.Expression is ThisExpressionSyntax))
                        {
                            return(false);
                        }

                        field = memberAccess.Name as IdentifierNameSyntax;
                    }

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

                    foreach (var member in property.FirstAncestorOrSelf <TypeDeclarationSyntax>().Members)
                    {
                        fieldDeclaration = member as FieldDeclarationSyntax;
                        if (fieldDeclaration != null)
                        {
                            foreach (var variable in fieldDeclaration.Declaration.Variables)
                            {
                                if (variable.Identifier.ValueText == field.Identifier.ValueText)
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 3
0
        internal static bool IsLazy(this PropertyDeclarationSyntax propertyDeclaration, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (propertyDeclaration.TryGetSetAccessorDeclaration(out AccessorDeclarationSyntax _))
            {
                return(false);
            }

            IFieldSymbol returnedField = null;

            if (propertyDeclaration.TryGetGetAccessorDeclaration(out AccessorDeclarationSyntax getter))
            {
                if (getter.Body == null)
                {
                    return(false);
                }

                using (var pooledReturns = ReturnExpressionsWalker.Create(getter.Body))
                {
                    if (pooledReturns.Item.ReturnValues.Count == 0)
                    {
                        return(false);
                    }

                    foreach (var returnValue in pooledReturns.Item.ReturnValues)
                    {
                        var returnedSymbol = returnValue?.IsKind(SyntaxKind.CoalesceExpression) == true
                            ? semanticModel.GetSymbolSafe((returnValue as BinaryExpressionSyntax)?.Left, cancellationToken) as IFieldSymbol
                            : semanticModel.GetSymbolSafe(returnValue, cancellationToken) as IFieldSymbol;

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

                        if (returnedField != null &&
                            !ReferenceEquals(returnedSymbol, returnedField))
                        {
                            return(false);
                        }

                        returnedField = returnedSymbol;
                    }
                }

                return(AssignmentWalker.Assigns(returnedField, getter.Body, semanticModel, cancellationToken));
            }

            var arrow = propertyDeclaration.ExpressionBody;

            if (arrow?.Expression?.IsKind(SyntaxKind.CoalesceExpression) != true)
            {
                return(false);
            }

            returnedField = semanticModel.GetSymbolSafe((arrow.Expression as BinaryExpressionSyntax)?.Left, cancellationToken) as IFieldSymbol;
            return(AssignmentWalker.Assigns(returnedField, arrow.Expression, semanticModel, cancellationToken));
        }