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