Skip to content

Commit 6778282

Browse files
bitonicpicnoir
authored andcommitted
Allow to export types easily
In some cases, we'd like to use the generated types in a typescript module instead of a declaration file. When used in a module, the types need to be explicitely exported to be re-used elsewhere. Adding a new exportTypes FormattingOption in charge of prefixing the generated types with "export".
1 parent 53555f6 commit 6778282

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/Data/Aeson/TypeScript/Formatting.hs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,32 @@ import qualified Data.Text as T
1010
import Data.Monoid
1111
#endif
1212

13-
13+
1414
-- | Same as 'formatTSDeclarations'', but uses default formatting options.
1515
formatTSDeclarations :: [TSDeclaration] -> String
1616
formatTSDeclarations = formatTSDeclarations' defaultFormattingOptions
1717

1818
-- | Format a single TypeScript declaration. This version accepts a FormattingOptions object in case you want more control over the output.
1919
formatTSDeclaration :: FormattingOptions -> TSDeclaration -> String
2020
formatTSDeclaration (FormattingOptions {..}) (TSTypeAlternatives name genericVariables names) =
21-
[i|type #{typeNameModifier name}#{getGenericBrackets genericVariables} = #{alternatives};|]
21+
[i|#{exportPrefix exportTypes}type #{typeNameModifier name}#{getGenericBrackets genericVariables} = #{alternatives};|]
2222
where alternatives = T.intercalate " | " (fmap T.pack names)
2323

24+
2425
formatTSDeclaration (FormattingOptions {..}) (TSInterfaceDeclaration interfaceName genericVariables members) =
25-
[i|interface #{modifiedInterfaceName}#{getGenericBrackets genericVariables} {
26+
[i|#{exportPrefix exportTypes}interface #{modifiedInterfaceName}#{getGenericBrackets genericVariables} {
2627
#{ls}
2728
}|] where ls = T.intercalate "\n" $ fmap T.pack [(replicate numIndentSpaces ' ') <> formatTSField member <> ";"| member <- members]
2829
modifiedInterfaceName = (\(li, name) -> li <> interfaceNameModifier name) . splitAt 1 $ interfaceName
2930

3031
formatTSDeclaration (FormattingOptions {..}) (TSRawDeclaration text) = text
3132

33+
exportPrefix :: ExportMode -> String
34+
exportPrefix em =
35+
case em of
36+
ExportEach -> "export "
37+
ExportNone -> ""
38+
3239
-- | Format a list of TypeScript declarations into a string, suitable for putting directly into a @.d.ts@ file.
3340
formatTSDeclarations' :: FormattingOptions -> [TSDeclaration] -> String
3441
formatTSDeclarations' options declarations = T.unpack $ T.intercalate "\n\n" (fmap (T.pack . formatTSDeclaration options) declarations)

src/Data/Aeson/TypeScript/Types.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class (Typeable a) => TypeScript a where
5858
isGenericVariable :: Proxy a -> Bool
5959
-- ^ Special flag to indicate whether this type corresponds to a template variable.
6060
isGenericVariable _ = False
61-
61+
6262
-- | An existential wrapper for any TypeScript instance.
6363
data TSType = forall a. (Typeable a, TypeScript a) => TSType { unTSType :: Proxy a }
6464

@@ -91,20 +91,29 @@ instance IsString (TSString a) where
9191

9292
-- * Formatting options
9393

94+
data ExportMode =
95+
ExportEach
96+
-- ^ Prefix every declaration with the "export" keyword (suitable for putting in a TypeScripe module)
97+
| ExportNone
98+
-- ^ No exporting (suitable for putting in a .d.ts file)
99+
94100
data FormattingOptions = FormattingOptions
95101
{ numIndentSpaces :: Int
96102
-- ^ How many spaces to indent TypeScript blocks
97103
, interfaceNameModifier :: String -> String
98104
-- ^ Function applied to generated interface names
99105
, typeNameModifier :: String -> String
100106
-- ^ Function applied to generated type names
107+
, exportMode :: ExportMode
108+
-- ^ Prefix the generated types with "export" if set to 'True'.
101109
}
102110

103111
defaultFormattingOptions :: FormattingOptions
104112
defaultFormattingOptions = FormattingOptions
105113
{ numIndentSpaces = 2
106114
, interfaceNameModifier = id
107115
, typeNameModifier = id
116+
, exportTypes = ExportNone
108117
}
109118

110119
-- | Convenience typeclass class you can use to "attach" a set of Aeson encoding options to a type.
@@ -136,7 +145,7 @@ instance TypeScript T9 where getTypeScriptType _ = "T9"; isGenericVariable _ = T
136145
instance TypeScript T10 where getTypeScriptType _ = "T10"; isGenericVariable _ = True
137146

138147
allStarConstructors :: [Type]
139-
allStarConstructors = [ConT ''T1, ConT ''T2, ConT ''T3, ConT ''T4, ConT ''T5, ConT ''T6, ConT ''T7, ConT ''T8, ConT ''T9, ConT ''T10]
148+
allStarConstructors = [ConT ''T1, ConT ''T2, ConT ''T3, ConT ''T4, ConT ''T5, ConT ''T6, ConT ''T7, ConT ''T8, ConT ''T9, ConT ''T10]
140149

141150
allStarConstructors' :: [Name]
142151
allStarConstructors' = [''T1, ''T2, ''T3, ''T4, ''T5, ''T6, ''T7, ''T8, ''T9, ''T10]

0 commit comments

Comments
 (0)