Example #1
0
        private static void AnalyzeModelDeclarationAttribute(SymbolAnalysisContext context, INamedTypeSymbol modelType, AttributeData attribute)
        {
            var compilation = context.Compilation;

            var spec = attribute.AttributeClass.GetModelDeclarationSpec(compilation);

            if (!spec.HasValue)
            {
                return;
            }

            var specValue = spec.Value;
            var name      = attribute.GetStringArgument();

            if (name == null)
            {
                return;
            }

            var implementation = GetImplementation(modelType, name, specValue);

            if (implementation == null)
            {
                var isProperty     = specValue.IsProperty;
                var parameterTypes = specValue.ParameterTypes;
                var returnType     = specValue.ReturnType;
                context.ReportDiagnostic(Diagnostic.Create(Rules.MissingImplementation, attribute.GetLocation(),
                                                           (isProperty ? Resources.StringFormatArg_Property : Resources.StringFormatArg_Method), name, returnType, parameterTypes.FormatString()));
                return;
            }

            var crossRefAttributeType = attribute.GetCrossReferenceAttributeType(compilation);

            if (crossRefAttributeType == null)
            {
                return;
            }
            if (!implementation.HasAttribute(crossRefAttributeType))
            {
                context.ReportDiagnostic(Diagnostic.Create(Rules.MissingImplementationAttribute, implementation.Locations[0], crossRefAttributeType));
            }
        }
Example #2
0
        private static void AnalyzeImplementation(SymbolAnalysisContext context, INamedTypeSymbol modelType, ISymbol symbol, AttributeData attribute, Compilation compilation)
        {
            var name = symbol.Name;

            var modelAttributeType = attribute.GetCrossReferenceAttributeType(compilation);

            if (modelAttributeType == null)
            {
                return;
            }

            var spec = modelAttributeType.GetModelDeclarationSpec(compilation);

            if (!spec.HasValue)
            {
                return;
            }
            var specValue      = spec.Value;
            var isProperty     = specValue.IsProperty;
            var returnType     = specValue.ReturnType;
            var parameterTypes = specValue.ParameterTypes;

            if (!IsImplementation(symbol, isProperty, specValue.ReturnType, specValue.ParameterTypes))
            {
                context.ReportDiagnostic(Diagnostic.Create(Rules.InvalidImplementationAttribute, attribute.GetLocation(), attribute.AttributeClass,
                                                           isProperty ? Resources.StringFormatArg_Property : Resources.StringFormatArg_Method, returnType, parameterTypes.FormatString()));
                return;
            }

            var modelAttribute = modelType.GetAttributes().Where(x => x.AttributeClass.Equals(modelAttributeType) && x.GetStringArgument() == name).FirstOrDefault();

            if (modelAttribute == null)
            {
                context.ReportDiagnostic(Diagnostic.Create(Rules.MissingDeclarationAttribute, attribute.GetLocation(), modelAttributeType, name));
            }
        }