Exemple #1
0
        private static void AnalyzeNode(SyntaxNodeAnalysisContext nodeContext)
        {
            var argumentsList = GetArgumentsList(nodeContext.Node);

            if (argumentsList == null || argumentsList.Arguments.Count < ParamsThreshold)
            {
                return;
            }

            var argumentsWithoutNamecolon = argumentsList.Arguments.Where(x => x.NameColon == null);

            if (!argumentsWithoutNamecolon.Any())
            {
                return;
            }

            var call           = nodeContext.Node as ExpressionSyntax;
            var callParameters = ParametersHelper.GetInvocationParameters(nodeContext.SemanticModel, call);

            if (callParameters.Any(x => x.isParams))
            {
                return;
            }

            var diagnostic = Diagnostic.Create(
                descriptor: Rule,
                location: nodeContext.Node.GetLocation());

            nodeContext.ReportDiagnostic(diagnostic);
        }
        private async Task <Solution> UpdateCreationArguments(CodeFixContext context, ObjectCreationExpressionSyntax creation, CancellationToken cancellationToken)
        {
            var semanticModel = await context.Document.GetSemanticModelAsync(cancellationToken);

            var parameterNames = ParametersHelper.GetInvocationParameters(semanticModel, creation, cancellationToken);

            var updatedArguments = GetUpdatedArguments(creation.ArgumentList, parameterNames.Select(x => x.name));
            var newCreation      = creation.WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(updatedArguments)));

            var documentEditor = await DocumentEditor.CreateAsync(context.Document, cancellationToken);

            documentEditor.ReplaceNode(creation, newCreation);
            var newDocument = documentEditor.GetChangedDocument();

            return(newDocument.Project.Solution);
        }