Add support for post-qualified imports
This commit is contained in:
parent
ec8c9831cd
commit
3bea6e1ccb
@ -18,6 +18,7 @@ import SortImports.Types
|
|||||||
, Module(..)
|
, Module(..)
|
||||||
, Parser
|
, Parser
|
||||||
, View(..)
|
, View(..)
|
||||||
|
, Qualified(..)
|
||||||
)
|
)
|
||||||
|
|
||||||
rword :: String -> Parser String
|
rword :: String -> Parser String
|
||||||
@ -95,11 +96,13 @@ exposure = do
|
|||||||
module' :: Parser Module
|
module' :: Parser Module
|
||||||
module' = do
|
module' = do
|
||||||
void $ rword "import"
|
void $ rword "import"
|
||||||
_qualified <- isJust <$> optional (rword "qualified")
|
_qualified' <- isJust <$> optional (rword "qualified")
|
||||||
_name <- moduleName
|
_name <- moduleName
|
||||||
|
_qualified'' <- isJust <$> optional (rword "qualified")
|
||||||
_alias <- optional alias
|
_alias <- optional alias
|
||||||
_exports <- optional exposure
|
_exports <- optional exposure
|
||||||
space
|
space
|
||||||
|
let _qualified = if _qualified' then Just Qualified_Pre else if _qualified'' then Just Qualified_Post else Nothing
|
||||||
pure Module {..}
|
pure Module {..}
|
||||||
|
|
||||||
sortExports :: Module -> Module
|
sortExports :: Module -> Module
|
||||||
|
@ -28,8 +28,13 @@ data Exposure a
|
|||||||
| Hidden a
|
| Hidden a
|
||||||
deriving (Eq, Functor, Show)
|
deriving (Eq, Functor, Show)
|
||||||
|
|
||||||
|
data Qualified
|
||||||
|
= Qualified_Pre
|
||||||
|
| Qualified_Post
|
||||||
|
deriving (Eq, Ord, Show)
|
||||||
|
|
||||||
data Module = Module
|
data Module = Module
|
||||||
{ _qualified :: Bool
|
{ _qualified :: Maybe Qualified
|
||||||
, _name :: String
|
, _name :: String
|
||||||
, _alias :: Maybe String
|
, _alias :: Maybe String
|
||||||
, _exports :: Maybe (Exposure [Export])
|
, _exports :: Maybe (Exposure [Export])
|
||||||
@ -63,8 +68,9 @@ instance View Module where
|
|||||||
view (Module qualified name alias exports) =
|
view (Module qualified name alias exports) =
|
||||||
unwords . filter (/= "") $
|
unwords . filter (/= "") $
|
||||||
[ "import"
|
[ "import"
|
||||||
, if qualified then "qualified" else ""
|
, if qualified == Just Qualified_Pre then "qualified" else ""
|
||||||
, name
|
, name
|
||||||
|
, if qualified == Just Qualified_Post then "qualified" else ""
|
||||||
, maybe "" ("as " <>) alias
|
, maybe "" ("as " <>) alias
|
||||||
, maybe "" view exports
|
, maybe "" view exports
|
||||||
]
|
]
|
||||||
|
10
test/Spec.hs
10
test/Spec.hs
@ -10,8 +10,10 @@ unitTests :: TestTree
|
|||||||
unitTests = testGroup "Unit tests"
|
unitTests = testGroup "Unit tests"
|
||||||
[ testCase "" $
|
[ testCase "" $
|
||||||
lineType "not a module" @?= CodeLine "not a module"
|
lineType "not a module" @?= CodeLine "not a module"
|
||||||
, testCase "" $
|
, testCase "bare" $
|
||||||
lineType "import ModuleName" @?= ModuleLine (Module False "ModuleName" Nothing Nothing)
|
lineType "import ModuleName" @?= ModuleLine (Module Nothing "ModuleName" Nothing Nothing)
|
||||||
, testCase "" $
|
, testCase "pre-qualified" $
|
||||||
lineType "import ModuleName" @?= ModuleLine (Module False "ModuleName" Nothing Nothing)
|
lineType "import qualified ModuleName" @?= ModuleLine (Module (Just Qualified_Pre) "ModuleName" Nothing Nothing)
|
||||||
|
, testCase "post-qualified" $
|
||||||
|
lineType "import ModuleName qualified as Mod" @?= ModuleLine (Module (Just Qualified_Post) "ModuleName" (Just "Mod") Nothing)
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user