Exemple #1
0
 public virtual void CheckEvents(TypeDefinition type, ITranslator translator)
 {
     if (type.HasEvents && this.IsObjectLiteral(type))
     {
         TranslatorException.Throw("ObjectLiteral type doesn't support events: {0}", type);
     }
 }
Exemple #2
0
        public string GenerateSourceMap(string fileName, string content, Action <SourceMapBuilder> before = null)
        {
            if (this.AssemblyInfo.SourceMap.Enabled)
            {
                var projectPath = Path.GetDirectoryName(this.Location);

                SourceMapGenerator.Generate(fileName, projectPath, ref content,
                                            before,
                                            (sourceRelativePath) =>
                {
                    string path = null;
                    ParsedSourceFile sourceFile = null;

                    try
                    {
                        path       = Path.Combine(projectPath, sourceRelativePath);
                        sourceFile = this.ParsedSourceFiles.First(pf => pf.ParsedFile.FileName == path);

                        return(sourceFile.SyntaxTree.TextSource ?? sourceFile.SyntaxTree.ToString(Translator.GetFormatter()));
                    }
                    catch (Exception ex)
                    {
                        throw (TranslatorException)TranslatorException.Create(
                            "Could not get ParsedSourceFile for SourceMap. Exception: {0}; projectPath: {1}; sourceRelativePath: {2}; path: {3}.",
                            ex.ToString(), projectPath, sourceRelativePath, path);
                    }
                },
                                            new string[0], this.SourceFiles, this.AssemblyInfo.SourceMap.Eol, this.Log
                                            );
            }

            return(content);
        }
Exemple #3
0
        public virtual void CheckObjectLiteral(TypeDefinition type, ITranslator translator)
        {
            if (this.IsObjectLiteral(type))
            {
                if (type.IsInterface && (type.HasMethods || type.HasProperties || type.HasEvents || type.HasFields))
                {
                    TranslatorException.Throw("ObjectLiteral interface doesn't support any contract members: {0}", type);
                }

                var objectCreateMode = this.GetObjectCreateMode(type);

                if (type.BaseType != null)
                {
                    TypeDefinition baseType = null;
                    try
                    {
                        baseType = type.BaseType.Resolve();
                    }
                    catch (Exception)
                    {
                    }

                    if (objectCreateMode == 1 && baseType != null && baseType.FullName != "System.Object" && this.GetObjectCreateMode(baseType) == 0)
                    {
                        TranslatorException.Throw("[ObjectLiteral] with Constructor mode should be inherited from class with same options: {0}", type);
                    }

                    if (objectCreateMode == 0 && baseType != null && this.GetObjectCreateMode(baseType) == 1)
                    {
                        TranslatorException.Throw("[ObjectLiteral] with Plain mode cannot be inherited from [ObjectLiteral] with Constructor mode: {0}", type);
                    }
                }

                if (type.Interfaces.Count > 0 && objectCreateMode == 1)
                {
                    foreach (var @interface in type.Interfaces)
                    {
                        TypeDefinition iDef = null;
                        try
                        {
                            iDef = @interface.Resolve();
                        }
                        catch (Exception)
                        {
                        }

                        if (iDef != null && iDef.FullName != "System.Object" && !this.IsObjectLiteral(iDef))
                        {
                            TranslatorException.Throw("[ObjectLiteral] should implement an interface which must be object literal also: {0}", type);
                        }
                    }
                }
            }
        }
Exemple #4
0
 public virtual void CheckProperties(TypeDefinition type, ITranslator translator)
 {
     if (type.HasProperties && this.IsObjectLiteral(type))
     {
         foreach (PropertyDefinition prop in type.Properties)
         {
             if ((prop.GetMethod != null && prop.GetMethod.IsVirtual) || (prop.SetMethod != null && prop.SetMethod.IsVirtual))
             {
                 TranslatorException.Throw("ObjectLiteral type doesn't support virtual members: {0}", type);
             }
         }
     }
 }
Exemple #5
0
 public virtual void CheckFields(TypeDefinition type, ITranslator translator)
 {
     if (this.IsObjectLiteral(type) && type.HasFields)
     {
         foreach (FieldDefinition field in type.Fields)
         {
             if (field.IsStatic)
             {
                 TranslatorException.Throw("ObjectLiteral type doesn't support static members: {0}", type);
             }
         }
     }
 }
Exemple #6
0
        public virtual void CheckObjectLiteral(TypeDefinition type, ITranslator translator)
        {
            if (!this.IsObjectLiteral(type))
            {
                return;
            }

            var objectCreateMode = this.GetObjectCreateMode(type);

            if (objectCreateMode == 0)
            {
                var ctors = type.GetConstructors();

                foreach (var ctor in ctors)
                {
                    foreach (var parameter in ctor.Parameters)
                    {
                        if (parameter.ParameterType.FullName == "Bridge.ObjectCreateMode")
                        {
                            TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_PLAIN_NO_CREATE_MODE_CUSTOM_CONSTRUCTOR, type);
                        }

                        if (parameter.ParameterType.FullName == "Bridge.ObjectInitializationMode")
                        {
                            continue;
                        }

                        TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_PLAIN_CUSTOM_CONSTRUCTOR, type);
                    }
                }
            }

            if (type.IsInterface)
            {
                if (type.HasMethods && type.Methods.GroupBy(m => m.Name).Any(g => g.Count() > 1))
                {
                    TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_INTERFACE_NO_OVERLOAD_METHODS, type);
                }

                if (type.HasEvents)
                {
                    TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_INTERFACE_NO_EVENTS, type);
                }
            }
            else
            {
                if (type.Methods.Any(m => !m.IsRuntimeSpecialName && m.Name.Contains(".") && !m.Name.Contains("<")) ||
                    type.Properties.Any(m => !m.IsRuntimeSpecialName && m.Name.Contains(".") && !m.Name.Contains("<")))
                {
                    TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_INTERFACE_NO_EXPLICIT_IMPLEMENTATION, type);
                }
            }

            if (type.BaseType != null)
            {
                TypeDefinition baseType = null;
                try
                {
                    baseType = type.BaseType.Resolve();
                }
                catch (Exception)
                {
                }

                if (objectCreateMode == 1 && baseType != null && baseType.FullName != "System.Object" && baseType.FullName != "System.ValueType" && this.GetObjectCreateMode(baseType) == 0)
                {
                    TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_CONSTRUCTOR_INHERITANCE, type);
                }

                if (objectCreateMode == 0 && baseType != null && this.GetObjectCreateMode(baseType) == 1)
                {
                    TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_PLAIN_INHERITANCE, type);
                }
            }

            if (type.Interfaces.Count > 0)
            {
                foreach (var @interface in type.Interfaces)
                {
                    TypeDefinition iDef = null;
                    try
                    {
                        iDef = @interface.Resolve();
                    }
                    catch (Exception)
                    {
                    }

                    if (iDef != null && iDef.FullName != "System.Object" && !this.IsObjectLiteral(iDef))
                    {
                        TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_INTERFACE_INHERITANCE, type);
                    }
                }
            }

            if (objectCreateMode == 0)
            {
                var hasVirtualMethods = false;

                foreach (MethodDefinition method in type.Methods)
                {
                    if (AttributeHelper.HasCompilerGeneratedAttribute(method))
                    {
                        continue;
                    }

                    if (method.IsVirtual && !(method.IsSetter || method.IsGetter))
                    {
                        hasVirtualMethods = true;
                        break;
                    }
                }

                if (hasVirtualMethods)
                {
                    Bridge.Translator.TranslatorException.Throw(Constants.Messages.Exceptions.OBJECT_LITERAL_NO_VIRTUAL_METHODS, type);
                }
            }
        }
Exemple #7
0
        public virtual void CheckObjectLiteral(TypeDefinition type, ITranslator translator)
        {
            if (this.IsObjectLiteral(type))
            {
                var objectCreateMode = this.GetObjectCreateMode(type);

                if (type.IsInterface)
                {
                    if (type.HasMethods && type.Methods.GroupBy(m => m.Name).Any(g => g.Count() > 1))
                    {
                        TranslatorException.Throw("ObjectLiteral interface doesn't support overloaded methods: {0}",
                                                  type);
                    }

                    if (type.HasEvents)
                    {
                        TranslatorException.Throw("ObjectLiteral interface doesn't support events: {0}", type);
                    }
                }
                else
                {
                    if (type.Methods.Any(m => !m.IsRuntimeSpecialName && m.Name.Contains(".")) ||
                        type.Properties.Any(m => !m.IsRuntimeSpecialName && m.Name.Contains(".")))
                    {
                        TranslatorException.Throw("ObjectLiteral doesn't support explicit interface member implementation: {0}", type);
                    }
                }

                if (type.BaseType != null)
                {
                    TypeDefinition baseType = null;
                    try
                    {
                        baseType = type.BaseType.Resolve();
                    }
                    catch (Exception)
                    {
                    }

                    if (objectCreateMode == 1 && baseType != null && baseType.FullName != "System.Object" && this.GetObjectCreateMode(baseType) == 0)
                    {
                        TranslatorException.Throw("[ObjectLiteral] with Constructor mode should be inherited from class with same options: {0}", type);
                    }

                    if (objectCreateMode == 0 && baseType != null && this.GetObjectCreateMode(baseType) == 1)
                    {
                        TranslatorException.Throw("[ObjectLiteral] with Plain mode cannot be inherited from [ObjectLiteral] with Constructor mode: {0}", type);
                    }
                }

                if (type.Interfaces.Count > 0 && objectCreateMode == 1)
                {
                    foreach (var @interface in type.Interfaces)
                    {
                        TypeDefinition iDef = null;
                        try
                        {
                            iDef = @interface.Resolve();
                        }
                        catch (Exception)
                        {
                        }

                        if (iDef != null && iDef.FullName != "System.Object" && !this.IsObjectLiteral(iDef))
                        {
                            TranslatorException.Throw("[ObjectLiteral] should implement an interface which must be object literal also: {0}", type);
                        }
                    }
                }
            }
        }