From ec66e577571721ae9841d84a5603d69bc45b214e Mon Sep 17 00:00:00 2001 From: Maxim Koltsov Date: Sat, 1 Feb 2020 12:03:36 +0300 Subject: [PATCH 1/2] Disable formatting of data types without records Perhaps the safest way for now is to revert to old behavior for data types -- i.e. do nothing. Attemmpt reformatting only if at least one constructor has record fields. Fixes https://github.com/jaspervdj/stylish-haskell/issues/262 --- lib/Language/Haskell/Stylish/Step/Data.hs | 10 ++++++++-- .../Haskell/Stylish/Step/Data/Tests.hs | 20 +++++++++++++------ tests/Language/Haskell/Stylish/Tests.hs | 10 ++++------ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/Language/Haskell/Stylish/Step/Data.hs b/lib/Language/Haskell/Stylish/Step/Data.hs index 94aaf22f..681c7c8c 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 712ffaee..45fc8e53 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" + ] diff --git a/tests/Language/Haskell/Stylish/Tests.hs b/tests/Language/Haskell/Stylish/Tests.hs index 3a27ce7a..16f7009d 100644 --- a/tests/Language/Haskell/Stylish/Tests.hs +++ b/tests/Language/Haskell/Stylish/Tests.hs @@ -28,10 +28,9 @@ 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" result = Right [ "module Herp where" - , "data Foo = Bar" - , " | Baz" + , "data Foo = Bar | Baz" ] @@ -47,10 +46,9 @@ 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" result = Right [ "module Herp where" - , "data Foo = Bar" - , " | Baz" + , "data Foo = Bar | Baz" ] From 398d75cac44f90e20bfb340056df21dd3231617b Mon Sep 17 00:00:00 2001 From: Maxim Koltsov Date: Sun, 2 Feb 2020 12:48:15 +0300 Subject: [PATCH 2/2] Changes by review comments --- tests/Language/Haskell/Stylish/Step/Data/Tests.hs | 2 +- tests/Language/Haskell/Stylish/Tests.hs | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs index 45fc8e53..ff5ca3be 100644 --- a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs +++ b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs @@ -395,5 +395,5 @@ case20 = input @=? testStep (step 2) input input = unlines [ "module Herp where" , "" - , "data Tag = Title | Text" + , "data Tag = Title | Text deriving (Eq, Show)" ] diff --git a/tests/Language/Haskell/Stylish/Tests.hs b/tests/Language/Haskell/Stylish/Tests.hs index 16f7009d..59ca92bf 100644 --- a/tests/Language/Haskell/Stylish/Tests.hs +++ b/tests/Language/Haskell/Stylish/Tests.hs @@ -28,9 +28,12 @@ tests = testGroup "Language.Haskell.Stylish.Step.Tabs.Tests" case01 :: Assertion case01 = (@?= result) =<< format Nothing Nothing input where - input = "module Herp where\ndata Foo = Bar | Baz" + input = "module Herp where\ndata Foo = Bar | Baz { baz :: Int }" result = Right [ "module Herp where" - , "data Foo = Bar | Baz" + , "data Foo = Bar" + , " | Baz" + , " { baz :: Int" + , " }" ] @@ -46,9 +49,12 @@ case02 = withTestDirTree $ do actual <- format (Just $ ConfigPath "test-config.yaml") Nothing input actual @?= result where - input = "module Herp where\ndata Foo = Bar | Baz" + input = "module Herp where\ndata Foo = Bar | Baz { baz :: Int }" result = Right [ "module Herp where" - , "data Foo = Bar | Baz" + , "data Foo = Bar" + , " | Baz" + , " { baz :: Int" + , " }" ]