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
10 changes: 8 additions & 2 deletions lib/Language/Haskell/Stylish/Step/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ commentsWithin lb = filter within

changeDecl :: [Comment] -> Int -> H.Decl LineBlock -> Maybe ChangeLine
changeDecl _ _ (H.DataDecl _ (H.DataType _) Nothing _ [] _) = Nothing
changeDecl allComments indentSize (H.DataDecl block (H.DataType _) Nothing dhead decls derivings) =
Just $ change block (const $ concat newLines)
changeDecl allComments indentSize (H.DataDecl block (H.DataType _) Nothing dhead decls derivings)
| hasRecordFields = Just $ change block (const $ concat newLines)
| otherwise = Nothing
where
hasRecordFields = any
(\qual -> case qual of
(H.QualConDecl _ _ _ (H.RecDecl {})) -> True
_ -> False)
decls
newLines = fmap constructors zipped ++ [fmap (indented . H.prettyPrint) derivings]
zipped = zip decls ([1..] ::[Int])
constructors (decl, 1) = processConstructor allComments typeConstructor indentSize decl
Expand Down
20 changes: 14 additions & 6 deletions tests/Language/Haskell/Stylish/Step/Data/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Data.Tests"
, testCase "case 17" case17
, testCase "case 18" case18
, testCase "case 19" case19
, testCase "case 20 (issue 262)" case20
]

case00 :: Assertion
Expand Down Expand Up @@ -155,19 +156,14 @@ case07 = expected @=? testStep (step 2) input
expected = input

case08 :: Assertion
case08 = expected @=? testStep (step 2) input
case08 = input @=? testStep (step 2) input
where
input = unlines
[ "module Herp where"
, ""
, "data Phantom a ="
, " Phantom"
]
expected = unlines
[ "module Herp where"
, ""
, "data Phantom a = Phantom"
]

case09 :: Assertion
case09 = expected @=? testStep (step 4) input
Expand Down Expand Up @@ -389,3 +385,15 @@ case19 = expected @=? testStep (step 2) input
, " , age :: Int"
, " }"
]

-- | Should not break Enums (data without records) formating
--
-- See https://github.com/jaspervdj/stylish-haskell/issues/262
case20 :: Assertion
case20 = input @=? testStep (step 2) input
where
input = unlines
[ "module Herp where"
, ""
, "data Tag = Title | Text deriving (Eq, Show)"
]
8 changes: 6 additions & 2 deletions tests/Language/Haskell/Stylish/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ tests = testGroup "Language.Haskell.Stylish.Step.Tabs.Tests"
case01 :: Assertion
case01 = (@?= result) =<< format Nothing Nothing input
where
input = "module Herp where\n data Foo = Bar | Baz"
input = "module Herp where\ndata Foo = Bar | Baz { baz :: Int }"
result = Right [ "module Herp where"
, "data Foo = Bar"
, " | Baz"
, " { baz :: Int"
, " }"
]


Expand All @@ -47,10 +49,12 @@ case02 = withTestDirTree $ do
actual <- format (Just $ ConfigPath "test-config.yaml") Nothing input
actual @?= result
where
input = "module Herp where\n data Foo = Bar | Baz"
input = "module Herp where\ndata Foo = Bar | Baz { baz :: Int }"
result = Right [ "module Herp where"
, "data Foo = Bar"
, " | Baz"
, " { baz :: Int"
, " }"
]


Expand Down