Ejemplo n.º 1
0
                > GetReferencesForMatchingNamespacesAsync(SearchScope searchScope)
            {
                searchScope.CancellationToken.ThrowIfCancellationRequested();
                if (_owner.CanAddImportForNamespace(_diagnosticId, _node, out var nameNode))
                {
                    _syntaxFacts.GetNameAndArityOfSimpleName(nameNode, out var name, out var arity);

                    if (
                        arity == 0 &&
                        !ExpressionBinds(
                            nameNode,
                            checkForExtensionMethods: false,
                            cancellationToken: searchScope.CancellationToken
                            )
                        )
                    {
                        var symbols = await searchScope
                                      .FindDeclarationsAsync(name, nameNode, SymbolFilter.Namespace)
                                      .ConfigureAwait(false);

                        var namespaceSymbols           = OfType <INamespaceSymbol>(symbols);
                        var containingNamespaceSymbols = OfType <INamespaceSymbol>(symbols)
                                                         .SelectAsArray(s => s.WithSymbol(s.Symbol.ContainingNamespace));

                        return(GetNamespaceSymbolReferences(
                                   searchScope,
                                   containingNamespaceSymbols
                                   ));
                    }
                }

                return(ImmutableArray <SymbolReference> .Empty);
            }
            private async Task <IList <SymbolReference> > GetNamespacesForMatchingNamespacesAsync(
                SearchScope searchScope)
            {
                searchScope.CancellationToken.ThrowIfCancellationRequested();

                TSimpleNameSyntax nameNode;

                if (!_owner.CanAddImportForNamespace(_diagnostic, _node, out nameNode))
                {
                    return(null);
                }

                string name;
                int    arity;

                _syntaxFacts.GetNameAndArityOfSimpleName(nameNode, out name, out arity);

                if (arity > 0)
                {
                    return(null);
                }

                if (ExpressionBinds(searchScope, nameNode, checkForExtensionMethods: false))
                {
                    return(null);
                }

                var symbols = await searchScope.FindDeclarationsAsync(name, nameNode, SymbolFilter.Namespace).ConfigureAwait(false);

                return(GetProposedNamespaces(
                           searchScope, OfType <INamespaceSymbol>(symbols).Select(s => s.WithSymbol(s.Symbol.ContainingNamespace))));
            }
Ejemplo n.º 3
0
        private async Task <IEnumerable <IMethodSymbol> > GetAddMethodsAsync(
            Project project,
            Diagnostic diagnostic,
            SyntaxNode node,
            SemanticModel semanticModel,
            ISet <INamespaceSymbol> namespacesInScope,
            ISyntaxFactsService syntaxFacts,
            SyntaxNode expression,
            CancellationToken cancellationToken)
        {
            string name;
            int    arity;

            syntaxFacts.GetNameAndArityOfSimpleName(node, out name, out arity);
            if (name != null)
            {
                return(SpecializedCollections.EmptyEnumerable <IMethodSymbol>());
            }

            if (IsAddMethodContext(node, semanticModel))
            {
                var symbols = await SymbolFinder.FindDeclarationsAsync(project, "Add", this.IgnoreCase, SymbolFilter.Member, cancellationToken).ConfigureAwait(false);

                return(symbols
                       .OfType <IMethodSymbol>()
                       .Where(method => method.IsExtensionMethod &&
                              method.IsAccessibleWithin(semanticModel.Compilation.Assembly) == true &&
                              IsViableExtensionMethod(method, expression, semanticModel, syntaxFacts, cancellationToken)));
            }

            return(SpecializedCollections.EmptyEnumerable <IMethodSymbol>());
        }
Ejemplo n.º 4
0
        private Task <IEnumerable <ISymbol> > GetSymbolsAsync(
            Project project,
            SyntaxNode node,
            SemanticModel semanticModel,
            ISyntaxFactsService syntaxFacts,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            // See if the name binds.  If it does, there's nothing further we need to do.
            if (ExpressionBinds(node, semanticModel, cancellationToken, checkForExtensionMethods: true))
            {
                return(SpecializedTasks.EmptyEnumerable <ISymbol>());
            }

            string name;
            int    arity;

            syntaxFacts.GetNameAndArityOfSimpleName(node, out name, out arity);
            if (name == null)
            {
                return(SpecializedTasks.EmptyEnumerable <ISymbol>());
            }

            return(SymbolFinder.FindDeclarationsAsync(project, name, this.IgnoreCase, SymbolFilter.Member, cancellationToken));
        }
Ejemplo n.º 5
0
        private async Task <IEnumerable <INamespaceSymbol> > GetNamespacesForMatchingNamespacesAsync(
            Project project,
            Diagnostic diagnostic,
            SyntaxNode node,
            SemanticModel semanticModel,
            ISet <INamespaceSymbol> namespacesInScope,
            ISyntaxFactsService syntaxFacts,
            CancellationToken cancellationToken)
        {
            if (!this.CanAddImportForNamespace(diagnostic, ref node))
            {
                return(null);
            }

            string name;
            int    arity;

            syntaxFacts.GetNameAndArityOfSimpleName(node, out name, out arity);

            if (ExpressionBinds(node, semanticModel, cancellationToken))
            {
                return(null);
            }

            var symbols = await SymbolFinder.FindDeclarationsAsync(
                project, name, this.IgnoreCase, SymbolFilter.Namespace, cancellationToken).ConfigureAwait(false);

            return(GetProposedNamespaces(
                       symbols.OfType <INamespaceSymbol>().Select(n => n.ContainingNamespace),
                       semanticModel,
                       namespacesInScope));
        }
Ejemplo n.º 6
0
        private static void CalculateContext(SyntaxNode node, ISyntaxFactsService syntaxFacts, out string name, out int arity, out bool inAttributeContext, out bool hasIncompleteParentMember)
        {
            // Has to be a simple identifier or generic name.
            syntaxFacts.GetNameAndArityOfSimpleName(node, out name, out arity);

            inAttributeContext        = syntaxFacts.IsAttributeName(node);
            hasIncompleteParentMember = syntaxFacts.HasIncompleteParentMember(node);
        }
        private static bool TryAnalyzeInvocationCondition(
            SyntaxNodeAnalysisContext context,
            ISyntaxFactsService syntaxFacts,
            IMethodSymbol?referenceEqualsMethodOpt,
            TInvocationExpression invocation,
            [NotNullWhen(true)] out SyntaxNode?conditionPartToCheck,
            out bool isEquals)
        {
            conditionPartToCheck = null;
            isEquals             = true;

            var expression = syntaxFacts.GetExpressionOfInvocationExpression(invocation);
            var nameNode   = syntaxFacts.IsIdentifierName(expression)
                ? expression
                : syntaxFacts.IsSimpleMemberAccessExpression(expression)
                    ? syntaxFacts.GetNameOfMemberAccessExpression(expression)
                    : null;

            if (!syntaxFacts.IsIdentifierName(nameNode))
            {
                return(false);
            }

            syntaxFacts.GetNameAndArityOfSimpleName(nameNode, out var name, out _);
            if (!syntaxFacts.StringComparer.Equals(name, nameof(ReferenceEquals)))
            {
                return(false);
            }

            var arguments = syntaxFacts.GetArgumentsOfInvocationExpression(invocation);

            if (arguments.Count != 2)
            {
                return(false);
            }

            var conditionLeft  = syntaxFacts.GetExpressionOfArgument(arguments[0]);
            var conditionRight = syntaxFacts.GetExpressionOfArgument(arguments[1]);

            if (conditionLeft == null || conditionRight == null)
            {
                return(false);
            }

            conditionPartToCheck = GetConditionPartToCheck(syntaxFacts, conditionLeft, conditionRight);
            if (conditionPartToCheck == null)
            {
                return(false);
            }

            var semanticModel     = context.SemanticModel;
            var cancellationToken = context.CancellationToken;
            var symbol            = semanticModel.GetSymbolInfo(invocation, cancellationToken).Symbol;

            return(referenceEqualsMethodOpt != null && referenceEqualsMethodOpt.Equals(symbol));
        }
Ejemplo n.º 8
0
        private bool TryAnalyzeAddInvocation(
            TExpressionStatementSyntax statement,
            out SyntaxNode instance)
        {
            instance = null;
            var invocationExpression = _syntaxFacts.GetExpressionOfExpressionStatement(statement) as TInvocationExpressionSyntax;

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

            var arguments = _syntaxFacts.GetArgumentsOfInvocationExpression(invocationExpression);

            if (arguments.Count < 1)
            {
                return(false);
            }

            foreach (var argument in arguments)
            {
                if (!_syntaxFacts.IsSimpleArgument(argument))
                {
                    return(false);
                }
            }

            var memberAccess = _syntaxFacts.GetExpressionOfInvocationExpression(invocationExpression) as TMemberAccessExpressionSyntax;

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

            if (!_syntaxFacts.IsSimpleMemberAccessExpression(memberAccess))
            {
                return(false);
            }

            SyntaxNode memberName;

            _syntaxFacts.GetPartsOfMemberAccessExpression(memberAccess, out instance, out memberName);

            string name;
            int    arity;

            _syntaxFacts.GetNameAndArityOfSimpleName(memberName, out name, out arity);

            if (arity != 0 || !name.Equals(nameof(IList.Add)))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 9
0
            private void CalculateContext(
                TSimpleNameSyntax nameNode, ISyntaxFactsService syntaxFacts, out string name, out int arity,
                out bool inAttributeContext, out bool hasIncompleteParentMember, out bool looksGeneric)
            {
                // Has to be a simple identifier or generic name.
                syntaxFacts.GetNameAndArityOfSimpleName(nameNode, out name, out arity);

                inAttributeContext        = syntaxFacts.IsAttributeName(nameNode);
                hasIncompleteParentMember = syntaxFacts.HasIncompleteParentMember(nameNode);
                looksGeneric = syntaxFacts.LooksGeneric(nameNode);
            }
            private async Task <IList <SymbolReference> > GetNamespacesForMatchingNamespacesAsync(SearchScope searchScope)
            {
                if (!_owner.CanAddImportForNamespace(_diagnostic, ref _node))
                {
                    return(null);
                }

                string name;
                int    arity;

                _syntaxFacts.GetNameAndArityOfSimpleName(_node, out name, out arity);

                if (ExpressionBinds(checkForExtensionMethods: false))
                {
                    return(null);
                }

                var symbols = await searchScope.FindDeclarationsAsync(name, SymbolFilter.Namespace).ConfigureAwait(false);

                return(GetProposedNamespaces(
                           searchScope, symbols.OfType <INamespaceSymbol>().Select(n => n.ContainingNamespace)));
            }
        private async Task <IEnumerable <INamespaceSymbol> > GetNamespacesForMatchingExtensionMethodsAsync(
            Project project,
            Diagnostic diagnostic,
            SyntaxNode node,
            SemanticModel semanticModel,
            ISet <INamespaceSymbol> namespacesInScope,
            ISyntaxFactsService syntaxFacts,
            CancellationToken cancellationToken)
        {
            if (!this.CanAddImportForMethod(diagnostic, syntaxFacts, ref node))
            {
                return(null);
            }

            var expression = node.Parent;

            cancellationToken.ThrowIfCancellationRequested();

            // See if the name binds.  If it does, there's nothing further we need to do.
            if (ExpressionBinds(node, semanticModel, cancellationToken, checkForExtensionMethods: true))
            {
                return(null);
            }

            string name;
            int    arity;

            syntaxFacts.GetNameAndArityOfSimpleName(node, out name, out arity);

            var symbols = await SymbolFinder.FindDeclarationsAsync(project, name, this.IgnoreCase, SymbolFilter.Member, cancellationToken).ConfigureAwait(false);

            var extensionMethodSymbols = symbols
                                         .OfType <IMethodSymbol>()
                                         .Where(method => method.IsExtensionMethod &&
                                                method.ContainingType?.IsAccessibleWithin(semanticModel.Compilation.Assembly) == true &&
                                                IsViableExtensionMethod(method, expression, semanticModel, syntaxFacts, cancellationToken))
                                         .ToList();

            return(GetProposedNamespaces(
                       extensionMethodSymbols.Select(s => s.ContainingNamespace),
                       semanticModel,
                       namespacesInScope));
        }
        private static void CalculateContext(SyntaxNode node, ISyntaxFactsService syntaxFacts, out string name, out int arity, out bool inAttributeContext, out bool hasIncompleteParentMember)
        {
            // Has to be a simple identifier or generic name.
            syntaxFacts.GetNameAndArityOfSimpleName(node, out name, out arity);

            inAttributeContext = syntaxFacts.IsAttributeName(node);
            hasIncompleteParentMember = syntaxFacts.HasIncompleteParentMember(node);
        }
        private async Task<IEnumerable<IMethodSymbol>> GetAddMethodsAsync(
            Project project,
            Diagnostic diagnostic,
            SyntaxNode node,
            SemanticModel semanticModel,
            ISet<INamespaceSymbol> namespacesInScope,
            ISyntaxFactsService syntaxFacts,
            SyntaxNode expression,
            CancellationToken cancellationToken)
        {
            string name;
            int arity;
            syntaxFacts.GetNameAndArityOfSimpleName(node, out name, out arity);
            if (name != null)
            {
                return SpecializedCollections.EmptyEnumerable<IMethodSymbol>();
            }

            if (IsAddMethodContext(node, semanticModel))
            {
                var symbols = await SymbolFinder.FindDeclarationsAsync(project, "Add", this.IgnoreCase, SymbolFilter.Member, cancellationToken).ConfigureAwait(false);
                return symbols
                    .OfType<IMethodSymbol>()
                    .Where(method => method.IsExtensionMethod &&
                                     method.ContainingType?.IsAccessibleWithin(semanticModel.Compilation.Assembly) == true &&
                                     IsViableExtensionMethod(method, expression, semanticModel, syntaxFacts, cancellationToken));
            }

            return SpecializedCollections.EmptyEnumerable<IMethodSymbol>();
        }
        private Task<IEnumerable<ISymbol>> GetSymbolsAsync(
            Project project,
            SyntaxNode node,
            SemanticModel semanticModel,
            ISyntaxFactsService syntaxFacts,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            // See if the name binds.  If it does, there's nothing further we need to do.
            if (ExpressionBinds(node, semanticModel, cancellationToken, checkForExtensionMethods: true))
            {
                return SpecializedTasks.EmptyEnumerable<ISymbol>();
            }

            string name;
            int arity;
            syntaxFacts.GetNameAndArityOfSimpleName(node, out name, out arity);
            if (name == null)
            {
                return SpecializedTasks.EmptyEnumerable<ISymbol>();
            }

            return SymbolFinder.FindDeclarationsAsync(project, name, this.IgnoreCase, SymbolFilter.Member, cancellationToken);
        }
        private async Task<IEnumerable<INamespaceSymbol>> GetNamespacesForMatchingNamespacesAsync(
            Project project,
            Diagnostic diagnostic,
            SyntaxNode node,
            SemanticModel semanticModel,
            ISet<INamespaceSymbol> namespacesInScope,
            ISyntaxFactsService syntaxFacts,
            CancellationToken cancellationToken)
        {
            if (!this.CanAddImportForNamespace(diagnostic, ref node))
            {
                return null;
            }

            string name;
            int arity;
            syntaxFacts.GetNameAndArityOfSimpleName(node, out name, out arity);

            if (ExpressionBinds(node, semanticModel, cancellationToken))
            {
                return null;
            }

            var symbols = await SymbolFinder.FindDeclarationsAsync(
                project, name, this.IgnoreCase, SymbolFilter.Namespace, cancellationToken).ConfigureAwait(false);

            return GetProposedNamespaces(
                symbols.OfType<INamespaceSymbol>().Select(n => n.ContainingNamespace),
                semanticModel,
                namespacesInScope);
        }