示例#1
0
        private static void UpdateInvocationLocation(
            SyntaxNodeAnalysisContext context,
            ISymbol symbol,
            SimpleNameSyntax currentAccessNode,
            GetAccessInfoDelegate getAccessInfo)
        {
            var symbolInfo = _applicationBuilderSymbolInfos.GetOrCreateValue(symbol);

            var scopeEnclosingNode =
                currentAccessNode.FirstAncestorOrSelf <CSharpSyntaxNode>(IsScopeEnclosingNode);

            lock (symbolInfo)
            {
                if (!symbolInfo.Scopes.TryGetValue(scopeEnclosingNode, out var scopeInfo))
                {
                    symbolInfo.Scopes[scopeEnclosingNode] = scopeInfo = new ScopeInfo();
                }

                if (scopeInfo.DiagnosticReported)
                {
                    return;
                }

                ref var existingAccessInfo = ref getAccessInfo(scopeInfo);

                if (existingAccessInfo.location == null ||
                    currentAccessNode.GetLocation().SourceSpan.Start < existingAccessInfo.location.SourceSpan.Start)
                {
                    existingAccessInfo = (currentAccessNode.GetLocation(), currentAccessNode.ToString());
                    VerifyInvocationOrder(context, scopeInfo);
                }
            }
        public override IEnumerable <MemberDeclarationSyntax> Generate()
        {
            if (!HasDiscriminator)
            {
                // We have no discriminator, so we're just going to implement using dynamic expando objects
                // So we shouldn't implement a specific class
                yield break;
            }

            var interfaceNameAndNamespace = (QualifiedNameSyntax)GetTypeName();

            // Register the referenced schema to implement this interface
            var baseTypeRegistry = Context.GenerationServices.GetRequiredService <ISchemaBaseTypeRegistry>();

            foreach (var referencedSchema in Schema.OneOf
                     .Where(p => p.Reference != null)
                     .Select(p => ((OpenApiSchema)Context.Document.ResolveReference(p.Reference)).CreateRoot(p.Reference.Id)))
            {
                baseTypeRegistry.AddBaseType(referencedSchema, SyntaxFactory.SimpleBaseType(interfaceNameAndNamespace));
            }

            SimpleNameSyntax interfaceName = interfaceNameAndNamespace.Right;

            yield return(SyntaxFactory.InterfaceDeclaration(interfaceName.ToString())
                         .AddElementAnnotation(Element, Context.ElementRegistry)
                         .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)));
        }
 private void RegisterCodeFixVariant(CodeFixContext context, CodeFixHelper codeFixHelper, SimpleNameSyntax oldMethodName, SimpleNameSyntax newMethodName)
 {
     context.RegisterCodeFix(
         CodeAction.Create(
             title: CodeFixMessagesProvider.GetReplaceWithMessage(newMethodName),
             createChangedDocument: c => codeFixHelper.ReplaceExpressionWith(oldMethodName, newMethodName, c),
             equivalenceKey: newMethodName.ToString()),
         codeFixHelper.GetFirstDiagnostic());
 }
示例#4
0
        protected override IEnumerable <Diagnostic> Analyze(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocationExpressionSyntax, MemberAccessExpressionSyntax memberAccessExpression)
        {
            string memberName = memberAccessExpression.Name switch
            {
                GenericNameSyntax generic => generic.Identifier.ToString(),
                SimpleNameSyntax name => name.ToString()
            };

            if ((memberName != @"AreEqual") && (memberName != @"AreNotEqual"))
            {
                return(null);
            }

            IMethodSymbol memberSymbol = context.SemanticModel.GetSymbolInfo(memberAccessExpression).Symbol as IMethodSymbol;

            if ((memberSymbol == null) || !memberSymbol.ToString().StartsWith("Microsoft.VisualStudio.TestTools.UnitTesting.Assert"))
            {
                return(null);
            }

            // Assert.AreEqual is incorrectly used if the literal is the second argument (including null) or if the first argument is null
            ArgumentListSyntax argumentList = invocationExpressionSyntax.ArgumentList;

            bool arg0Literal = IsLiteral(argumentList.Arguments[0].Expression, context.SemanticModel);
            bool arg1Literal = IsLiteral(argumentList.Arguments[1].Expression, context.SemanticModel);

            if (!arg0Literal && !arg1Literal)
            {
                return(null);
            }

            if (arg0Literal)
            {
                return(null);
            }

            if (arg1Literal)
            {
                Diagnostic diagnostic = Diagnostic.Create(Rule, invocationExpressionSyntax.GetLocation());
                return(new[] { diagnostic });
            }

            return(null);
        }
        private InvocationExpressionSyntax ConvertToInvocation(SimpleNameSyntax calledMethod, ExpressionSyntax literalExpected, ExpressionSyntax actual, ExpressionSyntax message)
        {
            bool isLiteralTrue = GetMethod(literalExpected);

            if (calledMethod.ToString() == @"AreNotEqual")
            {
                isLiteralTrue = !isLiteralTrue;
            }

            string method = isLiteralTrue ? "IsTrue" : "IsFalse";

            ArgumentListSyntax argumentListSyntax = SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[] { SyntaxFactory.Argument(actual) }));

            if (message != null)
            {
                argumentListSyntax = argumentListSyntax.AddArguments(SyntaxFactory.Argument(message));
            }

            return(SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ParseTypeName("Assert"), SyntaxFactory.Token(SyntaxKind.DotToken), (SimpleNameSyntax)SyntaxFactory.ParseName(method)), argumentListSyntax));
        }
        public override IEnumerable <MemberDeclarationSyntax> Generate()
        {
            var interfaceNameAndNamespace = (QualifiedNameSyntax)TypeInfo.Name;

            // Register the referenced schema to implement this interface
            var baseTypeRegistry = Context.GenerationServices.GetRequiredService <ISchemaBaseTypeRegistry>();

            foreach (var referencedSchema in Schema.OneOf
                     .Where(p => p.Reference != null)
                     .Select(p => ((OpenApiSchema)Context.Document.ResolveReference(p.Reference)).CreateRoot(p.Reference.Id)))
            {
                baseTypeRegistry.AddBaseType(referencedSchema, SyntaxFactory.SimpleBaseType(interfaceNameAndNamespace));
            }

            SimpleNameSyntax interfaceName = interfaceNameAndNamespace.Right;

            yield return(SyntaxFactory.InterfaceDeclaration(interfaceName.ToString())
                         .AddElementAnnotation(Element, Context.ElementRegistry)
                         .AddGeneratorAnnotation(this)
                         .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)));
        }