private static IMappingSourceFinder GetMappingSourceFindersForQueryExpression(SemanticModel semanticModel, SyntaxGenerator syntaxGenerator, QueryExpressionSyntax queryExpression, IReadOnlyList <ISymbol> localSymbols)
        {
            var queryLocation = queryExpression.GetLocation().SourceSpan;

            var queryVariablesSourceFinders = localSymbols.Where(s => s is IRangeVariableSymbol).Where(s =>
            {
                var symbolLocation = s.Locations.First().SourceSpan;
                return(symbolLocation.Start >= queryLocation.Start && symbolLocation.End <= queryLocation.End);
            }).Select(s =>
            {
                var type = semanticModel.GetTypeForSymbol(s);
                if (ObjectHelper.IsSimpleType(type))
                {
                    return(null);
                }

                if (type is INamedTypeSymbol namedType && type.ToDisplayString().StartsWith("System.Linq.IGrouping<"))
                {
                    var keyType = namedType.TypeArguments[0];
                    if (ObjectHelper.IsSimpleType(keyType) == false)
                    {
                        var accessor = syntaxGenerator.MemberAccessExpression(syntaxGenerator.IdentifierName(s.Name), syntaxGenerator.IdentifierName("Key"));
                        return((IMappingSourceFinder) new OrderedSourceFinder(new []
                        {
                            new ObjectMembersMappingSourceFinder(type, syntaxGenerator.IdentifierName(s.Name), syntaxGenerator),
                            new ObjectMembersMappingSourceFinder(keyType, accessor, syntaxGenerator)
                        }));
                    }
                }

                return(new ObjectMembersMappingSourceFinder(type, syntaxGenerator.IdentifierName(s.Name), syntaxGenerator));
            }).Where(x => x != null).ToList();

            return(new OrderedSourceFinder(queryVariablesSourceFinders));
        }
Exemplo n.º 2
0
        private bool?CheckIfAllElementsAreAtTheSameLine(QueryExpressionSyntax query)
        {
            var fileLinePositionSpan = query.GetLocation().GetLineSpan();

            if (fileLinePositionSpan.IsValid)
            {
                return(fileLinePositionSpan.StartLinePosition.Line == fileLinePositionSpan.EndLinePosition.Line);
            }

            return(null);
        }
        private Diagnostic AnalyzeQueryExpression(QueryExpressionSyntax query, SemanticModel semanticModel)
        {
            var hasLinqMethods = false;

            if (TryFindSyntaxNode(query, out var syntaxNode, out var identifier))
            {
                hasLinqMethods = syntaxNode
                                 .DescendantNodes()
                                 .OfType <MemberAccessExpressionSyntax>()
                                 .SelectMany(_ => semanticModel.LookupSymbols(_.GetLocation().SourceSpan.Start))
                                 .Select(_ => _.ContainingNamespace?.ToString())
                                 .Where(_ => _ != null)
                                 .Any(_ => _.StartsWith("System.Linq", StringComparison.OrdinalIgnoreCase));
            }

            return(hasLinqMethods
                       ? ReportIssue(identifier, query.GetLocation())
                       : null);
        }