Skip to content

Commit a7d273e

Browse files
committed
Get rid of validateFormattingOptions
1 parent 1afccfe commit a7d273e

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

src/Data/Aeson/TypeScript/Formatting.hs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ formatTSDeclaration (FormattingOptions {..}) (TSTypeAlternatives name genericVar
2525
where
2626
mainDeclaration = case typeAlternativesFormat of
2727
Enum -> [i|#{exportPrefix exportMode}enum #{typeNameModifier name} { #{alternativesEnum} }|]
28-
EnumWithType -> [i|#{exportPrefix exportMode}enum #{typeNameModifier name} { #{alternativesEnumWithType} }#{enumType}|]
28+
EnumWithType -> [i|#{exportPrefix exportMode}enum #{typeNameModifier name}Enum { #{alternativesEnumWithType} }#{enumType}|]
2929
TypeAlias -> [i|#{exportPrefix exportMode}type #{typeNameModifier name}#{getGenericBrackets genericVariables} = #{alternatives};|]
3030

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

3737
formatTSDeclaration (FormattingOptions {..}) (TSInterfaceDeclaration interfaceName genericVariables (filter (not . isNoEmitTypeScriptField) -> members) maybeDoc) =
@@ -57,18 +57,16 @@ exportPrefix ExportNone = ""
5757
-- | Format a list of TypeScript declarations into a string, suitable for putting directly into a @.d.ts@ file.
5858
formatTSDeclarations' :: FormattingOptions -> [TSDeclaration] -> String
5959
formatTSDeclarations' options allDeclarations =
60-
declarations & fmap (T.pack . formatTSDeclaration (validateFormattingOptions options declarations))
60+
declarations & fmap (T.pack . formatTSDeclaration options)
6161
& T.intercalate "\n\n"
6262
& T.unpack
6363
where
64-
removedDeclarations = filter isNoEmitTypeScriptDeclaration allDeclarations
65-
66-
getDeclarationName :: TSDeclaration -> Maybe String
67-
getDeclarationName (TSInterfaceDeclaration {..}) = Just interfaceName
68-
getDeclarationName (TSTypeAlternatives {..}) = Just typeName
69-
_ = Nothing
70-
71-
removedDeclarationNames = mapMaybe getDeclarationName removedDeclarations
64+
removedDeclarationNames = mapMaybe getDeclarationName (filter isNoEmitTypeScriptDeclaration allDeclarations)
65+
where
66+
getDeclarationName :: TSDeclaration -> Maybe String
67+
getDeclarationName (TSInterfaceDeclaration {..}) = Just interfaceName
68+
getDeclarationName (TSTypeAlternatives {..}) = Just typeName
69+
_ = Nothing
7270

7371
removeReferencesToRemovedNames :: [String] -> TSDeclaration -> TSDeclaration
7472
removeReferencesToRemovedNames removedNames decl@(TSTypeAlternatives {..}) = decl { alternativeTypes = [x | x <- alternativeTypes, not (x `L.elem` removedNames)] }
@@ -78,20 +76,6 @@ formatTSDeclarations' options allDeclarations =
7876
& filter (not . isNoEmitTypeScriptDeclaration)
7977
& fmap (removeReferencesToRemovedNames removedDeclarationNames)
8078

81-
validateFormattingOptions :: FormattingOptions -> [TSDeclaration] -> FormattingOptions
82-
validateFormattingOptions options@FormattingOptions{..} decls
83-
| typeAlternativesFormat == Enum && isPlainSumType decls = options
84-
| typeAlternativesFormat == EnumWithType && isPlainSumType decls = options { typeNameModifier = flip (<>) "Enum" }
85-
| otherwise = options { typeAlternativesFormat = TypeAlias }
86-
where
87-
isInterface :: TSDeclaration -> Bool
88-
isInterface TSInterfaceDeclaration{} = True
89-
isInterface _ = False
90-
91-
-- Plain sum types have only one declaration with multiple alternatives
92-
-- Units (data U = U) contain two declarations, and thus are invalid
93-
isPlainSumType ds = (not . any isInterface $ ds) && length ds == 1
94-
9579
makeDocPrefix :: Maybe String -> String
9680
makeDocPrefix maybeDoc = case maybeDoc of
9781
Nothing -> ""

0 commit comments

Comments
 (0)