Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to ignore weeds #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Weeder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Weeder

-- * Declarations
, Declaration(..)
, declarationMatchesRegularExpression
)
where

Expand Down Expand Up @@ -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
Expand All @@ -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 )

Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/Weeder/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}


Expand All @@ -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{..}
13 changes: 4 additions & 9 deletions src/Weeder/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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 =
Expand All @@ -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

Expand Down