Skip to content

Commit

Permalink
[FSharpToFable] hacky fix for unknown attribute constructors (solving #…
Browse files Browse the repository at this point in the history
  • Loading branch information
krauthaufen committed May 31, 2019
1 parent bf25730 commit cd1cb8f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/Fable.Transforms/FSharp2Fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
| _ ->
Expand Down Expand Up @@ -1545,6 +1564,9 @@ type FableCompiler(com: ICompiler, implFiles: IDictionary<string, FSharpImplemen
member __.AddLog(msg, severity, ?range, ?fileName:string, ?tag: string) =
com.AddLog(msg, severity, ?range=range, ?fileName=fileName, ?tag=tag)

member __.RemoveLastError() =
com.RemoveLastError()

let getRootModuleFullName (file: FSharpImplementationFileContents) =
let rootEnt, _ = getRootModuleAndDecls file.Declarations
match rootEnt with
Expand Down
2 changes: 2 additions & 0 deletions src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,8 @@ module Compiler =
member __.GetOrAddInlineExpr(fullName, generate) = com.GetOrAddInlineExpr(fullName, generate)
member __.AddLog(msg, severity, ?range, ?fileName:string, ?tag: string) =
com.AddLog(msg, severity, ?range=range, ?fileName=fileName, ?tag=tag)
member __.RemoveLastError() =
com.RemoveLastError()

let makeCompiler com = new BabelCompiler(com)

Expand Down
1 change: 1 addition & 0 deletions src/Fable.Transforms/Global/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ type ICompiler =
abstract GetOrAddInlineExpr: string * (unit->InlineExpr) -> InlineExpr
abstract AddLog: msg:string * severity: Severity * ?range:SourceLocation
* ?fileName:string * ?tag: string -> unit
abstract RemoveLastError : unit -> unit
7 changes: 7 additions & 0 deletions src/Fable.Transforms/State.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/Main/ReflectionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type TestType =
type TestType2 =
| Union2 of string

[<System.Runtime.CompilerServices.Extension>]
type TestType3 = class end
type TestType4 = class end
type TestType5 = class end
Expand Down

0 comments on commit cd1cb8f

Please sign in to comment.