private static void Handle(SyntaxNodeAnalysisContext context)
 {
     if (!context.IsExcludedFromAnalysis() &&
         context.Node is AttributeSyntax attribute &&
         Attribute.IsType(attribute, KnownSymbol.DependencyAttribute, context.SemanticModel, context.CancellationToken) &&
         Attribute.TryFindArgument(attribute, 0, "dependentAssemblyArgument", out var argument) &&
         context.SemanticModel.TryGetConstantValue(argument.Expression, context.CancellationToken, out string assemblyName) &&
         !context.Compilation.ReferencedAssemblyNames.TryFirst(x => x.Name == assemblyName, out _))
     {
         context.ReportDiagnostic(Diagnostic.Create(REFL032DependencyMustExist.Descriptor, argument.GetLocation()));
     }
 }
        private static bool TryGetTemplate(AttributeSyntax attribute, SyntaxNodeAnalysisContext context, out UrlTemplate template)
        {
            if (attribute.ArgumentList is AttributeArgumentListSyntax argumentList &&
                argumentList.Arguments.TrySingle(out var argument) &&
                argument.Expression is LiteralExpressionSyntax literal &&
                literal.IsKind(SyntaxKind.StringLiteralExpression) &&
                (Attribute.IsType(attribute, KnownSymbol.HttpDeleteAttribute, context.SemanticModel, context.CancellationToken) ||
                 Attribute.IsType(attribute, KnownSymbol.HttpGetAttribute, context.SemanticModel, context.CancellationToken) ||
                 Attribute.IsType(attribute, KnownSymbol.HttpHeadAttribute, context.SemanticModel, context.CancellationToken) ||
                 Attribute.IsType(attribute, KnownSymbol.HttpOptionsAttribute, context.SemanticModel, context.CancellationToken) ||
                 Attribute.IsType(attribute, KnownSymbol.HttpPatchAttribute, context.SemanticModel, context.CancellationToken) ||
                 Attribute.IsType(attribute, KnownSymbol.HttpPostAttribute, context.SemanticModel, context.CancellationToken) ||
                 Attribute.IsType(attribute, KnownSymbol.HttpPutAttribute, context.SemanticModel, context.CancellationToken) ||
                 Attribute.IsType(attribute, KnownSymbol.RouteAttribute, context.SemanticModel, context.CancellationToken)) &&
                UrlTemplate.TryParse(literal, out template))
            {
                return(true);
            }

            template = default(UrlTemplate);
            return(false);
        }
Example #3
0
        /// <summary>
        /// Attempt to retrieve information about the application of the
        /// DefaultMemberAttribute:
        ///
        ///  - Specified member name
        ///  - Location of attribute
        ///  - Type symbol of type with attribute applied
        ///
        /// Does not throw on failure.
        /// </summary>
        /// <param name="context">Context of attribute application.</param>
        /// <param name="memberName">Member name specified by attribute.</param>
        /// <param name="location">Location of attribute application.</param>
        /// <param name="typeSymbol">Attribute target.</param>
        /// <returns>Success</returns>
        private static bool TryGetAttributeAndTypeInfo(
            SyntaxNodeAnalysisContext context,
            out string memberName,
            out Location location,
            out ITypeSymbol typeSymbol)
        {
            memberName = default(string);
            location   = default(Location);
            typeSymbol = default(ITypeSymbol);

            if (!(context.Node is AttributeSyntax attribute) ||
                !Attribute.IsType(attribute, KnownSymbol.DefaultMemberAttribute, context.SemanticModel, context.CancellationToken))
            {
                return(false);
            }

            if (!Attribute.TryFindArgument(attribute, 0, "memberName", out var argument) ||
                !context.SemanticModel.TryGetConstantValue(argument.Expression, context.CancellationToken, out string workingMemberName))
            {
                return(false);
            }

            if (!attribute.TryFirstAncestor(out ClassDeclarationSyntax classDeclaration))
            {
                return(false);
            }

            if (!context.SemanticModel.TryGetSymbol(classDeclaration, context.CancellationToken, out ITypeSymbol workingTypeSymbol))
            {
                return(false);
            }

            memberName = workingMemberName;
            location   = argument.GetLocation();
            typeSymbol = workingTypeSymbol;
            return(true);
        }