Beispiel #1
0
        internal static void ReplaceWithEnumerable(DocumentEditor editor, SyntaxNode nodeToReplace)
        {
            ContractInvocationInfo contractCallInfo = null;

            if (!ContractStatementAnalyzer.ParseInvocation(nodeToReplace.Parent as ExpressionStatementSyntax, out contractCallInfo))
            {
                if (!ContractStatementAnalyzer.ParseInvocation(nodeToReplace as InvocationExpressionSyntax, out contractCallInfo))
                {
                    return;
                }
            }

            if (!contractCallInfo.IsContractType || !ContractForAllToEnumerableAnalyzer.MethodNamesToFix.Contains(contractCallInfo.MethodNameAsString))
            {
                return;
            }

            var generator = editor.Generator;

            var trailingTrivia = nodeToReplace.GetTrailingTrivia();
            var leadingTrivia  = nodeToReplace.GetLeadingTrivia();

            SyntaxNode newCallNode = null;

            if (contractCallInfo.MethodNameAsString == nameof(System.Diagnostics.Contracts.Contract.ForAll) && contractCallInfo.AllArguments.Arguments.Count == 2)
            {
                newCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(contractCallInfo.AllArguments.Arguments[0].Expression, nameof(Enumerable.All)),
                    contractCallInfo.AllArguments.Arguments[1]);
            }
            else if (contractCallInfo.MethodNameAsString == nameof(System.Diagnostics.Contracts.Contract.Exists) && contractCallInfo.AllArguments.Arguments.Count == 2)
            {
                newCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(contractCallInfo.AllArguments.Arguments[0].Expression, nameof(Enumerable.Any)),
                    contractCallInfo.AllArguments.Arguments[1]);
            }


            if (newCallNode != null)
            {
                newCallNode = newCallNode.WithTrailingTrivia(trailingTrivia).WithLeadingTrivia(leadingTrivia);
                editor.ReplaceNode(nodeToReplace, newCallNode);
            }
        }
Beispiel #2
0
        private static async Task <Document> ReplaceWithTurboContract(Document document, SyntaxNode nodeToReplace, string stringText, CancellationToken cancellationToken)
        {
            var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);

            var generator = editor.Generator;

            var semanticModel = await document.GetSemanticModelAsync(cancellationToken);

            ContractInvocationInfo contractCallInfo = null;

            if (!ContractStatementAnalyzer.ParseInvocation(nodeToReplace.Parent as ExpressionStatementSyntax, out contractCallInfo) ||
                !contractCallInfo.IsSpecialContractType || !TurboContractConditionStringNotInSyncAnalyzer.MethodNamesToFix.Contains(contractCallInfo.MethodNameAsString))
            {
                return(document);
            }

            var trailingTrivia = nodeToReplace.GetTrailingTrivia();
            var leadingTrivia  = nodeToReplace.GetLeadingTrivia();

            SyntaxNode debugAssertCallNode = null;


            if (contractCallInfo.Message == null)
            {
                debugAssertCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(generator.IdentifierName(ContractStatementAnalyzer.SpecialContractClass), contractCallInfo.MethodName),
                    contractCallInfo.Condition,
                    generator.Argument("conditionString", RefKind.None, generator.LiteralExpression(contractCallInfo.Condition.ToString())));
            }
            else
            {
                debugAssertCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(generator.IdentifierName(ContractStatementAnalyzer.SpecialContractClass), contractCallInfo.MethodName),
                    contractCallInfo.Condition,
                    contractCallInfo.Message,
                    generator.Argument("conditionString", RefKind.None, generator.LiteralExpression(contractCallInfo.Condition.ToString())));
            }

            debugAssertCallNode = debugAssertCallNode.WithTrailingTrivia(trailingTrivia).WithLeadingTrivia(leadingTrivia);

            editor.ReplaceNode(nodeToReplace, debugAssertCallNode);
            return(editor.GetChangedDocument());
        }
Beispiel #3
0
        internal static void ReplaceWithDebugAssert(DocumentEditor editor, SyntaxNode nodeToReplace)
        {
            ContractInvocationInfo contractCallInfo = null;

            if (!ContractStatementAnalyzer.ParseInvocation(nodeToReplace.Parent as ExpressionStatementSyntax, out contractCallInfo) ||
                !contractCallInfo.IsContractType || !ContractToDebugAssertAnalyzer.MethodNamesToFix.Contains(contractCallInfo.MethodNameAsString))
            {
                return;
            }

            var generator = editor.Generator;

            var trailingTrivia = nodeToReplace.GetTrailingTrivia();
            var leadingTrivia  = nodeToReplace.GetLeadingTrivia();

            SyntaxNode debugAssertCallNode = null;

            if (contractCallInfo.Message == null)
            {
                debugAssertCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(generator.IdentifierName(nameof(Debug)), nameof(Debug.Assert)),
                    contractCallInfo.Condition,
                    generator.LiteralExpression(contractCallInfo.Condition.ToString()));
            }
            else
            {
                debugAssertCallNode = generator.InvocationExpression(
                    generator.MemberAccessExpression(generator.IdentifierName(nameof(Debug)), nameof(Debug.Assert)),
                    contractCallInfo.Condition,
                    generator.LiteralExpression(contractCallInfo.Condition.ToString()),
                    contractCallInfo.Message);
            }

            debugAssertCallNode = debugAssertCallNode.WithTrailingTrivia(trailingTrivia).WithLeadingTrivia(leadingTrivia);

            editor.ReplaceNode(nodeToReplace, debugAssertCallNode);
        }