Esempio n. 1
0
        private bool TryGetMember(
            TypeScriptInterface interfaceContext,
            CodeProperty property,
            TypeContext typeContext,
            out TypeScriptMember member)
        {
            member = null;
            if (!this.settings.MemberAccessTypes.Contains(property.Access))
            {
                return(false);
            }

            var getter = property.Getter;

            if (getter == null)
            {
                return(false);
            }

            string name = property.Name;

            if (name.StartsWith("@"))
            {
                name = name.Substring(1);
            }

            member = new TypeScriptMember
            {
                Name = name,
                Type = typeContext.GetTypeReference(
                    TypeName.ParseDte(getter.Type.AsFullName),
                    interfaceContext)
            };
            return(true);
        }
Esempio n. 2
0
        private void PopulateBases(
            CodeElements bases,
            TypeScriptInterface interfaceOutput,
            TypeContext typeContext)
        {
            if (bases != null)
            {
                foreach (CodeElement baseElement in bases)
                {
                    if (baseElement.FullName != typeof(Object).FullName)
                    {
                        interfaceOutput.Bases.Add(
                            typeContext.GetTypeReference(
                                TypeName.ParseDte(baseElement.FullName),
                                interfaceOutput));
                    }
                }

                TypeReference parentType = interfaceOutput.Bases.FirstOrDefault();
                if (parentType != null &&
                    BuilderHelper.IsValidBaseType(parentType.SourceType))
                {
                    interfaceOutput.Parent = parentType;
                }
            }
        }
Esempio n. 3
0
        public void DefaultTypescriptVersionYieldsBoolean()
        {
            TypeContext typeContext = new TypeContext();

            var sb     = new StringBuilder();
            var member = new TypeScriptMember
            {
                Name = "Foo",
                Type = typeContext.GetTypeReference(
                    TypeName.FromLiteral(typeof(bool).FullName),
                    contextTypeReference: null)
            };

            var appender = new MemberOutputAppender(
                new OutputSettings
            {
                CompatibilityVersion = null
            },
                new TypeContext());

            appender.AppendOutput(
                sb,
                0,
                member);
            Assert.AreEqual("Foo: boolean;", sb.ToString().Trim());
        }
Esempio n. 4
0
        public void TypescriptVersion083YieldsBool()
        {
            Version     version     = new Version(0, 8, 3);
            TypeContext typeContext = new TypeContext(new TypeContext.Settings()
            {
                CompatibilityVersion = version
            });

            var sb     = new StringBuilder();
            var member = new TypeScriptMember
            {
                Name = "Foo",
                Type = typeContext.GetTypeReference(
                    TypeName.FromLiteral(typeof(bool).FullName),
                    contextTypeReference: null)
            };

            var appender = new MemberOutputAppender(
                new OutputSettings
            {
                CompatibilityVersion = version
            },
                typeContext);

            appender.AppendOutput(
                sb,
                0,
                member);
            Assert.AreEqual("Foo: bool;", sb.ToString().Trim());
        }
Esempio n. 5
0
        private TypeReference GetIndexedType(
            TypeScriptInterface interfaceContext,
            CodeClass codeClass,
            TypeContext typeContext)
        {
            TypeReference result = null;

            if (codeClass.Bases != null)
            {
                TypeName baseName;
                foreach (CodeElement baseClass in codeClass.Bases)
                {
                    baseName = TypeName.ParseDte(baseClass.FullName);
                    if (baseName.UniversalName == typeof(IEnumerable <>).FullName)
                    {
                        result = typeContext.GetTypeReference(
                            baseName.TypeArguments.First(),
                            interfaceContext);
                    }
                }
            }
            return(result);
        }
Esempio n. 6
0
        public TypeScriptInterface Build(
            CodeClass codeClass,
            TypeContext typeContext)
        {
            TypeScriptInterface result = null;

            TypeScriptInterfaceAttributeValues attributeValues = this.GetAttributeValues(codeClass);

            if (attributeValues != null)
            {
                string moduleName = attributeValues.Module;
                if (String.IsNullOrEmpty(moduleName) &&
                    codeClass.Namespace != null)
                {
                    moduleName = codeClass.Namespace.FullName;
                }

                bool interfaceCreated;
                result = typeContext.GetOrCreateInterface(
                    moduleName,
                    TypeName.ParseDte(codeClass.FullName),
                    GetInterfaceName(attributeValues),
                    out interfaceCreated);

                if (!String.IsNullOrEmpty(attributeValues.Extends))
                {
                    result.Parent = typeContext.GetLiteralReference(attributeValues.Extends);
                }
                else if (codeClass.Bases.Count > 0)
                {
                    // Getting the first item directly causes problems in unit tests.  Get it from an enumerator.
                    IEnumerator enumerator = codeClass.Bases.GetEnumerator();
                    enumerator.MoveNext();
                    TypeName parentTypeName = TypeName.ParseDte(
                        ((CodeElement)enumerator.Current).FullName);

                    if (BuilderHelper.IsValidBaseType(parentTypeName))
                    {
                        result.Parent = typeContext.GetTypeReference(
                            parentTypeName,
                            result);
                    }
                }

                result.IndexedType = this.GetIndexedType(
                    result,
                    codeClass,
                    typeContext);

                Traversal.TraverseProperties(
                    codeClass.Members,
                    (property) =>
                {
                    TypeScriptMember member;
                    if (TryGetMember(
                            result,
                            property,
                            typeContext,
                            out member))
                    {
                        result.Fields.Add(member);
                    }
                });
            }
            return(result);
        }
Esempio n. 7
0
        private bool TryGetMember(TypeScriptInterface interfaceContext,
                                  CodeProperty property,
                                  TypeContext typeContext,
                                  out TypeScriptMember member)
        {
            member = null;
            if (property.Access != vsCMAccess.vsCMAccessPublic)
            {
                return(false);
            }

            var getter = property.Getter;

            if (getter == null)
            {
                return(false);
            }

            var values = GetMemberValues(property, typeContext);

            string name;

            if (values.Name != null)
            {
                name = values.Name;
            }
            else
            {
                name = property.Name;
                if (name.StartsWith("@"))
                {
                    name = name.Substring(1);
                }
            }

            TypeReference memberType;

            if (!string.IsNullOrWhiteSpace(values.Type))
            {
                memberType = typeContext.GetLiteralReference(values.Type);
            }
            else
            {
                memberType = typeContext.GetTypeReference(
                    TypeName.ParseDte(getter.Type.AsFullName),
                    interfaceContext);
            }

            member = new TypeScriptMember
            {
                Name = name,
                //FullName = property.FullName,
                Optional = values.Optional,
                Ignore   = values.Ignore,
                Type     = memberType
            };

            if (member.Ignore)
            {
                return(false);
            }

            if (values.CamelCase && values.Name == null)
            {
                member.Name = member.Name.Substring(0, 1).ToLowerInvariant() + member.Name.Substring(1);
            }

            return(true);
        }