@@ -6,7 +6,7 @@ module Language.PureScript.Linter.Imports (findUnusedImports, Name(..), UsedImpo
66
77import qualified Data.Map as M
88import Data.Maybe (mapMaybe )
9- import Data.List ((\\) )
9+ import Data.List ((\\) , find )
1010import Control.Monad.Error.Class (MonadError (.. ))
1111import Control.Monad.Writer.Class
1212import Control.Monad (unless ,when )
@@ -26,7 +26,7 @@ import Language.PureScript.Sugar.Names.Imports
2626import qualified Language.PureScript.Constants as C
2727
2828-- | Imported name used in some type or expression.
29- data Name = IdentName (Qualified Ident ) | IsProperName (Qualified ProperName )
29+ data Name = IdentName (Qualified Ident ) | IsProperName (Qualified ProperName ) | DctorName ( Qualified ProperName )
3030
3131-- | Map of module name to list of imported names from that module which have been used.
3232type UsedImports = M. Map ModuleName [Name ]
@@ -35,12 +35,12 @@ type UsedImports = M.Map ModuleName [Name]
3535-- Find and warn on any unused import statements (qualified or unqualified)
3636-- or references in an explicit import list.
3737--
38- findUnusedImports :: forall m . (Applicative m , MonadError MultipleErrors m , MonadWriter MultipleErrors m ) => Module -> Imports -> UsedImports -> m ()
39- findUnusedImports (Module _ _ _ mdecls _) _ usedImps = do
38+ findUnusedImports :: forall m . (Applicative m , MonadError MultipleErrors m , MonadWriter MultipleErrors m ) => Module -> Env -> UsedImports -> m ()
39+ findUnusedImports (Module _ _ _ mdecls _) env usedImps = do
4040 imps <- findImports mdecls
4141 forM_ (M. toAscList imps) $ \ (mni, decls) -> unless (mni `elem` autoIncludes) $
4242 forM_ decls $ \ (ss, declType, qualifierName) -> censor (onErrorMessages $ addModuleLocError ss) $
43- let usedNames = mapMaybe (matchName qualifierName) $ sugarNames ++ M. findWithDefault [] mni usedImps in
43+ let usedNames = mapMaybe (matchName (typeForDCtor mni) qualifierName) $ sugarNames ++ M. findWithDefault [] mni usedImps in
4444 case declType of
4545 Implicit -> when (null usedNames) $ tell $ errorMessage $ UnusedImport mni
4646 Explicit declrefs -> do
@@ -55,14 +55,24 @@ findUnusedImports (Module _ _ _ mdecls _) _ usedImps = do
5555 autoIncludes :: [ ModuleName ]
5656 autoIncludes = [ ModuleName [ProperName C. prim] ]
5757
58- matchName :: Maybe ModuleName -> Name -> Maybe String
59- matchName qual (IdentName (Qualified q x)) | q == qual = Just $ runIdent x
60- matchName qual (IsProperName (Qualified q x)) | q == qual = Just $ runProperName x
61- matchName _ _ = Nothing
58+ typeForDCtor :: ModuleName -> ProperName -> Maybe ProperName
59+ typeForDCtor mn pn =
60+ getTy <$> find matches tys
61+ where
62+ matches ((_, ctors), _) = pn `elem` ctors
63+ getTy ((ty, _), _) = ty
64+ tys :: [((ProperName , [ProperName ]), ModuleName )]
65+ tys = maybe [] exportedTypes $ envModuleExports <$> mn `M.lookup` env
66+
67+ matchName :: (ProperName -> Maybe ProperName ) -> Maybe ModuleName -> Name -> Maybe String
68+ matchName _ qual (IdentName (Qualified q x)) | q == qual = Just $ showIdent x
69+ matchName _ qual (IsProperName (Qualified q x)) | q == qual = Just $ runProperName x
70+ matchName lookupDc qual (DctorName (Qualified q x)) | q == qual = runProperName <$> lookupDc x
71+ matchName _ _ _ = Nothing
6272
6373runDeclRef :: DeclarationRef -> Maybe String
6474runDeclRef (PositionedDeclarationRef _ _ ref) = runDeclRef ref
65- runDeclRef (ValueRef ident) = Just $ runIdent ident
75+ runDeclRef (ValueRef ident) = Just $ showIdent ident
6676runDeclRef (TypeRef pn _) = Just $ runProperName pn
6777runDeclRef _ = Nothing
6878
0 commit comments