예제 #1
0
        public bool IsMarkedWithAttribute(ISymbol type, string attributeName)
        {
            if (_symbolAttributes.ContainsKey(type) == false)
            {
                _symbolAttributes.Add(type, new Dictionary <string, bool>());
            }

            var bucket = _symbolAttributes[type];

            if (bucket.ContainsKey(attributeName) == false)
            {
                bucket[attributeName] = SymbolHelper.IsMarkedWithAttribute(@type, attributeName);
            }

            return(bucket[attributeName]);
        }
        private IEnumerable <ISymbol> GetMembersForRequiredInit(ITypeSymbol type, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel)
        {
            var membersExtractor = new MembersExtractor(semanticModel, objectCreation);

            if (IsInsideInitBlockWithFullInit(objectCreation) ||
                SymbolHelper.IsMarkedWithAttribute(type, SmartAnnotations.InitRequired) ||
                SymbolHelper.IsMarkedWithAttribute(type, SmartAnnotations.InitOnly))
            {
                return(membersExtractor.GetAllMembersThatCanBeInitialized(type));
            }

            var symbolCache = new SymbolHelperCache();

            return(membersExtractor.GetAllMembersThatCanBeInitialized(type).Where(memberSymbol =>
                                                                                  SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitRequired) ||
                                                                                  SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitOnly) ||
                                                                                  NonNullableShouldBeInitialized(memberSymbol, symbolCache)));
        }
 private void AnalyzeSymbol(SymbolAnalysisContext context)
 {
     if (context.Symbol is INamedTypeSymbol namedType)
     {
         foreach (var twinType in SymbolHelper.GetTwinTypes(namedType))
         {
             var ownMembers     = GetMembers(namedType);
             var twinMembers    = GetMembers(twinType.Type).Except(twinType.IgnoredMembers);
             var missingMembers = twinMembers.Except(ownMembers);
             if (missingMembers.IsEmpty == false)
             {
                 var propertiesString = string.Join("\r\n", missingMembers.Select(x => $"- {x}"));
                 var diagnostic       = Diagnostic.Create(Rule, context.Symbol.Locations[0], twinType.Type.ToDisplayString(), propertiesString);
                 context.ReportDiagnostic(diagnostic);
             }
         }
     }
 }
        private void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            if (context.Symbol is INamedTypeSymbol namedType && (namedType.TypeKind == TypeKind.Class || namedType.TypeKind == TypeKind.Struct))
            {
                foreach (var twinType in SymbolHelper.GetTwinTypes(namedType))
                {
                    var missingMembers = twinType.GetMissingMembersFor(namedType);
                    if (missingMembers.Count > 0)
                    {
                        var propertiesString = string.Join("\r\n", missingMembers.Select(x => $"- {x.ExpectedName}"));
                        var properties       = new Dictionary <string, string>()
                        {
                            ["TwinType"] = twinType.Type.ToDisplayString()
                        };
                        var diagnostic = Diagnostic.Create(Rule, context.Symbol.Locations[0], properties.ToImmutableDictionary(), twinType.Type.ToDisplayString(), propertiesString);

                        context.ReportDiagnostic(diagnostic);
                    }
                }
            }
        }
예제 #5
0
        private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
        {
            var assignment = (AssignmentExpressionSyntax)context.Node;

            if (assignment.Parent is InitializerExpressionSyntax || assignment.Left == null)
            {
                return;
            }

            var memberSymbol = context.SemanticModel.GetSymbolInfo(assignment.Left).Symbol;

            if (memberSymbol is IPropertySymbol || memberSymbol is IFieldSymbol)
            {
                if (SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitOnly) || SymbolHelper.IsMarkedWithAttribute(memberSymbol.ContainingType, SmartAnnotations.InitOnly))
                {
                    var parentMethod = SyntaxHelper.FindNearestContainer <BaseMethodDeclarationSyntax>(assignment.Parent);
                    if (parentMethod is ConstructorDeclarationSyntax)
                    {
                        var constructorSymbol = context.SemanticModel.GetDeclaredSymbol(parentMethod);
                        if (constructorSymbol != null && constructorSymbol.ContainingType == memberSymbol.ContainingType)
                        {
                            return;
                        }
                    }
                    var diagnostic = Diagnostic.Create(Rule, assignment.GetLocation());
                    context.ReportDiagnostic(diagnostic);
                }
            }
        }
예제 #6
0
 private static bool IsInitOnlyMember(ISymbol memberSymbol)
 {
     return(SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitOnly) ||
            SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitOnlyOptional) ||
            SymbolHelper.IsMarkedWithAttribute(memberSymbol.ContainingType, SmartAnnotations.InitOnly));
 }