Beispiel #1
0
        public static GenericTrivia GetTokenTrivia(int tokenIndex, CSharpNode csNode)
        {
            switch (tokenIndex)
            {
            case 0:
                return(GenericTrivia.Empty);

            case 1:
                return((csNode.Visibility == Visibility.Public)
                        ? new GenericTrivia {
                    Left = GlobalTsInfo.SpaceToken, Right = GlobalTsInfo.Void
                }
                        : GenericTrivia.Empty);

            case 2:
                return(new GenericTrivia {
                    Left = GlobalTsInfo.SpaceToken, Right = GlobalTsInfo.Void
                });

            case 3:
                return(new GenericTrivia {
                    Left = GlobalTsInfo.SpaceToken, Right = GlobalTsInfo.LineJump
                });

            case 4:
                return(new GenericTrivia {
                    Left = GlobalTsInfo.Void, Right = GlobalTsInfo.LineJump
                });

            default:
                return(GenericTrivia.Empty);
            }
        }
        public static GenericTrivia GetTokenTrivia(int tokenIndex, CSharpNode node)
        {
            switch (tokenIndex)
            {
            case 0:
                if (node.IsReadOnly)
                {
                    return new GenericTrivia()
                           {
                               Left = GlobalTsInfo.SpaceToken, Right = GlobalTsInfo.Void
                           }
                }
                ;
                return(GenericTrivia.Empty);

            case 1:
                return(new GenericTrivia()
                {
                    Left = GlobalTsInfo.SpaceToken, Right = GlobalTsInfo.ToTypeToken
                });

            default:
                return(new GenericTrivia()
                {
                    Left = GlobalTsInfo.SpaceToken,
                    Right = GlobalTsInfo.CloseExpressionToken + GlobalTsInfo.LineJump
                });
            }
        }
        public void Compile()
        {
            Assembly assembly = Assembly.LoadFrom(Configuration.InputPath);

            var markerAttribute = GetAssemblyAttribute(assembly);

            foreach (var type in markerAttribute.Types ?? Enumerable.Empty <Type>())
            {
                var head = new CSharpNode(type.Name, TypeConversion.FromCSharpReflection(type));
                if (head.CSNodeType.Equals(CSharpNodeType.None))
                {
                    continue;
                }
                head.Visibility = Visibility.Public;

                Heads.Add(head);

                foreach (var property in type.GetProperties())
                {
                    InsertPropertyNodeInTree(property);
                }

                Trees.Add(Generator.GetSyntaxTree(head));
            }

            Trees.ForEach(tree => Generator.LinkingResolver.Resolve(tree));
        }
        public ISyntaxTree GetSyntaxTree(CSharpNode head)
        {
            _size = 0;
            CurrentHeadIdentifier = head.Identifier;
            GenericNode node = BuildGenericTree(head);

            return(new TypeScriptSyntaxTree(node, _size, head.Identifier));
        }
        private void CreateCSharpHeadNode(SyntaxNode root)
        {
            var    nodeType = TypeConversion.FromRoslynKind(root.Kind());
            string name     = Extractors.ExtractName(root);

            Head = new CSharpNode(name, nodeType)
            {
                Visibility = Configuration.Visibility
            };
        }
        private void InsertPropertyNodeInTree(PropertyInfo propertyInfo)
        {
            var node = new CSharpNode(propertyInfo.Name, CSharpNodeType.Property);

            node.Type = new TypeWrapper()
            {
                Kind           = TypeConversion.KindFromReflectionType(propertyInfo.PropertyType),
                RawKind        = TypeConversion.RawKindFromReflectionType(propertyInfo.PropertyType),
                UnderlyingKind = TypeConversion.UnderlyingKindFromReflectionType(propertyInfo.PropertyType)
            };
            Heads.Last().Children.Add(node);
        }
        private void ProcessLinking(CSharpNode node)
        {
            if (node.CSNodeType == CSharpNodeType.Class ||
                node.CSNodeType == CSharpNodeType.Interface)
            {
                LinkingResolver.Add(CurrentHeadIdentifier, new List <string>());
                return;
            }

            if (node.Type.Kind == SyntaxKind.IdentifierName && !PropertyTsInfo.HasKnowIdentifier(node.Identifier))
            {
                LinkingResolver.Push(CurrentHeadIdentifier, node.Type.RawKind);
            }
        }
        private GenericTrivia GetTokenTrivia(CSharpNode node, int tokenIndex)
        {
            switch ((Kind)node.CSNodeType)
            {
            case Kind.Class:
            case Kind.Interface:
                return(InterfaceInfo.GetTokenTrivia(tokenIndex, node));

            case Kind.Field:
            case Kind.Property:
                return(PropertyTsInfo.GetTokenTrivia(tokenIndex, node));

            default:
                return(GenericTrivia.Empty);
            }
        }
        private GenericNode BuildGenericTree(CSharpNode head)
        {
            GenericNode node = CreateGenericNode(head);

            _size++;

            ProcessLinking(head);

            foreach (var cSharpNode in head.Children)
            {
                GenericNode child = BuildGenericTree(cSharpNode);
                GenericNodeTree.AddChild(ref node, ref child);
                _size++;
            }
            return(node);
        }
        private string GetTokenContent(CSharpNode node, int tokenIndex)
        {
            switch ((Kind)node.CSNodeType)
            {
            case Kind.Class:
            case Kind.Interface:
                return(InterfaceInfo.TokenContent(tokenIndex, node));

            case Kind.Field:
            case Kind.Property:
                return(PropertyTsInfo.TokenContent(tokenIndex, node));

            default:
                return("");
            }
        }
        public static string TokenContent(int tokenIndex, CSharpNode node)
        {
            switch (tokenIndex)
            {
            case 0:
                return((node.IsReadOnly) ? ReadonlyToken : "");

            case 1:
                var text = $"\t{char.ToLowerInvariant(node.Identifier[0]) + node.Identifier.Substring(1)}";
                if (node.Type.UnderlyingKind == SyntaxKind.NullableType)
                {
                    text += GlobalTsInfo.NullableToken;
                }
                return(text);

            default:
                return(GetTsType(node.Type));
            }
        }
        private void InsertNodeInTree(SyntaxNode node)
        {
            SyntaxKind  topLevelKind = node.Kind();
            bool        isReadonly   = false;
            string      name         = "";
            TypeWrapper type         = new TypeWrapper();

            SyntaxNode[] children = node.ChildNodes().ToArray();
            if (children.Length < 1)
            {
                return;
            }

            switch (topLevelKind)
            {
            case SyntaxKind.PropertyDeclaration:
                type       = Extractors.ExtractTypesFromProperty(GetPropertyTypeNode(node));
                name       = Extractors.ExtractName(node);
                isReadonly = Extractors.IsReadOnly(node);
                break;

            case SyntaxKind.FieldDeclaration:
                SyntaxNode firstChild = children.First();
                type = Extractors.ExtractTypesFromField(firstChild);
                name = Extractors.ExtractName(firstChild);
                break;

            case SyntaxKind.MethodDeclaration:
                /*TODO: Method, can have multiple return/parameters type. Handle that when needed*/
                break;
            }

            var csharpNode = new CSharpNode(name, TypeConversion.FromRoslynKind(topLevelKind))
            {
                Type       = type,
                IsReadOnly = isReadonly
            };

            Head.Children.Add(csharpNode);
        }
        private GenericNode CreateGenericNode(CSharpNode node)
        {
            Kind tsKind = (Kind)node.CSNodeType;

            GenericNode gNode = new GenericNode()
            {
                Identifier = node.Identifier,
                Kind       = tsKind
            };

            int size = GetTokenSizeFor(tsKind);

            for (int i = 0; i < size; i++)
            {
                var token = new GenericToken();
                token.Text   = GetTokenContent(node, i);
                token.Trivia = GetTokenTrivia(node, i);
                gNode.Tokens.Add(token);
            }

            return(gNode);
        }
Beispiel #14
0
        public static string TokenContent(int tokenIndex, CSharpNode csNode)
        {
            switch (tokenIndex)
            {
            case 0:
                string text = (csNode.Visibility == Visibility.Public) ? GlobalTsInfo.VisibilityPublicToken : GlobalTsInfo.Void;
                return(text);

            case 1:
                return(InterfaceNameType);

            case 2:
                return(csNode.Identifier);

            case 3:
                return(GlobalTsInfo.OpenContentToken);

            case 4:
                return(GlobalTsInfo.CloseContentToken);

            default:
                return("");
            }
        }
 public AstNode VisitCSharp(CSharpNode n)
 {
     AppendLineAndIndent(n.Code);
     return(n);
 }
Beispiel #16
0
 public virtual AstNode VisitCSharp(CSharpNode n)
 {
     return(n);
 }