diff options
author | Maxim Koltsov <kolmax94@gmail.com> | 2020-02-02 15:18:24 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-02 13:18:24 +0100 |
commit | 41dcda2a34b5f12f3fa91480bfe2aaeb4afa90e5 (patch) | |
tree | 84f661b8d74b8a6525ed1c24d3319b053ae1f151 | |
parent | d2594404a89839edc3ac44ea1fed3f99c2aade68 (diff) | |
download | stylish-haskell-41dcda2a34b5f12f3fa91480bfe2aaeb4afa90e5.tar.gz |
Disable formatting of data types without records (#265)
-rw-r--r-- | lib/Language/Haskell/Stylish/Step/Data.hs | 10 | ||||
-rw-r--r-- | tests/Language/Haskell/Stylish/Step/Data/Tests.hs | 20 | ||||
-rw-r--r-- | tests/Language/Haskell/Stylish/Tests.hs | 8 |
3 files changed, 28 insertions, 10 deletions
diff --git a/lib/Language/Haskell/Stylish/Step/Data.hs b/lib/Language/Haskell/Stylish/Step/Data.hs index 94aaf22..681c7c8 100644 --- a/lib/Language/Haskell/Stylish/Step/Data.hs +++ b/lib/Language/Haskell/Stylish/Step/Data.hs @@ -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 diff --git a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs index 712ffae..ff5ca3b 100644 --- a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs +++ b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs @@ -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 @@ -155,7 +156,7 @@ 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" @@ -163,11 +164,6 @@ case08 = expected @=? testStep (step 2) input , "data Phantom a =" , " Phantom" ] - expected = unlines - [ "module Herp where" - , "" - , "data Phantom a = Phantom" - ] case09 :: Assertion case09 = expected @=? testStep (step 4) input @@ -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)" + ] diff --git a/tests/Language/Haskell/Stylish/Tests.hs b/tests/Language/Haskell/Stylish/Tests.hs index 3a27ce7..59ca92b 100644 --- a/tests/Language/Haskell/Stylish/Tests.hs +++ b/tests/Language/Haskell/Stylish/Tests.hs @@ -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" + , " }" ] @@ -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" + , " }" ] |