Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* Remove question mark in Data.Map instance
* Fix @no-emit-typescript not working for nullary constructors in simple enums

## 0.6.4.0

Expand Down
8 changes: 6 additions & 2 deletions src/Data/Aeson/TypeScript/Formatting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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 maybeDoc) =
formatTSDeclaration (FormattingOptions {..}) (TSTypeAlternatives name genericVariables ((fmap alternativeTypeName . filter (not . isNoEmitTypeScriptAlternative)) -> names) maybeDoc) =
makeDocPrefix maybeDoc <> mainDeclaration
where
mainDeclaration = case chooseTypeAlternativesFormat typeAlternativesFormat of
Expand Down Expand Up @@ -87,7 +87,7 @@ formatTSDeclarations' options allDeclarations =
getDeclarationName _ = Nothing

removeReferencesToRemovedNames :: [String] -> TSDeclaration -> TSDeclaration
removeReferencesToRemovedNames removedNames decl@(TSTypeAlternatives {..}) = decl { alternativeTypes = [x | x <- alternativeTypes, not (x `L.elem` removedNames)] }
removeReferencesToRemovedNames removedNames decl@(TSTypeAlternatives {..}) = decl { alternativeTypes = [x | x <- alternativeTypes, not (alternativeTypeName x `L.elem` removedNames)] }
removeReferencesToRemovedNames _ x = x

declarations = allDeclarations
Expand Down Expand Up @@ -119,3 +119,7 @@ isNoEmitTypeScriptDeclaration :: TSDeclaration -> Bool
isNoEmitTypeScriptDeclaration (TSInterfaceDeclaration {interfaceDoc=(Just doc)}) = noEmitTypeScriptAnnotation `L.isInfixOf` doc
isNoEmitTypeScriptDeclaration (TSTypeAlternatives {typeDoc=(Just doc)}) = noEmitTypeScriptAnnotation `L.isInfixOf` doc
isNoEmitTypeScriptDeclaration _ = False

isNoEmitTypeScriptAlternative :: TSAlternativeType -> Bool
isNoEmitTypeScriptAlternative (TSAlternativeType _ (Just doc)) = noEmitTypeScriptAnnotation `L.isInfixOf` doc
isNoEmitTypeScriptAlternative _ = False
19 changes: 11 additions & 8 deletions src/Data/Aeson/TypeScript/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
, T9(..)
, T10(..)

, module Data.Aeson.TypeScript.Instances

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.2.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.0.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 8.10.7

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.4.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.6.7

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.8.4

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.10.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.12.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.4.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.2.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 8.10.7

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.6.7

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.8.4

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.0.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.12.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.8.4

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.7

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.10.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.12.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.10.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.4.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.6.7

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.8.4

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.7

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.2.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.12.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.10.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.8.4

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.10.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.12.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.10.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.6.7

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.4.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.12.2

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.8.4

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing

Check warning on line 148 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.2.8

The export item ‘module Data.Aeson.TypeScript.Instances’ exports nothing
) where

import Control.Monad
Expand Down Expand Up @@ -248,7 +248,7 @@
if | (length datatypeCons == 1) && not (getTagSingleConstructors options) -> do
writeSingleConstructorEncoding
brackets <- lift $ getBracketsExpression False genericVariables
lift [|$(TH.stringE interfaceName) <> $(return brackets)|]
lift [|TSAlternativeType ($(TH.stringE interfaceName) <> $(return brackets)) Nothing|]
| allConstructorsAreNullary datatypeCons && allNullaryToStringTag options -> stringEncoding

-- With UntaggedValue, nullary constructors are encoded as strings
Expand All @@ -258,15 +258,15 @@
| isObjectWithSingleField $ sumEncoding options -> do
writeSingleConstructorEncoding
brackets <- lift $ getBracketsExpression False genericVariables
lift [|"{" <> $(TH.stringE $ show $ constructorNameToUse options ci) <> ": " <> $(TH.stringE interfaceName) <> $(return brackets) <> "}"|]
lift [|TSAlternativeType ("{" <> $(TH.stringE $ show $ constructorNameToUse options ci) <> ": " <> $(TH.stringE interfaceName) <> $(return brackets) <> "}") Nothing|]
| isTwoElemArray $ sumEncoding options -> do
writeSingleConstructorEncoding
brackets <- lift $ getBracketsExpression False genericVariables
lift [|"[" <> $(TH.stringE $ show $ constructorNameToUse options ci) <> ", " <> $(TH.stringE interfaceName) <> $(return brackets) <> "]"|]
lift [|TSAlternativeType ("[" <> $(TH.stringE $ show $ constructorNameToUse options ci) <> ", " <> $(TH.stringE interfaceName) <> $(return brackets) <> "]") Nothing|]
| isUntaggedValue $ sumEncoding options -> do
writeSingleConstructorEncoding
brackets <- lift $ getBracketsExpression False genericVariables
lift [|$(TH.stringE interfaceName) <> $(return brackets)|]
lift [|TSAlternativeType ($(TH.stringE interfaceName) <> $(return brackets)) Nothing|]
| otherwise -> do
tagField :: [Exp] <- lift $ case sumEncoding options of
TaggedObject tagFieldName _ -> (: []) <$> [|TSField False $(TH.stringE tagFieldName) $(TH.stringE [i|"#{constructorNameToUse options ci}"|]) Nothing|]
Expand All @@ -276,10 +276,13 @@
decl <- lift $ assembleInterfaceDeclaration (ListE (tagField ++ tsFields))
tell [ExtraDecl decl]
brackets <- lift $ getBracketsExpression False genericVariables
lift [|$(TH.stringE interfaceName) <> $(return brackets)|]
lift [|TSAlternativeType ($(TH.stringE interfaceName) <> $(return brackets)) Nothing|]

where
stringEncoding = lift $ TH.stringE [i|"#{(constructorTagModifier options) $ getTypeName (constructorName ci)}"|]
stringEncoding = do
let tagName = [i|"#{(constructorTagModifier options) $ getTypeName (constructorName ci)}"|]
lift [| TSAlternativeType $(TH.stringE tagName)
$(tryGetDoc haddockModifier (constructorName ci)) |]

writeSingleConstructorEncoding = if
| constructorVariant ci == NormalConstructor -> do
Expand All @@ -288,11 +291,11 @@

#if MIN_VERSION_aeson(0,10,0)
| unwrapUnaryRecords options && (isSingleRecordConstructor ci) -> do
let [typ] = constructorFields ci

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.2.8

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.4.8

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.6.7

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.8.4

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.10.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.12.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.4.8

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.2.8

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.6.7

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.8.4

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.12.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.8.4

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.7

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.8

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.10.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.12.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.10.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.4.8

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.8

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.6.7

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.8.4

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.7

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.2.8

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.12.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.10.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.8.4

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.10.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.12.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.10.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.6.7

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.4.8

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.12.2

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.8.4

Pattern match(es) are non-exhaustive

Check warning on line 294 in src/Data/Aeson/TypeScript/TH.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.2.8

Pattern match(es) are non-exhaustive
stringExp <- lift $ [|getTypeScriptTypeOrOptionalNull (Proxy :: Proxy $(return typ))|]
alternatives <- lift [|TSTypeAlternatives $(TH.stringE interfaceName)
$(genericVariablesListExpr True genericVariables)
[$(return stringExp)]
[TSAlternativeType $(return stringExp) Nothing]
$(tryGetDoc haddockModifier (constructorName ci))|]
tell [ExtraDecl alternatives]
#endif
Expand All @@ -311,7 +314,7 @@

lift [|TSTypeAlternatives $(TH.stringE interfaceName)
$(genericVariablesListExpr True genericVariables)
[$(return stringExp)]
[TSAlternativeType $(return stringExp) Nothing]
$(tryGetDoc haddockModifier (constructorName ci))|]

assembleInterfaceDeclaration members = [|TSInterfaceDeclaration $(TH.stringE interfaceName)
Expand Down
14 changes: 13 additions & 1 deletion src/Data/Aeson/TypeScript/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,23 @@ data TSDeclaration = TSInterfaceDeclaration { interfaceName :: String
, interfaceDoc :: Maybe String }
| TSTypeAlternatives { typeName :: String
, typeGenericVariables :: [String]
, alternativeTypes :: [String]
, alternativeTypes :: [TSAlternativeType]
, typeDoc :: Maybe String }
| TSRawDeclaration { text :: String }
deriving (Show, Eq, Ord)

data TSAlternativeType = TSAlternativeType {
-- | The type name/expression for this alternative
alternativeTypeName :: String
-- | Haddock documentation for this alternative
, alternativeTypeDoc :: Maybe String
} deriving (Show, Eq, Ord)

-- | Allows constructing 'TSAlternativeType' from string literals.
-- This keeps test code clean by allowing plain strings instead of full constructors.
instance IsString TSAlternativeType where
fromString s = TSAlternativeType s Nothing

data TSField = TSField
{ fieldOptional :: Bool
, fieldName :: String
Expand Down
10 changes: 10 additions & 0 deletions test/Formatting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ data NormalConstructors =
| Con2 Int
$(deriveTypeScript defaultOptions ''NormalConstructors)

data SimpleEnum =
EnumA
| -- | @no-emit-typescript
EnumB
| EnumC
$(deriveTypeScript defaultOptions ''SimpleEnum)

tests :: Spec
tests = describe "Formatting" $ do
describe "when given a Sum Type" $ do
Expand Down Expand Up @@ -104,6 +111,9 @@ tests = describe "Formatting" $ do

it [i|works on normal constructors|] $ do
formatTSDeclarations' defaultFormattingOptions (getTypeScriptDeclarations @NormalConstructors Proxy) `shouldBe` [i|type NormalConstructors = ICon2;\n\ninterface ICon2 {\n tag: "Con2";\n contents: number;\n}|]

it [i|works on nullary constructors in simple enums|] $ do
formatTSDeclarations' defaultFormattingOptions (getTypeScriptDeclarations @SimpleEnum Proxy) `shouldBe` [i|type SimpleEnum = "EnumA" | "EnumC";|]
#endif

main :: IO ()
Expand Down
2 changes: 1 addition & 1 deletion test/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tests = describe "Generic instances" $ do
(getTypeScriptDeclarationsRecursively (Proxy :: Proxy (Complex String))) `shouldBe` [
TSInterfaceDeclaration "IProduct" ["T"] [TSField False "tag" "\"Product\"" Nothing, TSField False "contents" "[number, T]" Nothing] Nothing
,TSInterfaceDeclaration "IUnary" ["T"] [TSField False "tag" "\"Unary\"" Nothing, TSField False "contents" "number" Nothing] Nothing
,TSTypeAlternatives "Complex" ["T"] ["IProduct<T>","IUnary<T>"] Nothing
,TSTypeAlternatives "Complex" ["T"] ["IProduct<T>", "IUnary<T>"] Nothing
]

it [i|Complex2 makes the declaration and types correctly|] $ do
Expand Down
Loading