Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
m-bock committed Nov 11, 2023
1 parent 372f187 commit aec0b44
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/TsBridge/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Prelude
import Control.Promise (Promise)
import DTS as DTS
import Data.Either (Either)
import Data.Function.Uncurried (Fn2, Fn3)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable)
import Data.Symbol (class IsSymbol)
Expand Down Expand Up @@ -78,6 +79,12 @@ instance TsBridge a => TsBridge (Nullable a) where
instance (TsBridge a, TsBridge b) => TsBridge (a -> b) where
tsBridge = TSB.tsBridgeFunction Tok

instance (TsBridge a, TsBridge b, TsBridge c) => TsBridge (Fn2 a b c) where
tsBridge = TSB.tsBridgeFn2 Tok

instance (TsBridge a, TsBridge b, TsBridge c, TsBridge d) => TsBridge (Fn3 a b c d) where
tsBridge = TSB.tsBridgeFn3 Tok

instance TsBridge a => TsBridge (Maybe a) where
tsBridge = TSB.tsBridgeMaybe Tok

Expand Down
42 changes: 41 additions & 1 deletion src/TsBridge/DefaultImpls.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module TsBridge.DefaultImpls
, tsBridgeEffect
, tsBridgeEither
, tsBridgeFn2
, tsBridgeFn3
, tsBridgeFunction
, tsBridgeInt
, tsBridgeMaybe
Expand Down Expand Up @@ -50,7 +51,7 @@ import DTS as DTS
import Data.Array (mapWithIndex, (:))
import Data.Array as A
import Data.Either (Either)
import Data.Function.Uncurried (Fn2)
import Data.Function.Uncurried (Fn2, Fn3)
import Data.Maybe (Maybe(..))
import Data.Newtype (class Newtype, over)
import Data.Nullable (Nullable)
Expand Down Expand Up @@ -436,6 +437,45 @@ tsBridgeFn2 tok _ = censor mapAccum ado
where
mapAccum = over TsBridgeAccum (\x -> x { scope = fixScope x.scope })

tsBridgeFn3
:: forall tok a1 a2 a3 b
. TsBridgeBy tok a1
=> TsBridgeBy tok a2
=> TsBridgeBy tok a3
=> TsBridgeBy tok b
=> tok
-> Proxy (Fn3 a1 a2 a3 b)
-> TsBridgeM DTS.TsType
tsBridgeFn3 tok _ = censor mapAccum ado
arg1 /\ TsBridgeAccum { scope: Scope scopeArg1 } <- listen $ tsBridgeBy tok (Proxy :: _ a1)
arg2 /\ TsBridgeAccum { scope: Scope scopeArg2 } <- listen $ tsBridgeBy tok (Proxy :: _ a2)
arg3 /\ TsBridgeAccum { scope: Scope scopeArg3 } <- listen $ tsBridgeBy tok (Proxy :: _ a3)
ret /\ TsBridgeAccum { scope: Scope scopeRet } <- listen $ tsBridgeBy tok (Proxy :: _ b)
let
newFixed =
( scopeRet.fixed
# OSet.intersect scopeArg1.fixed
# OSet.intersect scopeArg2.fixed
# OSet.intersect scopeArg3.fixed
)
<> scopeArg1.floating
<> scopeArg2.floating
<> scopeArg3.floating
<> scopeRet.floating

removeQuant =
DTS.mapQuantifier $ coerce $ OSet.filter (_ `OSet.notElem` newFixed)

in
DTS.TsTypeFunction (DTS.TsTypeArgsQuant $ coerce newFixed)
[ DTS.TsFnArg (DTS.TsName "arg1") (removeQuant arg1)
, DTS.TsFnArg (DTS.TsName "arg2") (removeQuant arg2)
, DTS.TsFnArg (DTS.TsName "arg3") (removeQuant arg3)
]
(removeQuant ret)
where
mapAccum = over TsBridgeAccum (\x -> x { scope = fixScope x.scope })

-- | `tsBridge` type class method implementation for opaque types
tsBridgeOpaqueType :: forall a. { moduleName :: String, typeName :: String, typeArgs :: Array (String /\ TsBridgeM DTS.TsType) } -> Proxy a -> TsBridgeM DTS.TsType
tsBridgeOpaqueType { moduleName, typeName, typeArgs } _ =
Expand Down
15 changes: 15 additions & 0 deletions test/TsBridgeSpec.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import DTS as DTS
import DTS.Print (printTsDeclarations, printTsType)
import Data.Either (Either(..), fromRight)
import Data.Foldable (fold)
import Data.Function.Uncurried (Fn2, Fn3)
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe(..))
Expand Down Expand Up @@ -63,6 +64,12 @@ instance (TsBridge a, TsBridge b) => TsBridge (OneOf a b) where
instance (TsBridge a, TsBridge b) => TsBridge (a -> b) where
tsBridge = TSB.tsBridgeFunction Tok

instance (TsBridge a, TsBridge b, TsBridge c) => TsBridge (Fn2 a b c) where
tsBridge = TSB.tsBridgeFn2 Tok

instance (TsBridge a, TsBridge b, TsBridge c, TsBridge d) => TsBridge (Fn3 a b c d) where
tsBridge = TSB.tsBridgeFn3 Tok

instance (TSB.TsBridgeRecord Tok r) => TsBridge (Record r) where
tsBridge = TSB.tsBridgeRecord Tok

Expand Down Expand Up @@ -235,6 +242,14 @@ spec = do
testTypePrint (tsBridge (Proxy :: _ (String -> Number -> Boolean)))
"(_: string) => (_: number) => boolean"

describe "Fn2" do
testTypePrint (tsBridge (Proxy :: _ (Fn2 String Number Boolean)))
"(arg1: string, arg2: number) => boolean"

describe "Fn3" do
testTypePrint (tsBridge (Proxy :: _ (Fn3 String Number Number Boolean)))
"(arg1: string, arg2: number, arg3: number) => boolean"

describe "Function" do
testTypePrint (tsBridge (Proxy :: _ (Array A -> Array B -> Array (Tuple A B))))
"<A>(_: Array<A>) => <B>(_: Array<B>) => Array<import('../Data.Tuple').Tuple<A, B>>"
Expand Down

0 comments on commit aec0b44

Please sign in to comment.