public virtual void Validate(DtoRules dtoRules)
        {
            if (dtoRules.DtoRulesSymbol
                .Constructors.Length > 1)
            {
                throw new NotSupportedException("Multiple constructor is not supported");
            }

            if (dtoRules.ClassDeclaration.Members.OfType <MethodDeclarationSyntax>()
                .GroupBy(m => m.Identifier.ValueText)
                .Any(mOverloads => mOverloads.Count() > 1))
            {
                throw new NotSupportedException("Method overloading is not supported");
            }

            DtoRulesNotSupportedCSharpFeaturesVisitor dtoRulesNotSupportedCSharpFeaturesVisitor = new DtoRulesNotSupportedCSharpFeaturesVisitor(dtoRules.SemanticModel);

            dtoRulesNotSupportedCSharpFeaturesVisitor.Visit(dtoRules.ClassRootNode);
        }
Exemple #2
0
        public async Task TestNotSupported(string testCode, string message)
        {
            Project proj = CreateProjectFromSourceCodes(Codes.BitDtoRules, testCode);

            DtoRules productDtoRules = (await new DefaultProjectDtoRulesProvider().GetProjectAllDtoRules(proj)).Single();

            try
            {
                new DefaultDtoRulesValidator().Validate(productDtoRules);
                Assert.Fail();
            }
            catch (NotSupportedException ex)
            {
                if (ex.Message != message)
                {
                    Assert.Fail();
                }
            }
        }
        public virtual async Task <IList <DtoRules> > GetProjectAllDtoRules(Project project, IList <Project>?allSourceProjects = null)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            IList <DtoRules> allDtoRules = new List <DtoRules>();

            foreach (Document doc in project.Documents)
            {
                if (!doc.SupportsSemanticModel)
                {
                    continue;
                }

                SemanticModel semanticModel = await doc.GetSemanticModelAsync();

                SyntaxNode root = await doc.GetSyntaxRootAsync();

                List <ClassDeclarationSyntax> allDtoRulesClasses = new List <ClassDeclarationSyntax>();

                foreach (ClassDeclarationSyntax classDeclarationSyntax in root.DescendantNodes()
                         .OfType <ClassDeclarationSyntax>())
                {
                    if (classDeclarationSyntax.BaseList == null)
                    {
                        continue;
                    }

                    bool isDtoRules = classDeclarationSyntax.BaseList.Types.Select(t => t.Type)
                                      .Select(t => semanticModel.GetSymbolInfo(t).Symbol?.OriginalDefinition?.ToString())
                                      .Any(t => t == "Bit.Owin.DtoRules.DtoRules<TDto>");

                    if (isDtoRules == true)
                    {
                        isDtoRules = semanticModel.GetDeclaredSymbol(classDeclarationSyntax)
                                     .GetAttributes()
                                     .Any(att => att.AttributeClass.Name == "AutoGenerateAttribute");
                    }

                    if (isDtoRules == true)
                    {
                        allDtoRulesClasses.Add(classDeclarationSyntax);
                    }
                }

                if (!allDtoRulesClasses.Any())
                {
                    continue;
                }

                foreach (ClassDeclarationSyntax dtoRulesClassDec in allDtoRulesClasses)
                {
                    INamedTypeSymbol dtoRuleSymbol = (INamedTypeSymbol)semanticModel.GetDeclaredSymbol(dtoRulesClassDec);

                    DtoRules dtoRules = new DtoRules
                    {
                        DtoRulesSymbol   = dtoRuleSymbol,
                        Name             = dtoRuleSymbol.Name,
                        ClassDeclaration = dtoRulesClassDec,
                        DtoSymbol        = (dtoRuleSymbol.BaseType.TypeArguments).ExtendedSingle($"Looking for dto of {dtoRuleSymbol.Name}"),
                        DtoRulesDocument = doc,
                        SemanticModel    = semanticModel,
                        ClassSyntaxTree  = dtoRulesClassDec.SyntaxTree
                    };

                    dtoRules.ClassRootNode = (CompilationUnitSyntax)dtoRules.ClassSyntaxTree.GetRoot();

                    allDtoRules.Add(dtoRules);
                }
            }

            return(allDtoRules);
        }