Exemplo n.º 1
0
        private (bool, ExpressionSyntax) CheckIfRewriteInvocation(InvocationExpressionSyntax node, ExpressionSyntax lastNode)
        {
            if (lastNode.ToString() == "Enumerable" || lastNode.ToString() == "ExtendedLinq")
            {
                lastNode = (ExpressionSyntax)lastNode.Parent.Parent;
            }
            var flows    = _data.Semantic.AnalyzeDataFlow(node);
            var flowsOut = flows.DataFlowsOut.ToArray();

            var flowsIn = flows.DataFlowsIn.Concat(node.DescendantNodesAndSelf()
                                                   .Where(x => x is IdentifierNameSyntax)
                                                   .Select(x => _data.Semantic.GetSymbolInfo(x).Symbol)
                                                   .Where(x => x is IFieldSymbol)).ToArray();

            var changingParams = flowsOut.Concat(flows.WrittenInside).ToArray();
            var currentFlow    = flowsIn.Union(flowsOut)
                                 .Where(x => (x as IParameterSymbol)?.IsThis != true)
                                 .Select(x => VariableExtensions.CreateVariableCapture(x, changingParams));

            _data.CurrentMethodParams = currentFlow
                                        .Select(x => CreateParameter(x.Name, SymbolExtensions.GetType(x.Symbol)).WithRef(x.Changes))
                                        .Distinct(new RewriteService.ParameterComparer()).ToList();
            _data.CurrentMethodArguments = currentFlow
                                           .GroupBy(x => x.Symbol, (x, y) => new VariableCapture(x, y.Any(z => z.Changes)))
                                           .Select(x => Argument(x.OriginalName).WithRef(x.Changes)).ToList();

            if (SyntaxExtensions.IsAnonymousType(_data.Semantic.GetTypeInfo(lastNode).Type))
            {
                return(false, null);
            }

            var returnType = _data.Semantic.GetTypeInfo(node).Type;

            if (SyntaxExtensions.IsAnonymousType(returnType) ||
                currentFlow.Any(x => SyntaxExtensions.IsAnonymousType(SymbolExtensions.GetType(x.Symbol))))
            {
                return(false, null);
            }

            return(true, lastNode);
        }