Skip to content

Commit

Permalink
Add a better check for enum mode
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjm committed Dec 1, 2023
1 parent 7933c64 commit 2e5c123
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
3 changes: 2 additions & 1 deletion aeson-typescript.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.1.
-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack

Expand Down Expand Up @@ -67,6 +67,7 @@ library
build-depends:
aeson
, base >=4.7 && <5
, bytestring
, containers
, mtl
, string-interpolate
Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tested-with:
dependencies:
- aeson
- base >= 4.7 && < 5
- bytestring
- containers
- mtl
- string-interpolate
Expand Down
28 changes: 23 additions & 5 deletions src/Data/Aeson/TypeScript/Formatting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

module Data.Aeson.TypeScript.Formatting where

import Data.Aeson as A
import Data.Aeson.TypeScript.Types
import qualified Data.ByteString.Lazy.Char8 as BL8
import Data.Function ((&))
import qualified Data.List as L
import Data.Maybe
Expand All @@ -23,15 +25,31 @@ formatTSDeclaration :: FormattingOptions -> TSDeclaration -> String
formatTSDeclaration (FormattingOptions {..}) (TSTypeAlternatives name genericVariables names maybeDoc) =
makeDocPrefix maybeDoc <> mainDeclaration
where
mainDeclaration = case typeAlternativesFormat of
mainDeclaration = case chooseTypeAlternativesFormat typeAlternativesFormat of
Enum -> [i|#{exportPrefix exportMode}enum #{typeNameModifier name} { #{alternativesEnum} }|]
where
alternativesEnum = T.intercalate ", " $ [toEnumName entry | entry <- T.pack <$> names]
EnumWithType -> [i|#{exportPrefix exportMode}enum #{typeNameModifier name}Enum { #{alternativesEnumWithType} }#{enumType}|]
where
alternativesEnumWithType = T.intercalate ", " $ [toEnumName entry <> "=" <> entry | entry <- T.pack <$> names]
enumType = [i|\n\ntype #{name} = keyof typeof #{typeNameModifier name}Enum;|] :: T.Text
TypeAlias -> [i|#{exportPrefix exportMode}type #{typeNameModifier name}#{getGenericBrackets genericVariables} = #{alternatives};|]
where
alternatives = T.intercalate " | " (fmap T.pack names)

-- Only allow certain formats if some checks pass
chooseTypeAlternativesFormat Enum
| all isDoubleQuotedString names = Enum
| otherwise = TypeAlias
chooseTypeAlternativesFormat EnumWithType
| all isDoubleQuotedString names = EnumWithType
| otherwise = TypeAlias
chooseTypeAlternativesFormat x = x

isDoubleQuotedString s = case A.eitherDecode (BL8.pack s) of
Right (A.String _) -> True
_ -> False

alternatives = T.intercalate " | " (fmap T.pack names)
alternativesEnum = T.intercalate ", " $ [toEnumName entry | entry <- T.pack <$> names]
alternativesEnumWithType = T.intercalate ", " $ [toEnumName entry <> "=" <> entry | entry <- T.pack <$> names]
enumType = [i|\n\ntype #{name} = keyof typeof #{typeNameModifier name}Enum;|] :: T.Text
toEnumName = T.replace "\"" ""

formatTSDeclaration (FormattingOptions {..}) (TSInterfaceDeclaration interfaceName genericVariables (filter (not . isNoEmitTypeScriptField) -> members) maybeDoc) =
Expand Down
10 changes: 5 additions & 5 deletions test/Formatting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Formatting (tests) where

import Control.Exception
import Data.Aeson (defaultOptions)
import Data.Aeson.TH
import Data.Aeson.TypeScript.TH
import Data.Proxy
import Data.String.Interpolate
Expand All @@ -20,7 +19,6 @@ $(deriveTypeScript defaultOptions ''D2)

data Unit = U deriving (Eq, Show)
$(deriveTypeScript defaultOptions ''Unit)
$(deriveJSON defaultOptions ''Unit)

data PrimeInType' = PrimeInType
$(deriveTypeScript defaultOptions ''PrimeInType')
Expand Down Expand Up @@ -66,9 +64,11 @@ tests = describe "Formatting" $ do

enum D2 { S2, F2 }|]

-- it "should generate a TS Enum from unit" $
-- formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @Unit Proxy) `shouldBe`
-- [__i|enum Unit { U }|]
it "should generate a TS Enum from unit" $
formatTSDeclarations' (defaultFormattingOptions { typeAlternativesFormat = Enum }) (getTypeScriptDeclarations @Unit Proxy) `shouldBe`
[__i|type Unit = IU;

type IU = void[];|]

describe "and the EnumWithType format option is set" $
it "should generate a TS Enum with a type declaration" $
Expand Down

0 comments on commit 2e5c123

Please sign in to comment.