Separate conversion from parsing
This commit is contained in:
parent
fc327d7e3f
commit
f91206517e
@ -93,8 +93,8 @@ exposure = do
|
|||||||
hiding <- isJust <$> optional (rword "hiding")
|
hiding <- isJust <$> optional (rword "hiding")
|
||||||
(if hiding then Hidden else Exposed) <$> exports
|
(if hiding then Hidden else Exposed) <$> exports
|
||||||
|
|
||||||
module' :: Maybe Qualified -> Parser Module
|
module' :: Parser Module
|
||||||
module' cfgQualified = do
|
module' = do
|
||||||
void $ rword "import"
|
void $ rword "import"
|
||||||
_qualified' <- isJust <$> optional (rword "qualified")
|
_qualified' <- isJust <$> optional (rword "qualified")
|
||||||
_name <- moduleName
|
_name <- moduleName
|
||||||
@ -102,19 +102,15 @@ module' cfgQualified = do
|
|||||||
_alias <- optional alias
|
_alias <- optional alias
|
||||||
_exports <- optional exposure
|
_exports <- optional exposure
|
||||||
space
|
space
|
||||||
let _qualified = case cfgQualified of
|
let _qualified = if _qualified' then Just Qualified_Pre else if _qualified'' then Just Qualified_Post else Nothing
|
||||||
Nothing -> if _qualified'
|
|
||||||
then Just Qualified_Pre
|
|
||||||
else if _qualified'' then Just Qualified_Post else Nothing
|
|
||||||
Just q -> if (_qualified' || _qualified'') then Just q else Nothing
|
|
||||||
pure Module {..}
|
pure Module {..}
|
||||||
|
|
||||||
sortExports :: Module -> Module
|
sortExports :: Module -> Module
|
||||||
sortExports m = m { _exports = fmap sort <$> _exports m }
|
sortExports m = m { _exports = fmap sort <$> _exports m }
|
||||||
|
|
||||||
lineType :: Maybe Qualified -> String -> LineType
|
lineType :: String -> LineType
|
||||||
lineType mq x =
|
lineType x =
|
||||||
case parse (module' mq) "" x of
|
case parse module' "" x of
|
||||||
Left _ -> CodeLine x
|
Left _ -> CodeLine x
|
||||||
Right m -> ModuleLine m
|
Right m -> ModuleLine m
|
||||||
|
|
||||||
@ -135,8 +131,10 @@ sortImports :: Maybe Qualified -> String -> String
|
|||||||
sortImports mq
|
sortImports mq
|
||||||
= unlines
|
= unlines
|
||||||
. fmap (\case CodeLine x -> x
|
. fmap (\case CodeLine x -> x
|
||||||
ModuleLine x -> view x)
|
ModuleLine x -> view $ convert x)
|
||||||
. concatMap sortIfModules
|
. concatMap sortIfModules
|
||||||
. groupLines
|
. groupLines
|
||||||
. map (lineType mq)
|
. map lineType
|
||||||
. lines
|
. lines
|
||||||
|
where
|
||||||
|
convert x = x { _qualified = mq <|> _qualified x}
|
||||||
|
@ -9,11 +9,11 @@ main = defaultMain $ testGroup "Tests" [unitTests]
|
|||||||
unitTests :: TestTree
|
unitTests :: TestTree
|
||||||
unitTests = testGroup "Unit tests"
|
unitTests = testGroup "Unit tests"
|
||||||
[ testCase "" $
|
[ testCase "" $
|
||||||
lineType Nothing "not a module" @?= CodeLine "not a module"
|
lineType "not a module" @?= CodeLine "not a module"
|
||||||
, testCase "bare" $
|
, testCase "bare" $
|
||||||
lineType Nothing "import ModuleName" @?= ModuleLine (Module Nothing "ModuleName" Nothing Nothing)
|
lineType "import ModuleName" @?= ModuleLine (Module Nothing "ModuleName" Nothing Nothing)
|
||||||
, testCase "pre-qualified" $
|
, testCase "pre-qualified" $
|
||||||
lineType Nothing "import qualified ModuleName" @?= ModuleLine (Module (Just Qualified_Pre) "ModuleName" Nothing Nothing)
|
lineType "import qualified ModuleName" @?= ModuleLine (Module (Just Qualified_Pre) "ModuleName" Nothing Nothing)
|
||||||
, testCase "post-qualified" $
|
, testCase "post-qualified" $
|
||||||
lineType Nothing "import ModuleName qualified as Mod" @?= ModuleLine (Module (Just Qualified_Post) "ModuleName" (Just "Mod") Nothing)
|
lineType "import ModuleName qualified as Mod" @?= ModuleLine (Module (Just Qualified_Post) "ModuleName" (Just "Mod") Nothing)
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user