diff --git a/src/Weeder.hs b/src/Weeder.hs index 5ac5aec..f80e1b2 100644 --- a/src/Weeder.hs +++ b/src/Weeder.hs @@ -22,6 +22,7 @@ module Weeder -- * Declarations , Declaration(..) + , declarationMatchesRegularExpression ) where @@ -65,7 +66,7 @@ import HieTypes , NodeInfo( NodeInfo, nodeIdentifiers, nodeAnnotations ) , Scope( ModuleScope ) ) -import Module ( Module, moduleStableString ) +import Module ( Module, moduleName, moduleNameString, moduleStableString ) import Name ( Name, nameModule_maybe, nameOccName ) import OccName ( OccName @@ -84,6 +85,9 @@ import Control.Lens ( (%=) ) -- mtl import Control.Monad.State.Class ( MonadState ) +-- regex-tdfa +import Text.Regex.TDFA ( (=~) ) + -- transformers import Control.Monad.Trans.Maybe ( runMaybeT ) @@ -119,6 +123,11 @@ declarationStableName Declaration { declModule, declOccName } = intercalate "$" [ namespace, moduleStableString declModule, "$", occNameString declOccName ] +declarationMatchesRegularExpression :: Declaration -> String -> Bool +declarationMatchesRegularExpression d p = + ( moduleNameString ( moduleName ( declModule d ) ) <> "." <> occNameString ( declOccName d ) ) =~ p + + -- | All information maintained by 'analyseHieFile'. data Analysis = Analysis diff --git a/src/Weeder/Config.hs b/src/Weeder/Config.hs index b9a82f9..e697508 100644 --- a/src/Weeder/Config.hs +++ b/src/Weeder/Config.hs @@ -22,6 +22,10 @@ data Config = Config -- ^ If True, consider all declarations in a type class as part of the root -- set. Weeder is currently unable to identify whether or not a type class -- instance is used - enabling this option can prevent false positives. + , ignore :: Set String + -- ^ Any weeds matching these regular expressions will not be reported. This + -- is different from 'rootPatterns', which causes the weed to become a root, + -- while this option is purely about filtering the reporting output. } @@ -31,7 +35,8 @@ data Config = Config config :: Dhall.Decoder Config config = Dhall.record do - rootPatterns <- Set.fromList <$> Dhall.field "roots" ( Dhall.list Dhall.string ) + rootPatterns <- Set.fromList <$> Dhall.field "roots" ( Dhall.list Dhall.string ) typeClassRoots <- Dhall.field "type-class-roots" Dhall.bool + ignore <- Set.fromList <$> Dhall.field "ignore" ( Dhall.list Dhall.string ) return Config{..} diff --git a/src/Weeder/Main.hs b/src/Weeder/Main.hs index 7bebe29..508451d 100644 --- a/src/Weeder/Main.hs +++ b/src/Weeder/Main.hs @@ -46,9 +46,6 @@ import OccName ( occNameString ) import SrcLoc ( realSrcSpanStart, srcLocCol, srcLocLine ) import UniqSupply ( mkSplitUniqSupply ) --- regex-tdfa -import Text.Regex.TDFA ( (=~) ) - -- optparse-applicative import Options.Applicative @@ -94,7 +91,7 @@ main = do -- This will recursively find all @.hie@ files in the current directory, perform -- analysis, and report all unused definitions according to the 'Config'. mainWithConfig :: [FilePath] -> Config -> IO () -mainWithConfig hieDirectories Config{ rootPatterns, typeClassRoots } = do +mainWithConfig hieDirectories Config{ rootPatterns, typeClassRoots, ignore } = do hieFilePaths <- concat <$> traverse getHieFilesIn @@ -116,11 +113,7 @@ mainWithConfig hieDirectories Config{ rootPatterns, typeClassRoots } = do let roots = Set.filter - ( \d -> - any - ( ( moduleNameString ( moduleName ( declModule d ) ) <> "." <> occNameString ( declOccName d ) ) =~ ) - rootPatterns - ) + ( \d -> any ( declarationMatchesRegularExpression d ) rootPatterns ) ( allDeclarations analysis ) reachableSet = @@ -142,6 +135,8 @@ mainWithConfig hieDirectories Config{ rootPatterns, typeClassRoots } = do spans <- Map.lookup d ( declarationSites analysis ) guard $ not $ null spans + guard $ all (not . declarationMatchesRegularExpression d) ignore + let snippets = do srcSpan <- Set.toList spans