@@ -50,8 +50,8 @@ module Lib =
5050 let libCall ( com : IBabelCompiler ) ctx r moduleName memberName args =
5151 CallExpression( com.TransformImport( ctx, memberName, getLibPath com moduleName), args, ?loc= r) :> Expression
5252
53- let libConsCall ( com : IBabelCompiler ) ctx moduleName memberName args =
54- NewExpression( com.TransformImport( ctx, memberName, getLibPath com moduleName), args) :> Expression
53+ let libConsCall ( com : IBabelCompiler ) ctx r moduleName memberName args =
54+ NewExpression( com.TransformImport( ctx, memberName, getLibPath com moduleName), args, ?loc = r ) :> Expression
5555
5656 let libValue ( com : IBabelCompiler ) ctx moduleName memberName =
5757 com.TransformImport( ctx, memberName, getLibPath com moduleName)
@@ -626,6 +626,9 @@ module Util =
626626 let ofInt i =
627627 NumericLiteral( float i) :> Expression
628628
629+ let ofString s =
630+ StringLiteral( s) :> Expression
631+
629632 let memberFromName ( memberName : string ): Expression * bool =
630633 if memberName.StartsWith( " Symbol." ) then
631634 upcast MemberExpression( Identifier " Symbol" , Identifier memberName.[ 7 ..], false ), true
@@ -655,11 +658,11 @@ module Util =
655658 | [] -> expr
656659 | m:: ms -> get None expr m |> getParts ms
657660
658- let makeList com ctx headAndTail =
661+ let makeList com ctx r headAndTail =
659662 match headAndTail with
660663 | None -> [||]
661664 | Some( TransformExpr com ctx head, TransformExpr com ctx tail) -> [| head; tail|]
662- |> libConsCall com ctx " Types" " List"
665+ |> libConsCall com ctx r " Types" " List"
663666
664667 let makeArray ( com : IBabelCompiler ) ctx exprs =
665668 List.mapToArray ( fun e -> com.TransformAsExpr( ctx, e)) exprs
@@ -676,7 +679,7 @@ module Util =
676679 | _ ->
677680 makeArray com ctx args
678681
679- let makeTypedAllochedArray ( com : IBabelCompiler ) ctx typ ( TransformExpr com ctx size ) =
682+ let makeTypedAllocatedArray ( com : IBabelCompiler ) ctx typ ( TransformExpr com ctx size ) =
680683 match typ with
681684 | Fable.Number kind when com.Options.TypedArrays ->
682685 let jsName = getTypedArrayName com kind
@@ -889,16 +892,16 @@ module Util =
889892 | Fable.NumberConstant ( x,_) -> upcast NumericLiteral( x, ?loc= r)
890893 | Fable.RegexConstant ( source, flags) -> upcast RegExpLiteral( source, flags, ?loc= r)
891894 | Fable.NewArray ( values, typ) -> makeTypedArray com ctx typ values
892- | Fable.NewArrayAlloc ( size, typ) -> makeTypedAllochedArray com ctx typ size
895+ | Fable.NewArrayAlloc ( size, typ) -> makeTypedAllocatedArray com ctx typ size
893896 | Fable.NewTuple vals -> makeArray com ctx vals
894897 // Optimization for bundle size: compile list literals as List.ofArray
895898 | Replacements.ListLiteral( exprs, t) ->
896899 match exprs with
897- | [] -> makeList com ctx None
898- | [ expr] -> Some( expr, Fable.Value( Fable.NewList ( None, t), None)) |> makeList com ctx
900+ | [] -> makeList com ctx r None
901+ | [ expr] -> Some( expr, Fable.Value( Fable.NewList ( None, t), None)) |> makeList com ctx r
899902 | exprs -> [| makeArray com ctx exprs|] |> libCall com ctx r " List" " ofArray"
900903 | Fable.NewList ( headAndTail, _) ->
901- makeList com ctx headAndTail
904+ makeList com ctx r headAndTail
902905 | Fable.NewOption ( value, t) ->
903906 match value with
904907 | Some ( TransformExpr com ctx e) ->
@@ -929,14 +932,20 @@ module Util =
929932 if com.Options.Typescript
930933 then makeGenTypeParamInst com ctx genArgs
931934 else None
935+ // let caseName = ent.UnionCases |> List.item tag |> getUnionCaseName |> ofString
932936 let values = ( ofInt tag):: values |> List.toArray
933937 upcast NewExpression( consRef, values, ?typeArguments= typeParamInst, ?loc= r)
934938
939+ let callToString com ctx =
940+ let toString = CallExpression( get None ( Identifier " this" ) " ToString" , [||]) :> Expression
941+ BlockStatement [| ReturnStatement( toString)|]
942+
935943 let enumerator2iterator com ctx =
936944 let enumerator = CallExpression( get None ( Identifier " this" ) " GetEnumerator" , [||]) :> Expression
937945 BlockStatement [| ReturnStatement( libCall com ctx None " Seq" " toIterator" [| enumerator|])|]
938946
939947 let transformObjectExpr ( com : IBabelCompiler ) ctx ( members : Fable.MemberDecl list ) baseCall : Expression =
948+ // TODO: refactor to simplify (DRY)
940949 let makeObjMethod kind prop computed hasSpread args body =
941950 let args , body , returnType , typeParamDecl =
942951 getMemberArgsAndBody com ctx Attached hasSpread args body
@@ -959,6 +968,13 @@ module Util =
959968 let body = enumerator2iterator com ctx
960969 ObjectMethod( ObjectMeth, prop, [||], body, computed_= computed) :> ObjectMember
961970 [ method; iterator]
971+ elif memb.Name = " ToString" then
972+ let method = makeObjMethod ObjectMeth prop computed info.HasSpread memb.Args memb.Body
973+ let method2 =
974+ let prop , computed = memberFromName " toString"
975+ let body = callToString com ctx
976+ ObjectMethod( ObjectMeth, prop, [||], body, computed_= computed) :> ObjectMember
977+ [ method; method2]
962978 else
963979 [ makeObjMethod ObjectMeth prop computed info.HasSpread memb.Args memb.Body]
964980 ) |> List.toArray |> ObjectExpression
@@ -1766,13 +1782,12 @@ module Util =
17661782 let declareClassType ( com : IBabelCompiler ) ctx ( ent : Fable.Entity ) entName ( consArgs : Pattern []) ( consBody : BlockStatement ) ( baseExpr : Expression option ) classMembers =
17671783 let consId = Identifier " constructor"
17681784 let typeParamDecl = makeEntityTypeParamDecl com ctx ent
1769- let baseRef =
1770- match baseExpr with
1771- | Some baseRef -> baseRef
1772- | _ -> makeImportTypeId com ctx " Types" " SystemObject" :> Expression
1785+ // let baseRef =
1786+ // match baseExpr with
1787+ // | Some baseRef -> baseRef
1788+ // | _ -> makeImportTypeId com ctx "Types" "SystemObject" :> Expression
17731789 let consBody =
1774- if ( Option.isNone baseExpr) || ( not ent.IsFSharpUnion) && ( ent.IsFSharpRecord || ent.IsValueType || ent.IsFSharpExceptionDeclaration)
1775- then
1790+ if ent.IsFSharpExceptionDeclaration then
17761791 let super = callSuperConstructor None [] |> ExpressionStatement :> Statement
17771792 BlockStatement ( Array.append [| super|] consBody.Body)
17781793 else consBody
@@ -1784,10 +1799,9 @@ module Util =
17841799 let ta = prop.Value |> TypeAnnotation |> Some
17851800 ClassProperty( prop.Key, ?`` static `` = prop.Static, ?typeAnnotation= ta) :> ClassMember)
17861801 else Array.empty
1787- // no need for constructor in unions
1788- let classMembers = if ent.IsFSharpUnion then classMembers else Array.append [| classCons |] classMembers
1802+ let classMembers = Array.append [| classCons |] classMembers
17891803 let classBody = ClassBody([| yield ! classFields; yield ! classMembers |])
1790- let classExpr = ClassExpression( classBody, ?superClass= Some baseRef , ?typeParameters= typeParamDecl)
1804+ let classExpr = ClassExpression( classBody, ?superClass= baseExpr , ?typeParameters= typeParamDecl)
17911805 classExpr |> declareModuleMember ent.IsPublic entName false
17921806
17931807 let declareType ( com : IBabelCompiler ) ctx ( ent : Fable.Entity ) entName ( consArgs : Pattern []) ( consBody : BlockStatement ) baseExpr classMembers : ModuleDeclaration list =
@@ -1841,43 +1855,37 @@ module Util =
18411855 |> Array.singleton
18421856
18431857 let transformAttachedMethod ( com : IBabelCompiler ) ctx ( memb : Fable.MemberDecl ) =
1858+ let makeMethod name args body =
1859+ let key , computed = memberFromName name
1860+ ClassMethod( ClassFunction, key, args, body, computed_= computed) :> ClassMember
18441861 let args , body , returnType , typeParamDecl =
18451862 getMemberArgsAndBody com ctx Attached memb.Info.HasSpread memb.Args memb.Body
1846- let key , computed = memberFromName memb.Name
1847- let method =
1848- ClassMethod( ClassFunction, key, args, body, computed_= computed)
1849- :> ClassMember
1850- if memb.Info.IsEnumerator then
1851- let iterator =
1852- let key , computed = memberFromName " Symbol.iterator"
1853- ClassMethod( ClassFunction, key, [||], enumerator2iterator com ctx, computed_= computed)
1854- :> ClassMember
1855- [| method; iterator|]
1856- else
1857- [| method|]
1863+ [|
1864+ yield makeMethod memb.Name args body
1865+ if memb.Name = " ToString" then
1866+ yield makeMethod " toString" args ( callToString com ctx)
1867+ if memb.Info.IsEnumerator then
1868+ yield makeMethod " Symbol.iterator" [||] ( enumerator2iterator com ctx)
1869+ |]
18581870
18591871 let transformUnion ( com : IBabelCompiler ) ctx ( ent : Fable.Entity ) ( entName : string ) classMembers =
1860- let baseRef = libValue com ctx " Types " " Union "
1872+
18611873 let tagId = makeTypedIdent ( Fable.Number Int32) " tag"
18621874 let fieldsId = makeTypedIdent ( Fable.Array Fable.Any) " fields"
18631875 let args =
18641876 [| typedIdent com ctx tagId :> Pattern
18651877 typedIdent com ctx fieldsId |> restElement |]
18661878 let body =
1867- [ ( ident tagId) :> Expression
1868- SpreadElement( ident fieldsId) :> Expression ]
1869- |> callSuperConstructor None
1870- |> ExpressionStatement :> Statement |> Array.singleton |> BlockStatement
1871- // [| tagId; fieldsId |]
1872- // |> Array.map (fun id ->
1873- // let left = get None thisExpr id.Name
1874- // let right =
1875- // match id.Type with
1876- // | Fable.Number _ ->
1877- // BinaryExpression(BinaryOrBitwise, ident id, NumericLiteral(0.)) :> Expression
1878- // | _ -> ident id :> Expression
1879- // assign None left right |> ExpressionStatement :> Statement)
1880- // |> BlockStatement
1879+ [| tagId; fieldsId |]
1880+ |> Array.map ( fun id ->
1881+ let left = get None thisExpr id.Name
1882+ let right =
1883+ match id.Type with
1884+ | Fable.Number _ ->
1885+ BinaryExpression( BinaryOrBitwise, ident id, NumericLiteral( 0. )) :> Expression
1886+ | _ -> ident id :> Expression
1887+ assign None left right |> ExpressionStatement :> Statement)
1888+ |> BlockStatement
18811889
18821890 let cases =
18831891 let body =
@@ -1891,7 +1899,7 @@ module Util =
18911899 ClassMethod( ClassFunction, Identifier " cases" , [||], body) :> ClassMember
18921900
18931901 Array.append [| cases|] classMembers
1894- |> declareType com ctx ent entName args body ( Some baseRef )
1902+ |> declareType com ctx ent entName args body None
18951903
18961904 let transformClassWithCompilerGeneratedConstructor ( com : IBabelCompiler ) ctx ( ent : Fable.Entity ) ( entName : string ) classMembers =
18971905 let fieldIds = getEntityFieldsAsIdents com ent
@@ -1906,8 +1914,8 @@ module Util =
19061914 let baseExpr =
19071915 if ent.IsFSharpExceptionDeclaration
19081916 then libValue com ctx " Types" " FSharpException" |> Some
1909- elif ent.IsFSharpRecord || ent.IsValueType
1910- then libValue com ctx " Types" " Record" |> Some
1917+ // elif ent.IsFSharpRecord || ent.IsValueType
1918+ // then libValue com ctx "Types" "Record" |> Some
19111919 else None
19121920 let typedPattern x = typedIdent com ctx x :> Pattern
19131921 let args = fieldIds |> Array.map typedPattern
@@ -1958,9 +1966,6 @@ module Util =
19581966 |> Array.append [| ExpressionStatement baseCall :> Statement|]
19591967 |> BlockStatement
19601968 | _ -> None, consBody // Unexpected
1961- // Structs have same properties as records
1962- | None when classDecl.Entity.IsValueType ->
1963- Some( libValue com ctx " Types" " Record" ), consBody
19641969 | None -> None, consBody
19651970
19661971 [
0 commit comments