From cd1cb8fcb89ad2027814dd1817bc143356ff5425 Mon Sep 17 00:00:00 2001 From: krauthaufen Date: Fri, 31 May 2019 21:31:59 +0200 Subject: [PATCH] [FSharpToFable] hacky fix for unknown attribute constructors (solving https://github.com/fable-compiler/Fable/pull/1839#issuecomment-496550295) --- src/Fable.Transforms/FSharp2Fable.fs | 36 ++++++++++++++++++++----- src/Fable.Transforms/Fable2Babel.fs | 2 ++ src/Fable.Transforms/Global/Compiler.fs | 1 + src/Fable.Transforms/State.fs | 7 +++++ tests/Main/ReflectionTests.fs | 1 + 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/Fable.Transforms/FSharp2Fable.fs b/src/Fable.Transforms/FSharp2Fable.fs index 55452e89bb..d1bece643c 100644 --- a/src/Fable.Transforms/FSharp2Fable.fs +++ b/src/Fable.Transforms/FSharp2Fable.fs @@ -281,11 +281,12 @@ let rec private transformDecisionTargets (com: IFableCompiler) (ctx: Context) ac let private skipAttribute (name : string) = // TODO: skip all attributes where definiton not known??? - name.StartsWith "Microsoft.FSharp.Core" || - name.StartsWith "System.Reflection" || - name.StartsWith "System.Runtime.CompilerServices" || - name.StartsWith "System.ObsoleteAttribute" || - name.StartsWith "System.Diagnostics" + // name.StartsWith "Microsoft.FSharp.Core" || + // name.StartsWith "System.Reflection" || + // name.StartsWith "System.Runtime.CompilerServices" || + // name.StartsWith "System.ObsoleteAttribute" || + // name.StartsWith "System.Diagnostics" + false let private transformAttribute (com: IFableCompiler) (ctx : Context) (a : FSharpAttribute) = match a.AttributeType.TryFullName with @@ -310,11 +311,29 @@ let private transformAttribute (com: IFableCompiler) (ctx : Context) (a : FSharp a.ConstructorArguments |> Seq.toList |> List.map (fun (t,v) -> match v with | :? string as str -> Fable.Value(Fable.StringConstant str, None) + | :? int8 as v -> Fable.Value(Fable.NumberConstant(float v, NumberKind.Int8), None) + | :? uint8 as v -> Fable.Value(Fable.NumberConstant(float v, NumberKind.UInt8), None) + | :? int16 as v -> Fable.Value(Fable.NumberConstant(float v, NumberKind.Int16), None) + | :? uint16 as v -> Fable.Value(Fable.NumberConstant(float v, NumberKind.UInt16), None) + | :? int32 as v -> Fable.Value(Fable.NumberConstant(float v, NumberKind.Int32), None) + | :? uint32 as v -> Fable.Value(Fable.NumberConstant(float v, NumberKind.UInt32), None) + | :? float32 as v -> Fable.Value(Fable.NumberConstant(float v, NumberKind.Float32), None) + | :? float as v -> Fable.Value(Fable.NumberConstant(v, NumberKind.Float64), None) | _ -> Fable.Value(Fable.StringConstant (string v), None) ) + + let typ = makeTypeFromDef com ctx.GenericArgs (System.Collections.Generic.List() :> IList<_>) a.AttributeType - let x = makeCallFrom com ctx None typ false [] None args ctor - Some (fullname,x) + try + let x = makeCallFrom com ctx None typ false [] None args ctor + match x with + | AST.Fable.Value(AST.Fable.Null AST.Fable.Any, None) -> + com.RemoveLastError() + None + | _ -> + Some (fullname,x) + with _ -> + None | _ -> None | _ -> @@ -1545,6 +1564,9 @@ type FableCompiler(com: ICompiler, implFiles: IDictionaryInlineExpr) -> InlineExpr abstract AddLog: msg:string * severity: Severity * ?range:SourceLocation * ?fileName:string * ?tag: string -> unit + abstract RemoveLastError : unit -> unit diff --git a/src/Fable.Transforms/State.fs b/src/Fable.Transforms/State.fs index fded469a62..8fa30ee408 100644 --- a/src/Fable.Transforms/State.fs +++ b/src/Fable.Transforms/State.fs @@ -94,6 +94,13 @@ type Compiler(currentFile, project: Project, options, fableLibraryDir: string) = Range = range FileName = fileName } |> logs.Add + + member __.RemoveLastError() = + let mutable i = logs.Count - 1 + while i >= 0 && logs.[i].Severity <> Severity.Error do + i <- i - 1 + if i >= 0 then logs.RemoveAt i + // TODO: If name includes `$$2` at the end, remove it member __.GetUniqueVar(name) = id <- id + 1 diff --git a/tests/Main/ReflectionTests.fs b/tests/Main/ReflectionTests.fs index 9a9454462e..dc1913b054 100644 --- a/tests/Main/ReflectionTests.fs +++ b/tests/Main/ReflectionTests.fs @@ -23,6 +23,7 @@ type TestType = type TestType2 = | Union2 of string +[] type TestType3 = class end type TestType4 = class end type TestType5 = class end