private static void Handle(SyntaxNodeAnalysisContext context) { if (!context.IsExcludedFromAnalysis() && context.Node is MemberDeclarationSyntax memberDeclaration) { if (BackingFieldOrProperty.TryCreateForDependencyProperty(context.ContainingSymbol, out var backingMember)) { if (DependencyProperty.TryGetRegisteredName(backingMember, context.SemanticModel, context.CancellationToken, out _, out var registeredName)) { if (backingMember.Type == KnownSymbols.DependencyProperty && !backingMember.Name.IsParts(registeredName, "Property")) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0001BackingFieldShouldMatchRegisteredName, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation(), ImmutableDictionary <string, string> .Empty.Add("ExpectedName", registeredName + "Property"), backingMember.Name, registeredName)); } if (backingMember.Type == KnownSymbols.DependencyPropertyKey && !backingMember.Name.IsParts(registeredName, "PropertyKey")) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0002BackingFieldShouldMatchRegisteredName, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation(), ImmutableDictionary <string, string> .Empty.Add("ExpectedName", registeredName + "PropertyKey"), backingMember.Name, registeredName)); } if (context.ContainingSymbol.ContainingType.TryFindProperty(registeredName, out _) && context.ContainingSymbol.DeclaredAccessibility.IsEither(Accessibility.Protected, Accessibility.Internal, Accessibility.Public) && !HasStandardText(memberDeclaration, registeredName, out var comment)) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0060DocumentDependencyPropertyBackingMember, comment == null ? BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation() : comment.GetLocation(), properties: ImmutableDictionary <string, string> .Empty.Add(nameof(CrefParameterSyntax), registeredName))); } if (DependencyProperty.TryGetRegisteredType(backingMember, context.SemanticModel, context.CancellationToken, out var type) && type.Is(KnownSymbols.Style) && !TryFindStyleTypedPropertyAttribute(memberDeclaration, registeredName, context.SemanticModel, context.CancellationToken) && backingMember.FieldOrProperty.Symbol.DeclaredAccessibility.IsEither(Accessibility.Public, Accessibility.Internal)) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0176StyleTypedPropertyMissing, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation(), ImmutableDictionary <string, string> .Empty.Add( nameof(AttributeListSyntax), $"[StyleTypedProperty(Property = {(context.ContainingSymbol.ContainingType.TryFindProperty(registeredName, out _) ? $"nameof({registeredName})" : $"\"{registeredName}\"")}, StyleTargetType = typeof(TYPE))]"),
private static void Handle(SyntaxNodeAnalysisContext context) { if (!context.IsExcludedFromAnalysis() && context.Node is MemberDeclarationSyntax memberDeclaration && FieldOrProperty.TryCreate(context.ContainingSymbol, out var fieldOrProperty) && fieldOrProperty.Type == KnownSymbol.RoutedEvent) { if (RoutedEvent.TryGetRegisteredName(fieldOrProperty, context.SemanticModel, context.CancellationToken, out var registeredName) && !fieldOrProperty.Name.IsParts(registeredName, "Event")) { context.ReportDiagnostic( Diagnostic.Create( WPF0100BackingFieldShouldMatchRegisteredName.Descriptor, FindIdentifier(context.Node).GetLocation(), ImmutableDictionary <string, string> .Empty.Add("ExpectedName", registeredName + "Event"), fieldOrProperty.Name, registeredName)); } if (RoutedEvent.TryGetRegisteredType(fieldOrProperty, context.SemanticModel, context.CancellationToken, out var typeArg, out var registeredOwnerType) && !Equals(registeredOwnerType, context.ContainingSymbol.ContainingType)) { context.ReportDiagnostic( Diagnostic.Create( WPF0101RegisterContainingTypeAsOwner.Descriptor, typeArg.GetLocation(), fieldOrProperty.ContainingType.Name, registeredName)); } if (!fieldOrProperty.IsStaticReadOnly()) { context.ReportDiagnostic( Diagnostic.Create( WPF0107BackingMemberShouldBeStaticReadonly.Descriptor, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation())); } } }
private static void Handle(SyntaxNodeAnalysisContext context) { if (context.Node is ObjectCreationExpressionSyntax objectCreation && (objectCreation.Type == KnownSymbol.RoutedCommand || objectCreation.Type == KnownSymbol.RoutedUICommand) && context.SemanticModel.TryGetSymbol(objectCreation, context.CancellationToken, out var ctor)) { if (ctor.TryFindParameter("ownerType", out var parameter)) { if (objectCreation.TryFindArgument(parameter, out var ownerTypeArg) && ownerTypeArg.TryGetTypeofValue(context.SemanticModel, context.CancellationToken, out var type) && !type.Equals(context.ContainingSymbol.ContainingType)) { context.ReportDiagnostic( Diagnostic.Create( WPF0121RegisterContainingTypeAsOwnerForRoutedCommand.Descriptor, ownerTypeArg.GetLocation(), context.ContainingSymbol.ContainingType.ToMinimalDisplayString(context.SemanticModel, objectCreation.SpanStart))); } } if (TryGetBackingMember(objectCreation, context, out var fieldOrProperty, out var memberDeclaration)) { if (ctor.TryFindParameter("name", out var nameParameter)) { if (objectCreation.TryFindArgument(nameParameter, out var nameArg) && nameArg.TryGetStringValue(context.SemanticModel, context.CancellationToken, out var registeredName) && registeredName != fieldOrProperty.Name) { context.ReportDiagnostic( Diagnostic.Create( WPF0120RegisterContainingMemberAsNameForRoutedCommand.Descriptor, nameArg.GetLocation(), ImmutableDictionary <string, string> .Empty.Add(nameof(IdentifierNameSyntax), fieldOrProperty.Name), fieldOrProperty.Name)); } } else { context.ReportDiagnostic( Diagnostic.Create( WPF0122RegisterRoutedCommand.Descriptor, objectCreation.ArgumentList.GetLocation(), ImmutableDictionary.CreateRange(new[] { new KeyValuePair <string, string>(nameof(IdentifierNameSyntax), fieldOrProperty.Name), new KeyValuePair <string, string>(nameof(TypeOfExpressionSyntax), context.ContainingSymbol.ContainingType.ToMinimalDisplayString(context.SemanticModel, objectCreation.SpanStart)), }))); } if (!fieldOrProperty.IsStaticReadOnly()) { context.ReportDiagnostic(Diagnostic.Create(WPF0123BackingMemberShouldBeStaticReadonly.Descriptor, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation())); } } } }
private static void Handle(SyntaxNodeAnalysisContext context) { if (!context.IsExcludedFromAnalysis() && context.Node is MemberDeclarationSyntax memberDeclaration) { if (BackingFieldOrProperty.TryCreateForDependencyProperty(context.ContainingSymbol, out var backing)) { if (DependencyProperty.TryGetRegisteredName(backing, context.SemanticModel, context.CancellationToken, out _, out var registeredName)) { if (backing.Type == KnownSymbols.DependencyProperty && !backing.Name.IsParts(registeredName, "Property")) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0001BackingFieldShouldMatchRegisteredName, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation(), ImmutableDictionary <string, string> .Empty.Add("ExpectedName", registeredName + "Property"), backing.Name, registeredName)); } if (backing.Type == KnownSymbols.DependencyPropertyKey && !backing.Name.IsParts(registeredName, "PropertyKey")) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0002BackingFieldShouldMatchRegisteredName, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation(), ImmutableDictionary <string, string> .Empty.Add("ExpectedName", registeredName + "PropertyKey"), backing.Name, registeredName)); } if (context.ContainingSymbol.ContainingType.TryFindProperty(registeredName, out _) && context.ContainingSymbol.DeclaredAccessibility.IsEither(Accessibility.Protected, Accessibility.Internal, Accessibility.Public)) { var summaryFormat = "<summary>Identifies the <see cref=\"{backing}\"/> dependency property.</summary>"; if (memberDeclaration.TryGetDocumentationComment(out var comment)) { if (comment.VerifySummary(summaryFormat, registeredName) is { } summaryError) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0060DocumentDependencyPropertyBackingMember, summaryError.Location, ImmutableDictionary <string, string> .Empty.Add(nameof(DocComment), summaryError.Text))); } } else { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0060DocumentDependencyPropertyBackingMember, backing.Symbol.Locations[0], ImmutableDictionary <string, string> .Empty.Add( nameof(DocComment), $"/// {DocComment.Format(summaryFormat, registeredName)}"))); } } if (DependencyProperty.TryGetRegisteredType(backing, context.SemanticModel, context.CancellationToken, out var type) && type.Is(KnownSymbols.Style) && !TryFindStyleTypedPropertyAttribute(memberDeclaration, registeredName, context.SemanticModel, context.CancellationToken) && backing.FieldOrProperty.Symbol.DeclaredAccessibility.IsEither(Accessibility.Public, Accessibility.Internal)) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0176StyleTypedPropertyMissing, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation(), ImmutableDictionary <string, string> .Empty.Add( nameof(AttributeListSyntax), $"[StyleTypedProperty(Property = {(context.ContainingSymbol.ContainingType.TryFindProperty(registeredName, out _) ? $"nameof({registeredName})" : $"\"{registeredName}\"")}, StyleTargetType = typeof(TYPE))]"),
private static void Handle(SyntaxNodeAnalysisContext context) { if (!context.IsExcludedFromAnalysis() && context.Node is MemberDeclarationSyntax memberDeclaration && FieldOrProperty.TryCreate(context.ContainingSymbol, out var fieldOrProperty) && fieldOrProperty.Type == KnownSymbols.RoutedEvent) { if (RoutedEvent.TryGetRegisteredName(fieldOrProperty, context.SemanticModel, context.CancellationToken, out var nameArg, out var registeredName)) { if (!fieldOrProperty.Name.IsParts(registeredName, "Event")) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0100BackingFieldShouldMatchRegisteredName, FindIdentifier(context.Node).GetLocation(), ImmutableDictionary <string, string> .Empty.Add("ExpectedName", registeredName + "Event"), fieldOrProperty.Name, registeredName)); } if (fieldOrProperty.ContainingType.TryFindEvent(registeredName, out var eventSymbol)) { if (nameArg.Expression is LiteralExpressionSyntax) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0150UseNameofInsteadOfLiteral, nameArg.GetLocation(), ImmutableDictionary <string, string> .Empty.Add(nameof(IdentifierNameSyntax), eventSymbol.Name), eventSymbol.Name)); } else if (!nameArg.Expression.IsNameof()) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0151UseNameofInsteadOfConstant, nameArg.GetLocation(), ImmutableDictionary <string, string> .Empty.Add(nameof(IdentifierNameSyntax), eventSymbol.Name), eventSymbol.Name)); } } if (context.ContainingSymbol.ContainingType.TryFindEvent(registeredName, out _) && context.ContainingSymbol.DeclaredAccessibility.IsEither(Accessibility.Protected, Accessibility.Internal, Accessibility.Public) && !HasStandardText(memberDeclaration, registeredName, out var comment)) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0108DocumentRoutedEventBackingMember, comment == null ? BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation() : comment.GetLocation(), properties: ImmutableDictionary <string, string> .Empty.Add(nameof(CrefParameterSyntax), registeredName))); } } if (RoutedEvent.TryGetRegisteredType(fieldOrProperty, context.SemanticModel, context.CancellationToken, out var typeArg, out var registeredOwnerType) && !Equals(registeredOwnerType, context.ContainingSymbol.ContainingType)) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0101RegisterContainingTypeAsOwner, typeArg.GetLocation(), fieldOrProperty.ContainingType.Name, registeredName)); } if (!fieldOrProperty.IsStaticReadOnly()) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0107BackingMemberShouldBeStaticReadonly, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation())); } } }
private static void Handle(SyntaxNodeAnalysisContext context) { if (!context.IsExcludedFromAnalysis() && context.Node is MemberDeclarationSyntax memberDeclaration && FieldOrProperty.TryCreate(context.ContainingSymbol, out var backing) && backing.Type == KnownSymbols.RoutedEvent) { if (RoutedEvent.TryGetRegisteredName(backing, context.SemanticModel, context.CancellationToken, out var nameArg, out var registeredName)) { if (!backing.Name.IsParts(registeredName, "Event")) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0100BackingFieldShouldMatchRegisteredName, backing.Symbol.Locations[0], ImmutableDictionary <string, string> .Empty.Add("ExpectedName", registeredName + "Event"), backing.Name, registeredName)); } if (backing.ContainingType.TryFindEvent(registeredName, out var eventSymbol)) { if (nameArg.Expression is LiteralExpressionSyntax) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0150UseNameofInsteadOfLiteral, nameArg.GetLocation(), ImmutableDictionary <string, string> .Empty.Add(nameof(IdentifierNameSyntax), eventSymbol.Name), eventSymbol.Name)); } else if (!nameArg.Expression.IsNameof()) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0151UseNameofInsteadOfConstant, nameArg.GetLocation(), ImmutableDictionary <string, string> .Empty.Add(nameof(IdentifierNameSyntax), eventSymbol.Name), eventSymbol.Name)); } } if (context.ContainingSymbol.ContainingType.TryFindEvent(registeredName, out _) && context.ContainingSymbol.DeclaredAccessibility.IsEither(Accessibility.Protected, Accessibility.Internal, Accessibility.Public)) { var summaryFormat = "<summary>Identifies the <see cref=\"{registered_name}\"/> routed event.</summary>"; if (memberDeclaration.TryGetDocumentationComment(out var comment)) { if (comment.VerifySummary(summaryFormat, registeredName) is { } summaryError) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0108DocumentRoutedEventBackingMember, summaryError.Location, ImmutableDictionary <string, string> .Empty.Add(nameof(DocComment), summaryError.Text))); } } else { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0108DocumentRoutedEventBackingMember, backing.Symbol.Locations[0], ImmutableDictionary <string, string> .Empty.Add( nameof(DocComment), $"/// {DocComment.Format(summaryFormat, registeredName)}"))); } } } if (RoutedEvent.TryGetRegisteredType(backing, context.SemanticModel, context.CancellationToken, out var typeArg, out var registeredOwnerType) && !Equals(registeredOwnerType, context.ContainingSymbol.ContainingType)) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0101RegisterContainingTypeAsOwner, typeArg.GetLocation(), backing.ContainingType.Name, registeredName)); } if (!backing.IsStaticReadOnly()) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0107BackingMemberShouldBeStaticReadonly, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation())); } } }
private static void Handle(SyntaxNodeAnalysisContext context) { if (!context.IsExcludedFromAnalysis() && context.Node is MemberDeclarationSyntax memberDeclaration) { if (BackingFieldOrProperty.TryCreateForDependencyProperty(context.ContainingSymbol, out var backingMember)) { if (DependencyProperty.TryGetRegisteredName(backingMember, context.SemanticModel, context.CancellationToken, out var registeredName)) { if (backingMember.Type == KnownSymbol.DependencyProperty && !backingMember.Name.IsParts(registeredName, "Property")) { context.ReportDiagnostic( Diagnostic.Create( WPF0001BackingFieldShouldMatchRegisteredName.Descriptor, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation(), ImmutableDictionary <string, string> .Empty.Add("ExpectedName", registeredName + "Property"), backingMember.Name, registeredName)); } if (backingMember.Type == KnownSymbol.DependencyPropertyKey && !backingMember.Name.IsParts(registeredName, "PropertyKey")) { context.ReportDiagnostic( Diagnostic.Create( WPF0002BackingFieldShouldMatchRegisteredName.Descriptor, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation(), ImmutableDictionary <string, string> .Empty.Add("ExpectedName", registeredName + "PropertyKey"), backingMember.Name, registeredName)); } if (context.ContainingSymbol.DeclaredAccessibility.IsEither( Accessibility.Protected, Accessibility.Internal, Accessibility.Public) && context.ContainingSymbol.ContainingType.TryFindProperty(registeredName, out _) && !HasStandardText(memberDeclaration, registeredName, out var comment)) { context.ReportDiagnostic( Diagnostic.Create( WPF0060DocumentDependencyPropertyBackingMember.Descriptor, comment == null ? BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation() : comment.GetLocation())); } } if (context.Node is FieldDeclarationSyntax fieldDeclaration && DependencyProperty.TryGetDependencyPropertyKeyField( backingMember, context.SemanticModel, context.CancellationToken, out var keyField) && Equals(backingMember.ContainingType, keyField.ContainingType) && keyField.TryGetSyntaxReference(out var reference)) { var keyNode = reference.GetSyntax(context.CancellationToken); if (ReferenceEquals(fieldDeclaration.SyntaxTree, keyNode.SyntaxTree) && fieldDeclaration.SpanStart < keyNode.SpanStart) { context.ReportDiagnostic( Diagnostic.Create( WPF0031FieldOrder.Descriptor, fieldDeclaration.GetLocation(), keyField.Name, backingMember.Name)); } } } if (BackingFieldOrProperty.TryCreateCandidate(context.ContainingSymbol, out var candidate) && DependencyProperty.TryGetRegisterInvocationRecursive(candidate, context.SemanticModel, context.CancellationToken, out _, out _) && !candidate.FieldOrProperty.IsStaticReadOnly()) { context.ReportDiagnostic( Diagnostic.Create( WPF0030BackingFieldShouldBeStaticReadonly.Descriptor, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation(), candidate.Name, candidate.Type.Name)); } } }