コード例 #1
0
 private static void Handle(SyntaxNodeAnalysisContext context)
 {
     if (!context.IsExcludedFromAnalysis() &&
         context.Node is PropertyDeclarationSyntax propertyDeclaration &&
         context.ContainingSymbol is IPropertySymbol property &&
         property.ContainingType.IsAssignableTo(KnownSymbol.MarkupExtension, context.Compilation) &&
         !Attribute.TryFind(propertyDeclaration, KnownSymbol.ConstructorArgumentAttribute, context.SemanticModel, context.CancellationToken, out _) &&
         ConstructorArgument.TryGetParameterName(property, context.SemanticModel, context.CancellationToken, out var parameterName))
     {
         context.ReportDiagnostic(
             Diagnostic.Create(
                 Descriptor,
                 propertyDeclaration.Identifier.GetLocation(),
                 ImmutableDictionary.CreateRange(new[] { new KeyValuePair <string, string>(nameof(ConstructorArgument), parameterName) }),
                 parameterName));
     }
 }
コード例 #2
0
 private static void Handle(SyntaxNodeAnalysisContext context)
 {
     if (!context.IsExcludedFromAnalysis() &&
         context.Node is AttributeSyntax attribute)
     {
         if (Attribute.IsType(attribute, KnownSymbol.XmlnsDefinitionAttribute, context.SemanticModel, context.CancellationToken) &&
             Attribute.TryFindArgument(attribute, 1, KnownSymbol.XmlnsDefinitionAttribute.ClrNamespaceArgumentName, out var arg) &&
             context.SemanticModel.TryGetConstantValue(arg.Expression, context.CancellationToken, out string @namespace) &&
             context.Compilation.GetSymbolsWithName(x => !string.IsNullOrEmpty(x) && @namespace.EndsWith(x), SymbolFilter.Namespace)
             .All(x => x.ToMinimalDisplayString(context.SemanticModel, 0) != @namespace))
         {
             context.ReportDiagnostic(Diagnostic.Create(WPF0051XmlnsDefinitionMustMapExistingNamespace.Descriptor, arg.GetLocation(), arg));
         }
         else if (Attribute.IsType(attribute, KnownSymbol.MarkupExtensionReturnTypeAttribute, context.SemanticModel, context.CancellationToken) &&
                  context.ContainingSymbol is ITypeSymbol containingType &&
                  !containingType.IsAbstract &&
                  containingType.IsAssignableTo(KnownSymbol.MarkupExtension, context.Compilation) &&
                  attribute.TryFirstAncestor <ClassDeclarationSyntax>(out var classDeclaration) &&
                  MarkupExtension.TryGetReturnType(classDeclaration, context.SemanticModel, context.CancellationToken, out var returnType) &&
                  returnType != KnownSymbol.Object &&
                  Attribute.TryFindArgument(attribute, 0, "returnType", out arg) &&
                  arg.Expression is TypeOfExpressionSyntax typeOf &&
                  context.SemanticModel.TryGetType(typeOf.Type, context.CancellationToken, out var argType) &&
                  !ReferenceEquals(argType, returnType))
         {
             context.ReportDiagnostic(Diagnostic.Create(WPF0081MarkupExtensionReturnTypeMustUseCorrectType.Descriptor, arg.GetLocation(), returnType));
         }
         else if (Attribute.IsType(attribute, KnownSymbol.ConstructorArgumentAttribute, context.SemanticModel, context.CancellationToken) &&
                  ConstructorArgument.TryGetArgumentName(attribute, out var argument, out var argumentName) &&
                  attribute.TryFirstAncestor <PropertyDeclarationSyntax>(out var propertyDeclaration) &&
                  context.SemanticModel.TryGetSymbol(propertyDeclaration, context.CancellationToken, out var property) &&
                  ConstructorArgument.TryGetParameterName(property, context.SemanticModel, context.CancellationToken, out var parameterName) &&
                  argumentName != parameterName)
         {
             context.ReportDiagnostic(
                 Diagnostic.Create(
                     WPF0082ConstructorArgument.Descriptor,
                     argument.GetLocation(),
                     ImmutableDictionary.CreateRange(new[] { new KeyValuePair <string, string>(nameof(ConstructorArgument), parameterName) }),
                     parameterName));
         }