Skip to content

Commit

Permalink
Fix the order in which the configurations are applied
Browse files Browse the repository at this point in the history
Fixes fourmolu#390

The documentation of `resolvePrinterOpts` states that "later options
override earlier", but it is not the case. This commit the application
order such that it is inline with the documentation.
  • Loading branch information
ozkutuk committed Dec 4, 2023
1 parent 08e89bb commit ee56ab3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ mkConfig path Opts {optQuiet, optConfig = cliConfig, optPrinterOpts = cliPrinter
cliConfig
{ cfgPrinterOpts =
resolvePrinterOpts
[ cliPrinterOpts,
cfgFilePrinterOpts fourmoluConfig
[ cfgFilePrinterOpts fourmoluConfig,
cliPrinterOpts
],
cfgFixityOverrides =
FixityOverrides . mconcat . map unFixityOverrides $
Expand Down
3 changes: 2 additions & 1 deletion src/Ormolu/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import Control.Monad (forM)
import Data.Aeson ((.!=), (.:?))
import Data.Aeson qualified as Aeson
import Data.Aeson.Types qualified as Aeson
import Data.Foldable (foldl')
import Data.Functor.Identity (Identity (..))
import Data.Map.Strict qualified as Map
import Data.Set (Set)
Expand Down Expand Up @@ -230,7 +231,7 @@ deriving instance Show PrinterOptsTotal

-- | Apply the given configuration in order (later options override earlier).
resolvePrinterOpts :: [PrinterOptsPartial] -> PrinterOptsTotal
resolvePrinterOpts = foldr fillMissingPrinterOpts defaultPrinterOpts
resolvePrinterOpts = foldl' (flip fillMissingPrinterOpts) defaultPrinterOpts

----------------------------------------------------------------------------
-- Loading Fourmolu configuration
Expand Down
7 changes: 6 additions & 1 deletion tests/Ormolu/ConfigSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Data.ByteString.Char8 qualified as Char8
import Data.List.NonEmpty qualified as NonEmpty
import Data.Map qualified as Map
import Data.Yaml qualified as Yaml
import Ormolu.Config (FourmoluConfig (..))
import Ormolu.Config (FourmoluConfig (..), poIndentation, resolvePrinterOpts)
import Ormolu.Fixity (ModuleReexports (..))
import Test.Hspec

Expand All @@ -26,3 +26,8 @@ spec = do
[ ("Foo", NonEmpty.fromList [(Nothing, "Bar2"), (Nothing, "Bar1")])
]
cfgFileReexports config `shouldBe` ModuleReexports expected
it "applies configurations in correct order" $ do
let opts1 = mempty {poIndentation = Just 2}
opts2 = mempty {poIndentation = Just 4}
configs = [opts1, opts2]
poIndentation (resolvePrinterOpts configs) `shouldBe` 4

0 comments on commit ee56ab3

Please sign in to comment.