Skip to content

Allow to export types easily #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Data/Aeson/TypeScript/Formatting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,30 @@ import qualified Data.Text as T
import Data.Monoid
#endif


-- | Same as 'formatTSDeclarations'', but uses default formatting options.
formatTSDeclarations :: [TSDeclaration] -> String
formatTSDeclarations = formatTSDeclarations' defaultFormattingOptions

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


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

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

exportPrefix :: ExportMode -> String
exportPrefix ExportEach = "export "
exportPrefix ExportNone = ""

-- | Format a list of TypeScript declarations into a string, suitable for putting directly into a @.d.ts@ file.
formatTSDeclarations' :: FormattingOptions -> [TSDeclaration] -> String
formatTSDeclarations' options declarations = T.unpack $ T.intercalate "\n\n" (fmap (T.pack . formatTSDeclaration options) declarations)
Expand Down
13 changes: 11 additions & 2 deletions src/Data/Aeson/TypeScript/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class (Typeable a) => TypeScript a where
isGenericVariable :: Proxy a -> Bool
-- ^ Special flag to indicate whether this type corresponds to a template variable.
isGenericVariable _ = False

-- | An existential wrapper for any TypeScript instance.
data TSType = forall a. (Typeable a, TypeScript a) => TSType { unTSType :: Proxy a }

Expand Down Expand Up @@ -91,20 +91,29 @@ instance IsString (TSString a) where

-- * Formatting options

data ExportMode =
ExportEach
-- ^ Prefix every declaration with the "export" keyword (suitable for putting in a TypeScripe module)
| ExportNone
-- ^ No exporting (suitable for putting in a .d.ts file)

data FormattingOptions = FormattingOptions
{ numIndentSpaces :: Int
-- ^ How many spaces to indent TypeScript blocks
, interfaceNameModifier :: String -> String
-- ^ Function applied to generated interface names
, typeNameModifier :: String -> String
-- ^ Function applied to generated type names
, exportMode :: ExportMode
-- ^ Prefix the generated types with "export" if set to 'True'.
}

defaultFormattingOptions :: FormattingOptions
defaultFormattingOptions = FormattingOptions
{ numIndentSpaces = 2
, interfaceNameModifier = id
, typeNameModifier = id
, exportMode = ExportNone
}

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

allStarConstructors :: [Type]
allStarConstructors = [ConT ''T1, ConT ''T2, ConT ''T3, ConT ''T4, ConT ''T5, ConT ''T6, ConT ''T7, ConT ''T8, ConT ''T9, ConT ''T10]
allStarConstructors = [ConT ''T1, ConT ''T2, ConT ''T3, ConT ''T4, ConT ''T5, ConT ''T6, ConT ''T7, ConT ''T8, ConT ''T9, ConT ''T10]

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