示例#1
0
        public static IList <AccessorStatement> FlattenAccessorStatements(
            this IEnumerable <Node> nodes,
            AbstractSyntaxTree ast,
            ClassMetadata classMetadata,
            IDictionary <string, string> typeOverrideMap
            )
        {
            var flattenedAccessorList = nodes.Where(a => IsGetterRule.Check(a)).Select(
                accessor =>
            {
                var name                = accessor.IdentifierStr;
                var isStatic            = IsStaticRule.Check(accessor);
                var typeOverrideDetails = new TypeOverrideDetails
                {
                    IsStatic        = isStatic,
                    TypeOverrideMap = typeOverrideMap,
                };
                var type = GenericTypeIdentifier.Identify(
                    accessor.Last,
                    classMetadata,
                    ast,
                    typeOverrideDetails
                    );
                if (TypeOverrideIdentifier.Identify(
                        TypeOverrideDeclarationIdentifier.Identify(
                            classMetadata,
                            isStatic,
                            name
                            ),
                        typeOverrideMap,
                        type,
                        out var overrideType
                        ))
                {
                    type = overrideType;
                }
                return(new AccessorStatement
                {
                    Name = name,
                    Type = NormalizeLiteralTypeStatement(
                        type,
                        classMetadata,
                        ast,
                        typeOverrideDetails
                        ),
                    IsStatic = isStatic,
                    IsInterfaceResponse = InterfaceResponseTypeIdentifier.Identify(
                        type,
                        ast
                        ),
                    HasSetter = IsSetterRule.Check(accessor),
                    //IsArrayResponse = IsArrayResposneTypeRule.Check(accessor),
                    UsedClassNames = UsedClassNamesIdentifier.Identify(type),
                });
            }
                ).ToList();

            // Loop through Setters and on the setter flat as HasSetter
            foreach (var node in nodes.Where(a => IsSetterRule.Check(a)))
            {
                var getNode = flattenedAccessorList.FirstOrDefault(
                    a => a.Name == node.IdentifierStr
                    );
                if (getNode != null)
                {
                    getNode.HasSetter = true;
                }
            }
            return(flattenedAccessorList);
        }
示例#2
0
        public static ClassStatement Generate(
            string projectAssembly,
            string classIdentifier,
            AbstractSyntaxTree ast,
            IDictionary <string, string> typeOverrideMap
            )
        {
            var(found, className, toGenerateNode) = GetNode(
                classIdentifier,
                ast
                );
            if (!found)
            {
                return(null);
            }

            var namespaceIdentifier = string.Join(".", GetNamespace(toGenerateNode));
            var classMetadata       = new ClassMetadata
            {
                Namespace = namespaceIdentifier,
                Name      = className,
            };


            var typeOverrideDetails = new TypeOverrideDetails
            {
                IsStatic        = false,
                TypeOverrideMap = typeOverrideMap,
            };
            // Get ExtendedClassNames
            var extendedClassType = ExtendedClassTypesIdentifier.Identify(
                toGenerateNode,
                ast,
                classMetadata,
                typeOverrideDetails
                );
            // Get ImplementedInterfaces
            var implementedInterfaces = ImplementedInterfacesIdentifier.Identify(
                toGenerateNode,
                ast,
                classMetadata,
                typeOverrideDetails
                );

            // Public Properties
            var publicProperties = toGenerateNode
                                   .Children.Where(
                child => IsNotPrivate(child) && IsPropertyType(child, classMetadata)
                );

            // Public Methods/Functions
            var publicMethods = toGenerateNode
                                .Children.Where(
                child => IsNotPrivate(child) && IsMethodType(child, classMetadata)
                );

            // Get/Set Accessors
            var accessorMethods = toGenerateNode
                                  .Children.Where(
                child => IsNotPrivate(child) && IsAccessorType(child)
                );

            // Is Observer Method/Function
            var observalbleMethods = publicProperties.Where(
                a => IsObservablePropertyRule.Check(a)
                ).ToList();

            var classStatement = new ClassStatement
            {
                ProjectAssembly = projectAssembly,
                Namespace       = namespaceIdentifier,
                Name            = DotNetClassNormalizer.Normalize(
                    className
                    ),
                IsInterface = IsInterfaceRule.Check(
                    toGenerateNode
                    ),
                GenericTypes = GetGenericTypes(
                    toGenerateNode,
                    classMetadata,
                    ast,
                    new TypeOverrideDetails
                {
                    IsStatic        = false,
                    TypeOverrideMap = typeOverrideMap,
                }
                    ),
                ExtendedType          = extendedClassType,
                ImplementedInterfaces = implementedInterfaces,
                ConstructorStatement  = new ConstructorStatement
                {
                    Arguments = ConstructorArgumentIdentifier.Identify(
                        toGenerateNode,
                        classMetadata,
                        ast,
                        new TypeOverrideDetails
                    {
                        IsStatic        = false,
                        TypeOverrideMap = typeOverrideMap,
                    }
                        ),
                },
                PublicPropertyStatements = publicProperties.ToList().Select(
                    a =>
                {
                    var name                = a.IdentifierStr;
                    var isStatic            = IsStaticRule.Check(a);
                    var typeOverrideDetails = new TypeOverrideDetails
                    {
                        IsStatic        = isStatic,
                        TypeOverrideMap = typeOverrideMap,
                    };
                    var type = GenericTypeIdentifier.Identify(
                        a.Last,
                        classMetadata,
                        ast,
                        typeOverrideDetails
                        );
                    if (TypeOverrideIdentifier.Identify(
                            TypeOverrideDeclarationIdentifier.Identify(
                                classMetadata,
                                typeOverrideDetails.IsStatic,
                                name
                                ),
                            typeOverrideDetails.TypeOverrideMap,
                            type,
                            out var overrideType
                            ))
                    {
                        type = overrideType;
                    }
                    return(new PublicPropertyStatement
                    {
                        Name = name,
                        Type = NormalizeLiteralTypeStatement(
                            type,
                            classMetadata,
                            ast,
                            typeOverrideDetails
                            ),
                        IsStatic = isStatic,
                        IsInterfaceResponse = InterfaceResponseTypeIdentifier.Identify(
                            type,
                            ast
                            ),
                        //IsArrayResponse = IsArrayResposneTypeRule.Check(a),
                        IsReadonly = IsReadonlyRule.Check(a),
                        UsedClassNames = UsedClassNamesIdentifier.Identify(type),
                    });
                }
                    ).ToList(),
                PublicMethodStatements = publicMethods.ToList().Select(
                    a =>
                {
                    var name                = a.IdentifierStr;
                    var isStatic            = IsStaticRule.Check(a);
                    var typeOverrideDetails = new TypeOverrideDetails
                    {
                        IsStatic        = isStatic,
                        TypeOverrideMap = typeOverrideMap,
                    };
                    var type = GenericTypeIdentifier.Identify(
                        a.Last,
                        classMetadata,
                        ast,
                        typeOverrideDetails
                        );
                    if (TypeOverrideIdentifier.Identify(
                            TypeOverrideDeclarationIdentifier.Identify(
                                classMetadata,
                                typeOverrideDetails.IsStatic,
                                name
                                ),
                            typeOverrideDetails.TypeOverrideMap,
                            type,
                            out var overrideType
                            ))
                    {
                        type = overrideType;
                    }
                    return(new PublicMethodStatement
                    {
                        Name = name,
                        Type = NormalizeLiteralTypeStatement(
                            type,
                            classMetadata,
                            ast,
                            typeOverrideDetails
                            ),
                        GenericTypes = DeclarationGenericTypesIdentifier.Identify(
                            a
                            ),
                        Arguments = ArgumentIdentifier.Identify(
                            a,
                            classMetadata,
                            ast,
                            typeOverrideDetails
                            ),
                        IsStatic = isStatic,
                        IsInterfaceResponse = InterfaceResponseTypeIdentifier.Identify(
                            type,
                            ast
                            ),
                        UsedClassNames = UsedClassNamesIdentifier.Identify(type),
                    });
                }
                    ).Distinct().ToList(),
                AccessorStatements = accessorMethods.FlattenAccessorStatements(
                    ast,
                    classMetadata,
                    typeOverrideMap
                    ),
            };

            classStatement.ConstructorStatement.NeedsInvokableReference = InvokableReferenceIdentifier.Identify(
                classStatement
                );

            return(classStatement);
        }