From 5a384b87fca6f4e7a93d9bfdf647fb4d68320276 Mon Sep 17 00:00:00 2001 From: Ollie Charles Date: Fri, 11 Dec 2020 10:38:32 +0000 Subject: [PATCH 1/2] Add the ability to ignore weeds Fixes #56 --- src/Weeder.hs | 11 ++++++++++- src/Weeder/Config.hs | 7 ++++++- src/Weeder/Main.hs | 15 +++++---------- 3 files changed, 21 insertions(+), 12 deletions(-) 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..6ef190a 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 @@ -155,7 +150,7 @@ mainWithConfig hieDirectories Config{ rootPatterns, typeClassRoots } = do dead for_ ( Map.toList warnings ) \( path, declarations ) -> - for_ declarations \( ( start, snippet ), d ) -> do + for_ (declarations) \( ( start, snippet ), d ) -> do putStrLn $ unwords [ foldMap ( <> ":" ) [ path, show ( srcLocLine start ), show ( srcLocCol start ) ] From f128ee0b409e8530edf6eaef4354fe3a0ae05a4d Mon Sep 17 00:00:00 2001 From: Ollie Charles Date: Thu, 7 Jan 2021 22:09:03 +0000 Subject: [PATCH 2/2] Update Main.hs --- src/Weeder/Main.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Weeder/Main.hs b/src/Weeder/Main.hs index 6ef190a..508451d 100644 --- a/src/Weeder/Main.hs +++ b/src/Weeder/Main.hs @@ -150,7 +150,7 @@ mainWithConfig hieDirectories Config{ rootPatterns, typeClassRoots, ignore } = d dead for_ ( Map.toList warnings ) \( path, declarations ) -> - for_ (declarations) \( ( start, snippet ), d ) -> do + for_ declarations \( ( start, snippet ), d ) -> do putStrLn $ unwords [ foldMap ( <> ":" ) [ path, show ( srcLocLine start ), show ( srcLocCol start ) ]