private static void Handle(SyntaxNodeAnalysisContext context) { if (!context.IsExcludedFromAnalysis() && context.ContainingSymbol is IMethodSymbol method && context.Node is MethodDeclarationSyntax methodDeclaration) { if (OnPropertyChanged.IsMatch(method, context.SemanticModel, context.CancellationToken, out var parameter) == AnalysisResult.Yes) { if (ShouldBeCallerMemberName(parameter, out var parameterSyntax)) { context.ReportDiagnostic(Diagnostic.Create(Descriptors.INPC004UseCallerMemberName, parameterSyntax !.GetLocation())); } if (ShouldBeProtected() is { } location) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.INPC018InvokerShouldBeProtected, location)); } } else if (TrySet.IsMatch(method, context.SemanticModel, context.CancellationToken, out _, out _, out parameter) == AnalysisResult.Yes) { if (ShouldBeCallerMemberName(parameter, out var parameterSyntax)) { context.ReportDiagnostic(Diagnostic.Create(Descriptors.INPC004UseCallerMemberName, parameterSyntax !.GetLocation())); } if (ShouldBeProtected() is { } location) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.INPC018InvokerShouldBeProtected, location)); } } } bool ShouldBeCallerMemberName(IParameterSymbol candidate, out ParameterSyntax?parameterSyntax) { parameterSyntax = null; return(!candidate.IsCallerMemberName() && candidate.Type == KnownSymbol.String && methodDeclaration.TryFindParameter(candidate.Name, out parameterSyntax) && CallerMemberNameAttribute.IsAvailable(context.SemanticModel)); } Location?ShouldBeProtected() { if (method is { DeclaredAccessibility : Accessibility.Private, ContainingType : { IsSealed : false, IsStatic : false } })
internal static async Task AddOnPropertyChangedMethodAsync(this DocumentEditor editor, ClassDeclarationSyntax classDeclaration, bool nullabilityAnnotationsEnabled, CancellationToken cancellationToken) { var qualifyAccess = classDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword) ? CodeStyleResult.No : await editor.QualifyEventAccessAsync(cancellationToken) .ConfigureAwait(false); _ = editor.AddMethod( classDeclaration, InpcFactory.OnPropertyChangedDeclaration( qualifyAccess, classDeclaration.Modifiers.Any(SyntaxKind.SealedKeyword), classDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword), CallerMemberNameAttribute.IsAvailable(editor.SemanticModel), nullabilityAnnotationsEnabled)); }