예제 #1
0
        private static (bool found, string className, Node toGenerateNode) GetNode(
            string classIdentifier,
            AbstractSyntaxTree ast
            )
        {
            var className = DotNetClassNormalizer.Denormalize(classIdentifier);

            var toGenerateNode = ast.OfKind(SyntaxKind.ClassDeclaration)
                                 .FirstOrDefault(a => a.IdentifierStr == className);

            if (toGenerateNode == null)
            {
                toGenerateNode = ast.OfKind(SyntaxKind.InterfaceDeclaration)
                                 .FirstOrDefault(a => a.IdentifierStr == className);
                if (toGenerateNode == null)
                {
                    return(false, className, toGenerateNode);
                }
            }
            return(true, className, toGenerateNode);
        }
        public static ClassStatement Generate(
            string projectAssembly,
            string classIdentifier,
            TypeScriptAST ast,
            IDictionary <string, string> typeOverrideMap
            )
        {
            //var namespaceIdentifier = string.Join(".", identifier.Take(identifier.Count - 1)); // "Bablyon";
            var className = DotNetClassNormalizer.Denormalize(classIdentifier);

            var toGenerateNode = ast.OfKind(SyntaxKind.ClassDeclaration)
                                 .FirstOrDefault(a => a.IdentifierStr == className);

            if (toGenerateNode == null)
            {
                toGenerateNode = ast.OfKind(SyntaxKind.InterfaceDeclaration)
                                 .FirstOrDefault(a => a.IdentifierStr == className);
                if (toGenerateNode == null)
                {
                    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),
                        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),
                        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);
        }