Esempio n. 1
0
        private void WriteEvent(TypeConfigItem ev, string name, bool adder)
        {
            XmlToJsDoc.EmitComment(this, ev.Entity, adder);
            this.Write(name);
            this.WriteOpenParentheses();
            this.Write("value");
            this.WriteColon();
            string typeName = BridgeTypes.ToTypeScriptName(ev.Entity.ReturnType, this.Emitter);

            this.Write(typeName);

            var resolveResult = this.Emitter.Resolver.ResolveNode(ev.Entity.ReturnType, this.Emitter);

            if (resolveResult != null && (resolveResult.Type.IsReferenceType.HasValue && resolveResult.Type.IsReferenceType.Value || resolveResult.Type.IsKnownType(KnownTypeCode.NullableOfT)))
            {
                this.Write(" | null");
            }

            this.WriteCloseParentheses();
            this.WriteColon();
            this.Write("void");

            this.WriteSemiColon();
            this.WriteNewLine();
        }
Esempio n. 2
0
        protected virtual void EmitClassEnd()
        {
            if (this.Position != this.Emitter.Output.Length && !this.Emitter.IsNewLine)
            {
                this.WriteNewLine();
            }

            var isInterface = this.Emitter.TypeInfo.TypeDeclaration.ClassType == ICSharpCode.NRefactory.CSharp.ClassType.Interface;

            this.EndBlock();

            if (this.IsGeneric && !isInterface)
            {
                this.WriteNewLine();
                this.EndBlock();
            }

            if (this.TypeInfo.ParentType == null && !isInterface)
            {
                string name = BridgeTypes.ToTypeScriptName(this.TypeInfo.Type, this.Emitter, true, true);
                this.WriteNewLine();
                this.Write("var ");
                this.Write(name);
                this.WriteColon();

                this.Write(name + "Func");

                this.WriteSemiColon();
            }

            this.WriteNestedTypes();
        }
Esempio n. 3
0
        protected virtual void EmitMethodParameters(IEnumerable <ParameterDeclaration> declarations, AstNode context)
        {
            this.WriteOpenParentheses();
            bool needComma = false;

            foreach (var p in declarations)
            {
                var name = this.Emitter.GetParameterName(p);

                if (needComma)
                {
                    this.WriteComma();
                }

                needComma = true;
                this.Write(name);
                this.WriteColon();
                name = BridgeTypes.ToTypeScriptName(p.Type, this.Emitter);
                this.Write(name);

                var resolveResult = this.Emitter.Resolver.ResolveNode(p.Type, this.Emitter);
                if (resolveResult != null && (resolveResult.Type.IsReferenceType.HasValue && resolveResult.Type.IsReferenceType.Value || resolveResult.Type.IsKnownType(KnownTypeCode.NullableOfT)))
                {
                    this.Write(" | null");
                }
            }

            this.WriteCloseParentheses();
        }
Esempio n. 4
0
        protected virtual void EmitFields(TypeConfigInfo info)
        {
            if (info.Fields.Count > 0)
            {
                foreach (var field in info.Fields)
                {
                    if (field.Entity.HasModifier(Modifiers.Public) || this.TypeInfo.IsEnum)
                    {
                        XmlToJsDoc.EmitComment(this, field.Entity);
                        this.Write(field.GetName(this.Emitter));
                        this.WriteColon();
                        string typeName = this.TypeInfo.IsEnum ? "number" : BridgeTypes.ToTypeScriptName(field.Entity.ReturnType, this.Emitter);
                        this.Write(typeName);
                        this.WriteSemiColon();
                        this.WriteNewLine();
                    }
                }
            }

            if (info.Events.Count > 0)
            {
                foreach (var ev in info.Events)
                {
                    if (ev.Entity.HasModifier(Modifiers.Public))
                    {
                        var name = ev.GetName(this.Emitter);
                        if (name.StartsWith("$"))
                        {
                            name = name.Substring(1);
                        }

                        this.WriteEvent(ev, "add" + name);
                        this.WriteEvent(ev, "remove" + name);
                    }
                }
            }

            if (info.Properties.Count > 0)
            {
                foreach (var prop in info.Properties)
                {
                    if (prop.Entity.HasModifier(Modifiers.Public))
                    {
                        var name = prop.GetName(this.Emitter);
                        if (name.StartsWith("$"))
                        {
                            name = name.Substring(1);
                        }

                        this.WriteProp(prop, name, true);
                        this.WriteProp(prop, name, false);
                    }
                }
            }

            new MethodsBlock(this.Emitter, this.TypeInfo, this.StaticBlock).Emit();
        }
Esempio n. 5
0
        protected virtual void EmitStructMethods()
        {
            var    typeDef    = this.Emitter.GetTypeDefinition();
            string structName = BridgeTypes.ToTypeScriptName(this.TypeInfo.Type, this.Emitter);

            if (this.TypeInfo.InstanceConfig.Fields.Count == 0)
            {
                this.Write(JS.Funcs.CLONE + "(to");
                this.WriteColon();
                this.Write(structName);
                this.WriteCloseParentheses();
                this.WriteColon();
                this.Write(structName);
                this.WriteSemiColon();
                this.WriteNewLine();
                return;
            }

            if (!this.TypeInfo.InstanceMethods.ContainsKey(CS.Methods.GETHASHCODE))
            {
                this.Write(JS.Funcs.GETHASHCODE + "()");
                this.WriteColon();
                this.Write("number");
                this.WriteSemiColon();
                this.WriteNewLine();
            }

            if (!this.TypeInfo.InstanceMethods.ContainsKey(CS.Methods.EQUALS))
            {
                this.Write(JS.Funcs.EQUALS + "(o");
                this.WriteColon();
                this.Write(structName);
                this.WriteCloseParentheses();
                this.WriteColon();
                this.Write("boolean");
                this.WriteSemiColon();
                this.WriteNewLine();
            }

            this.Write(JS.Funcs.CLONE + "(to");
            this.WriteColon();
            this.Write(structName);
            this.WriteCloseParentheses();
            this.WriteColon();
            this.Write(structName);
            this.WriteSemiColon();
            this.WriteNewLine();
        }
Esempio n. 6
0
        protected virtual void EmitPropertyMethod(CustomEventDeclaration customEventDeclaration, Accessor accessor, bool remover)
        {
            if (!accessor.IsNull && this.Emitter.GetInline(accessor) == null)
            {
                XmlToJsDoc.EmitComment(this, customEventDeclaration);
                this.Write(Helpers.GetEventRef(customEventDeclaration, this.Emitter, remover, false, false));
                this.WriteOpenParentheses();
                this.Write("value");
                this.WriteColon();
                var retType = BridgeTypes.ToTypeScriptName(customEventDeclaration.ReturnType, this.Emitter);
                this.Write(retType);
                this.WriteCloseParentheses();
                this.WriteColon();
                this.Write("void");

                this.WriteSemiColon();
                this.WriteNewLine();
            }
        }
Esempio n. 7
0
        private void WriteFieldDeclaration(TypeConfigItem field, VariableInitializer variableInitializer)
        {
            XmlToJsDoc.EmitComment(this, field.Entity, null, variableInitializer);

            if (this.TypeInfo.IsEnum)
            {
                this.Write(EnumBlock.GetEnumItemName(this.Emitter, field));
            }
            else
            {
                this.Write(field.GetName(this.Emitter));
            }

            if (field.VarInitializer != null)
            {
                var field_rr = this.Emitter.Resolver.ResolveNode(field.VarInitializer, this.Emitter);
                if (field_rr is MemberResolveResult mrr && mrr.Member.Attributes.Any(a => a.AttributeType.FullName == "Bridge.OptionalAttribute"))
                {
                    this.Write("?");
                }
            }

            this.WriteColon();

            string typeName = this.TypeInfo.IsEnum
                ? (Helpers.IsStringNameEnum(this.TypeInfo.Type) ? "string" : "number")
                : BridgeTypes.ToTypeScriptName(field.Entity.ReturnType, this.Emitter);

            this.Write(typeName);

            if (!this.TypeInfo.IsEnum)
            {
                var resolveResult = this.Emitter.Resolver.ResolveNode(field.Entity.ReturnType, this.Emitter);
                if (resolveResult != null && (resolveResult.Type.IsReferenceType.HasValue && resolveResult.Type.IsReferenceType.Value || resolveResult.Type.IsKnownType(KnownTypeCode.NullableOfT)))
                {
                    this.Write(" | null");
                }
            }

            this.WriteSemiColon();
            this.WriteNewLine();
        }
Esempio n. 8
0
        protected virtual void EmitPropertyMethod(CustomEventDeclaration customEventDeclaration, Accessor accessor, bool remover)
        {
            if (!accessor.IsNull && this.Emitter.GetInline(accessor) == null)
            {
                XmlToJsDoc.EmitComment(this, customEventDeclaration);
                var memberResult    = this.Emitter.Resolver.ResolveNode(customEventDeclaration, this.Emitter) as MemberResolveResult;
                var ignoreInterface = memberResult.Member.DeclaringType.Kind == TypeKind.Interface &&
                                      memberResult.Member.DeclaringType.TypeParameterCount > 0;
                this.Write(Helpers.GetEventRef(customEventDeclaration, this.Emitter, remover, false, ignoreInterface));
                this.WriteOpenParentheses();
                this.Write("value");
                this.WriteColon();
                var retType = BridgeTypes.ToTypeScriptName(customEventDeclaration.ReturnType, this.Emitter);
                this.Write(retType);
                this.WriteCloseParentheses();
                this.WriteColon();
                this.Write("void");

                this.WriteSemiColon();
                this.WriteNewLine();
            }
        }
Esempio n. 9
0
        protected virtual void EmitMethodParameters(IEnumerable <ParameterDeclaration> declarations, AstNode context)
        {
            bool needComma = false;

            foreach (var p in declarations)
            {
                var  name     = this.Emitter.GetParameterName(p);
                bool optional = p.DefaultExpression != null && !p.DefaultExpression.IsNull;

                if (needComma)
                {
                    this.WriteComma();
                }

                needComma = true;
                this.Write(name);

                if (optional)
                {
                    this.Write("?");
                }

                this.WriteColon();
                name = BridgeTypes.ToTypeScriptName(p.Type, this.Emitter);

                var resolveResult = this.Emitter.Resolver.ResolveNode(p.Type, this.Emitter);
                if (resolveResult != null && (resolveResult.Type.IsReferenceType.HasValue && resolveResult.Type.IsReferenceType.Value || resolveResult.Type.IsKnownType(KnownTypeCode.NullableOfT)))
                {
                    name += " | null";
                }

                if (p.ParameterModifier == ParameterModifier.Out || p.ParameterModifier == ParameterModifier.Ref)
                {
                    name = "{v: " + name + "}";
                }
                this.Write(name);
            }
        }
Esempio n. 10
0
        protected virtual void WriteNestedDefs()
        {
            if (this.NestedTypes != null)
            {
                foreach (var nestedType in this.NestedTypes)
                {
                    var typeDef   = this.Emitter.GetTypeDefinition(nestedType.Type);
                    var isGeneric = typeDef.GenericParameters.Count > 0;

                    if (!isGeneric)
                    {
                        string name = this.Emitter.Validator.GetCustomTypeName(typeDef);

                        if (name.IsEmpty())
                        {
                            name = BridgeTypes.ToTypeScriptName(nestedType.Type, this.Emitter, false, true);
                        }

                        this.Write(name);
                        this.WriteColon();


                        var    parentTypeDef = this.Emitter.GetTypeDefinition();
                        string parentName    = this.Emitter.Validator.GetCustomTypeName(parentTypeDef);
                        if (parentName.IsEmpty())
                        {
                            parentName = this.TypeInfo.Type.Name;
                        }

                        this.Write(parentName);
                        this.WriteDot();
                        this.Write(name + "Func");
                        this.WriteSemiColon();
                        this.WriteNewLine();
                    }
                }
            }
        }