Ejemplo n.º 1
0
        private void Run()
        {
            if (this.CurrentSymbol == null)
            {
                return;
            }

            if (this.CurrentSymbol is ILocalSymbol local)
            {
                var declaration = local.DeclaringSyntaxReferences.Single().GetSyntax(this.cancellationToken);
                var scope       = (SyntaxNode)declaration.FirstAncestorOrSelf <AnonymousFunctionExpressionSyntax>() ??
                                  declaration.FirstAncestorOrSelf <MemberDeclarationSyntax>();
                if (scope != null)
                {
                    this.Visit(scope);
                }

                return;
            }

            if (this.CurrentSymbol is IParameterSymbol)
            {
                var scope = (SyntaxNode)this.context?.FirstAncestorOrSelf <AnonymousFunctionExpressionSyntax>() ??
                            this.context?.FirstAncestorOrSelf <MemberDeclarationSyntax>();
                if (scope != null)
                {
                    this.Visit(scope);
                }

                return;
            }

            if (this.CurrentSymbol is IFieldSymbol ||
                this.CurrentSymbol is IPropertySymbol)
            {
                var type = (INamedTypeSymbol)this.semanticModel.GetDeclaredSymbolSafe(this.context?.FirstAncestorOrSelf <TypeDeclarationSyntax>(), this.cancellationToken);
                if (type == null)
                {
                    return;
                }

                if (this.CurrentSymbol is IFieldSymbol &&
                    this.CurrentSymbol.TrySingleDeclaration(this.cancellationToken, out FieldDeclarationSyntax fieldDeclarationSyntax))
                {
                    this.Visit(fieldDeclarationSyntax);
                }
                else if (this.CurrentSymbol is IPropertySymbol &&
                         this.CurrentSymbol.TrySingleDeclaration(this.cancellationToken, out PropertyDeclarationSyntax propertyDeclaration) &&
                         propertyDeclaration.Initializer != null)
                {
                    this.values.Add(propertyDeclaration.Initializer.Value);
                }

                var contextCtor = this.context?.FirstAncestorOrSelf <ConstructorDeclarationSyntax>();
                foreach (var reference in type.DeclaringSyntaxReferences)
                {
                    using (var ctorWalker = ConstructorsWalker.Borrow((TypeDeclarationSyntax)reference.GetSyntax(this.cancellationToken), this.semanticModel, this.cancellationToken))
                    {
                        foreach (var creation in ctorWalker.ObjectCreations)
                        {
                            if (this.visitedLocations.Add(creation))
                            {
                                if (contextCtor == null ||
                                    creation.Creates(contextCtor, Search.Recursive, this.semanticModel, this.cancellationToken))
                                {
                                    this.VisitObjectCreationExpression(creation);
                                    var method = this.semanticModel.GetSymbolSafe(creation, this.cancellationToken);
                                    this.HandleInvoke(method, creation.ArgumentList);
                                }
                            }
                        }

                        if (contextCtor != null)
                        {
                            foreach (var initializer in ctorWalker.Initializers)
                            {
                                var other = (ConstructorDeclarationSyntax)initializer.Parent;
                                if (Constructor.IsRunBefore(contextCtor, other, this.semanticModel, this.cancellationToken))
                                {
                                    this.Visit(other);
                                }
                            }

                            if (!contextCtor.Modifiers.Any(SyntaxKind.PrivateKeyword))
                            {
                                this.Visit(contextCtor);
                            }

                            return;
                        }

                        if (ctorWalker.Default != null)
                        {
                            this.Visit(ctorWalker.Default);
                        }

                        foreach (var ctor in ctorWalker.NonPrivateCtors)
                        {
                            this.Visit(ctor);
                        }
                    }
                }

                if ((this.CurrentSymbol is IFieldSymbol field &&
                     !field.IsReadOnly) ||
                    (this.CurrentSymbol is IPropertySymbol property &&
                     !property.IsReadOnly))
                {
                    var scope = (SyntaxNode)this.context?.FirstAncestorOrSelf <AnonymousFunctionExpressionSyntax>() ??
                                this.context?.FirstAncestorOrSelf <MemberDeclarationSyntax>();
                    if (scope != null &&
                        !(scope is ConstructorDeclarationSyntax))
                    {
                        while (type.Is(this.CurrentSymbol.ContainingType))
                        {
                            foreach (var reference in type.DeclaringSyntaxReferences)
                            {
                                var typeDeclaration = (TypeDeclarationSyntax)reference.GetSyntax(this.cancellationToken);
                                this.memberWalker.Visit(typeDeclaration);
                            }

                            type = type.BaseType;
                        }
                    }
                }
            }
        }