11module TsBridge.Core
2- ( StandaloneTsType
3- , class TsBridgeBy
2+ ( class TsBridgeBy
43 , class TsValues
54 , class TsValuesRL
65 , tsBridgeBy
@@ -15,8 +14,9 @@ module TsBridge.Core
1514
1615import Prelude
1716
18- import Control.Monad.Error.Class (class MonadError , catchError , throwError )
17+ import Control.Monad.Error.Class (throwError )
1918import Control.Monad.Writer (listens , tell )
19+ import DTS as DTS
2020import Data.Array as A
2121import Data.Array as Array
2222import Data.Either (Either )
@@ -32,16 +32,15 @@ import Prim.Row as Row
3232import Prim.RowList (class RowToList , RowList )
3333import Prim.RowList as RL
3434import Safe.Coerce (coerce )
35- import TsBridge.DTS (TsNameDraft )
36- import TsBridge.DTS as DTS
3735import TsBridge.Monad (Scope (..), TsBridgeAccum (..), TsBridgeM , runTsBridgeM )
36+ import TsBridge.Types (AppError (..), mapErr , mkName , mkPursModuleName , toTsName )
3837import Type.Proxy (Proxy (..))
3938
4039-- | A `StandaloneTsType` represents a TypeScript type with everything it needs
4140-- | to be placed inside complete TS program: If the type references nominal
4241-- | types from other modules, all information is contained that is needed to
4342-- | render those references.
44- type StandaloneTsType = TsBridgeM DTS.TsType
43+ -- type StandaloneTsType = TsBridgeM DTS.TsType
4544
4645-- | Type Class that is used by the type generator to recursively traverse
4746-- | types.
@@ -64,16 +63,19 @@ type StandaloneTsType = TsBridgeM DTS.TsType
6463
6564class TsBridgeBy :: Type -> Type -> Constraint
6665class TsBridgeBy tok a where
67- tsBridgeBy :: tok -> Proxy a -> StandaloneTsType
66+ tsBridgeBy :: tok -> Proxy a -> TsBridgeM DTS.TsType
6867
69- tsModuleFile :: String -> Array (TsBridgeM (Array DTS.TsDeclaration )) -> Either DTS.Error (Array DTS.TsModuleFile )
70- tsModuleFile n xs = do
71- (xs' /\ TsBridgeAccum { typeDefs }) <- runTsBridgeM $ mapErr (DTS.AtModule n) $ join <$> sequence xs
68+ tsModuleFile :: String -> Array (TsBridgeM (Array DTS.TsDeclaration )) -> Either AppError (Array DTS.TsModuleFile )
69+ tsModuleFile n xs =
70+ mapErr (AtModule n)
71+ do
72+ -- TODO: check for duplicate identifiers
7273
73- pure (typeDefs <> [ DTS.TsModuleFile ( DTS.TsFilePath (n <> " /index.d.ts " )) ( DTS.TsModule xs') ])
74+ _ <- mkPursModuleName n
7475
75- mapErr :: forall e m a . MonadError e m => (e -> e ) -> m a -> m a
76- mapErr f ma = catchError ma (f >>> throwError)
76+ (xs' /\ TsBridgeAccum { typeDefs }) <- runTsBridgeM $ join <$> sequence xs
77+
78+ pure (typeDefs <> [ DTS.TsModuleFile (DTS.TsFilePath (n <> " /index.d.ts" )) (DTS.TsModule xs') ])
7779
7880mergeModules :: Array DTS.TsModuleFile -> DTS.TsProgram
7981mergeModules xs =
@@ -87,17 +89,19 @@ mergeModule (DTS.TsModule ds1) (DTS.TsModule ds2) =
8789 DTS.TsModule
8890 (Array .nub (ds1 <> ds2))
8991
90- tsProgram :: Array (Either DTS.Error (Array DTS.TsModuleFile )) -> Either DTS.Error DTS.TsProgram
91- tsProgram xs = xs # sequence <#> join >>> mergeModules
92+ tsProgram :: Array (Either AppError (Array DTS.TsModuleFile )) -> Either AppError DTS.TsProgram
93+ tsProgram xs =
94+ -- TODO: check for duplicate modules
95+ xs # sequence <#> join >>> mergeModules
9296
9397-- | For rare cases where you want to export a type alias. References to this type
9498-- | alias will be fully resolved in the generated code. So it is more practical
9599-- | to use a newtype instead, which can be references by name.
96- tsTypeAlias :: forall tok a . TsBridgeBy tok a => tok -> TsNameDraft -> Proxy a -> TsBridgeM (Array DTS.TsDeclaration )
97- tsTypeAlias tok n x = ado
100+ tsTypeAlias :: forall tok a . TsBridgeBy tok a => tok -> String -> Proxy a -> TsBridgeM (Array DTS.TsDeclaration )
101+ tsTypeAlias tok aliasName x = ado
98102 x /\ scope <- listens (un TsBridgeAccum >>> _.scope >>> un Scope ) t
99- name <- DTS .mkTsName n
100- in [ DTS.TsDeclTypeDef name DTS.Public (coerce scope.floating) x ]
103+ name <- mkName aliasName
104+ in [ DTS.TsDeclTypeDef (toTsName name) DTS.Public (coerce scope.floating) x ]
101105 where
102106 t = tsBridgeBy tok x
103107
@@ -117,23 +121,26 @@ tsOpaqueType tok x = do
117121 _ -> pure []
118122
119123-- | Exports a single PureScript value to TypeScript. `tsValues` may be better choice.
120- tsValue :: forall tok a . TsBridgeBy tok a => tok -> TsNameDraft -> a -> TsBridgeM (Array DTS.TsDeclaration )
124+ tsValue :: forall tok a . TsBridgeBy tok a => tok -> String -> a -> TsBridgeM (Array DTS.TsDeclaration )
121125tsValue tok n _ = tsValue' tok n (Proxy :: _ a )
122126
123- tsValue' :: forall tok a . TsBridgeBy tok a => tok -> TsNameDraft -> Proxy a -> TsBridgeM (Array DTS.TsDeclaration )
124- tsValue' tok n _ = do
125- let t = tsBridgeBy tok (Proxy :: _ a )
126- x /\ scope <- listens (un TsBridgeAccum >>> _.scope >>> un Scope ) t
127+ tsValue' :: forall tok a . TsBridgeBy tok a => tok -> String -> Proxy a -> TsBridgeM (Array DTS.TsDeclaration )
128+ tsValue' tok n _ =
129+ mapErr (AtValue n)
130+ do
131+ let t = tsBridgeBy tok (Proxy :: _ a )
132+ x /\ scope <- listens (un TsBridgeAccum >>> _.scope >>> un Scope ) t
127133
128- name <- DTS .mkTsName n
134+ name <- mkName n
129135
130- when (OSet .length scope.floating /= 0 )
131- $ throwError
132- $ DTS.ErrUnquantifiedTypeVariables
133- $ (Set .fromFoldable :: Array _ -> _ )
134- $ OSet .toUnfoldable scope.floating
136+ when (OSet .length scope.floating /= 0 )
137+ ( throwError
138+ $ ErrUnquantifiedTypeVariables
139+ $ (Set .fromFoldable :: Array _ -> _ )
140+ $ OSet .toUnfoldable scope.floating
141+ )
135142
136- pure [ DTS.TsDeclValueDef name DTS.Public x ]
143+ pure [ DTS.TsDeclValueDef (toTsName name) DTS.Public x ]
137144
138145-- ------------------------------------------------------------------------------
139146-- class TsValues
@@ -169,4 +176,4 @@ instance
169176 tsValuesRL tok r _ = (<>) <$> head <*> tail
170177 where
171178 tail = tsValuesRL tok r (Proxy :: _ rl )
172- head = tsValue' tok (DTS.TsName $ reflectSymbol (Proxy :: _ sym )) (Proxy :: _ a )
179+ head = tsValue' tok (reflectSymbol (Proxy :: _ sym )) (Proxy :: _ a )
0 commit comments