unused 0.6.0.0 → 0.6.1.0
raw patch · 10 files changed
+72/−147 lines, 10 filesdep +file-embed
Dependencies added: file-embed
Files
- app/Main.hs +1/−0
- data/config.yml +0/−86
- src/Common.hs +9/−0
- src/Unused/Projection.hs +4/−3
- src/Unused/ResultsClassifier/Config.hs +10/−9
- src/Unused/ResultsClassifier/Types.hs +1/−1
- test/Unused/LikelihoodCalculatorSpec.hs +12/−11
- test/Unused/ParserSpec.hs +3/−3
- test/Unused/ResponseFilterSpec.hs +29/−30
- unused.cabal +3/−4
app/Main.hs view
@@ -1,6 +1,7 @@ module Main where import App (runProgram, Options(Options))+import Common import qualified Data.Maybe as M import Options.Applicative import Unused.CLI (SearchRunner(..))
− data/config.yml
@@ -1,86 +0,0 @@-- name: Rails- aliases:- - from: "*?"- to: "be_{}"- - from: "has_*?"- to: "have_{}"- - from: "*Validator"- to: "{snakecase}"- allowedTerms:- # serialization- - as_json- # inflection- - Inflector- # Concerns- - ClassMethods- - class_methods- - included- # rendering- - to_partial_path- autoLowLikelihood:- - name: Migration- pathStartsWith: db/migrate/- classOrModule: true- appOccurrences: 1- - name: Migration Helper- pathStartsWith: db/migrate/- allowedTerms:- - up- - down- - change- - index- - name: i18n- allowedTerms:- - t- - l- pathEndsWith: .rb- - name: Controller- pathStartsWith: app/controllers- termEndsWith: Controller- classOrModule: true- - name: Helper- pathStartsWith: app/helpers- termEndsWith: Helper- classOrModule: true-- name: Phoenix- allowedTerms:- - Mixfile- - __using__- autoLowLikelihood:- - name: Migration- pathStartsWith: priv/repo/migrations- classOrModule: true- - name: View- pathStartsWith: web/views/- termEndsWith: View- classOrModule: true- - name: Test- pathStartsWith: test/- termEndsWith: Test- classOrModule: true- - name: Controller actions- pathStartsWith: web/controllers- allowedTerms:- - index- - new- - create- - show- - edit- - update- - destroy-- name: Haskell- allowedTerms: []- autoLowLikelihood:- - name: Spec- pathStartsWith: test/- termEndsWith: Spec- classOrModule: true- - name: Cabalfile- pathEndsWith: .cabal- appOccurrences: 1- - name: TypeClasses- termEquals: instance- pathEndsWith: .hs- - name: Spec functions- termEquals: spec- pathStartsWith: test/
+ src/Common.hs view
@@ -0,0 +1,9 @@+{-# LANGUAGE CPP #-}++module Common+ ( (<>)+ ) where++#if MIN_VERSION_base(4, 8, 0)+import Data.Monoid ((<>))+#endif
src/Unused/Projection.hs view
@@ -2,6 +2,7 @@ module Unused.Projection where +import qualified Data.Bifunctor as BF import Data.Monoid ((<>)) import Data.Text (Text) import qualified Data.Text as T@@ -15,7 +16,7 @@ , ptPost :: Text } -translate :: Text -> Either ParseError (Text -> Text)+translate :: Text -> Either String (Text -> Text) translate template = applyTransform <$> parseTransform template applyTransform :: ParsedTransform -> Text -> Text@@ -24,8 +25,8 @@ <> runTransformations t (ptTransforms pt) <> ptPost pt -parseTransform :: Text -> Either ParseError ParsedTransform-parseTransform = parse parsedTransformParser ""+parseTransform :: Text -> Either String ParsedTransform+parseTransform = BF.first show . parse parsedTransformParser "" parsedTransformParser :: Parser ParsedTransform parsedTransformParser =
src/Unused/ResultsClassifier/Config.hs view
@@ -1,31 +1,32 @@+{-# LANGUAGE TemplateHaskell #-}+ module Unused.ResultsClassifier.Config ( loadConfig , loadAllConfigurations ) where import qualified Data.Bifunctor as BF+import qualified Data.ByteString as BS import qualified Data.Either as E+import qualified Data.FileEmbed as FE import qualified Data.Yaml as Y-import qualified Paths_unused as Paths import qualified System.Directory as D import System.FilePath ((</>)) import Unused.ResultsClassifier.Types (LanguageConfiguration, ParseConfigError(..)) import Unused.Util (safeReadFile) -loadConfig :: IO (Either String [LanguageConfiguration])-loadConfig = do- configFileName <- Paths.getDataFileName ("data" </> "config.yml")+loadConfig :: Either String [LanguageConfiguration]+loadConfig = Y.decodeEither defaultConfigFile - either- (const $ Left "default config not found")- Y.decodeEither- <$> safeReadFile configFileName+defaultConfigFile :: BS.ByteString+defaultConfigFile = $(FE.embedFile "data/config.yml") loadAllConfigurations :: IO (Either [ParseConfigError] [LanguageConfiguration]) loadAllConfigurations = do homeDir <- D.getHomeDirectory - defaultConfig <- addSourceToLeft "default config" <$> loadConfig+ let defaultConfig = addSourceToLeft "default config" loadConfig+ localConfig <- loadConfigFromFile ".unused.yml" userConfig <- loadConfigFromFile $ homeDir </> ".unused.yml"
src/Unused/ResultsClassifier/Types.hs view
@@ -66,7 +66,7 @@ parseJSON (Y.Object o) = TermAlias <$> o .: "from" <*> o .: "to"- <*> (either (fail . show) return =<< (translate . T.pack <$> (o .: "to")))+ <*> (either fail return =<< (translate . T.pack <$> (o .: "to"))) parseJSON _ = M.mzero data MatchHandler a = MatchHandler
test/Unused/LikelihoodCalculatorSpec.hs view
@@ -16,10 +16,10 @@ describe "calculateLikelihood" $ do it "prefers language-specific checks first" $ do let railsMatches = [ TermMatch "ApplicationController" "app/controllers/application_controller.rb" Nothing 1 ]- removalLikelihood' railsMatches `shouldReturn` Low+ removalLikelihood' railsMatches `shouldBe` Low let elixirMatches = [ TermMatch "AwesomeView" "web/views/awesome_view.ex" Nothing 1 ]- removalLikelihood' elixirMatches `shouldReturn` Low+ removalLikelihood' elixirMatches `shouldBe` Low it "weighs widely-used methods as low likelihood" $ do let matches = [ TermMatch "full_name" "app/models/user.rb" Nothing 4@@ -28,19 +28,19 @@ , TermMatch "full_name" "spec/models/user_spec.rb" Nothing 10 ] - removalLikelihood' matches `shouldReturn` Low+ removalLikelihood' matches `shouldBe` Low it "weighs only-occurs-once methods as high likelihood" $ do let matches = [ TermMatch "obscure_method" "app/models/user.rb" Nothing 1 ] - removalLikelihood' matches `shouldReturn` High+ removalLikelihood' matches `shouldBe` High it "weighs methods that seem to only be tested and never used as high likelihood" $ do let matches = [ TermMatch "obscure_method" "app/models/user.rb" Nothing 1 , TermMatch "obscure_method" "spec/models/user_spec.rb" Nothing 5 ] - removalLikelihood' matches `shouldReturn` High+ removalLikelihood' matches `shouldBe` High it "weighs methods that seem to only be tested and used in one other area as medium likelihood" $ do let matches = [ TermMatch "obscure_method" "app/models/user.rb" Nothing 1@@ -49,14 +49,15 @@ , TermMatch "obscure_method" "spec/controllers/user_controller_spec.rb" Nothing 5 ] - removalLikelihood' matches `shouldReturn` Medium+ removalLikelihood' matches `shouldBe` Medium it "doesn't mis-categorize allowed terms from different languages" $ do let matches = [ TermMatch "t" "web/models/foo.ex" Nothing 1 ] - removalLikelihood' matches `shouldReturn` High+ removalLikelihood' matches `shouldBe` High -removalLikelihood' :: [TermMatch] -> IO RemovalLikelihood-removalLikelihood' ms = do- (Right config) <- loadConfig- return $ rLikelihood $ trRemoval $ calculateLikelihood config $ resultsFromMatches ms+removalLikelihood' :: [TermMatch] -> RemovalLikelihood+removalLikelihood' =+ rLikelihood . trRemoval . calculateLikelihood config . resultsFromMatches+ where+ (Right config) = loadConfig
test/Unused/ParserSpec.hs view
@@ -23,7 +23,7 @@ let r2Matches = [ TermMatch "other" "app/path/other.rb" Nothing 1 ] let r2Results = TermResults "other" ["other"] r2Matches (Occurrences 0 0) (Occurrences 1 1) (Occurrences 1 1) (Removal High "occurs once") Nothing - (Right config) <- loadConfig+ let (Right config) = loadConfig let result = parseResults config $ SearchResults $ r1Matches ++ r2Matches @@ -50,7 +50,7 @@ ] - (Right config) <- loadConfig+ let (Right config) = loadConfig let searchResults = r1Matches ++ r2Matches let result = parseResults config $ SearchResults searchResults@@ -60,6 +60,6 @@ Map.fromList [ ("admin?|be_admin", results) ] it "handles empty input" $ do- (Right config) <- loadConfig+ let (Right config) = loadConfig let result = parseResults config $ SearchResults [] result `shouldBe` Map.fromList []
test/Unused/ResponseFilterSpec.hs view
@@ -19,114 +19,114 @@ let match = TermMatch "ApplicationController" "app/controllers/application_controller.rb" Nothing 1 let result = resultsFromMatches [match] - railsAutoLowLikelihood result `shouldReturn` True+ railsAutoLowLikelihood result `shouldBe` True it "allows helpers" $ do let match = TermMatch "ApplicationHelper" "app/helpers/application_helper.rb" Nothing 1 let result = resultsFromMatches [match] - railsAutoLowLikelihood result `shouldReturn` True+ railsAutoLowLikelihood result `shouldBe` True it "allows migrations" $ do let match = TermMatch "CreateUsers" "db/migrate/20160101120000_create_users.rb" Nothing 1 let result = resultsFromMatches [match] - railsAutoLowLikelihood result `shouldReturn` True+ railsAutoLowLikelihood result `shouldBe` True it "disallows service objects" $ do let match = TermMatch "CreatePostWithNotifications" "app/services/create_post_with_notifications.rb" Nothing 1 let result = resultsFromMatches [match] - railsAutoLowLikelihood result `shouldReturn` False+ railsAutoLowLikelihood result `shouldBe` False it "disallows methods" $ do let match = TermMatch "my_method" "app/services/create_post_with_notifications.rb" Nothing 1 let result = resultsFromMatches [match] - railsAutoLowLikelihood result `shouldReturn` False+ railsAutoLowLikelihood result `shouldBe` False it "disallows models that occur in migrations" $ do let model = TermMatch "User" "app/models/user.rb" Nothing 1 let migration = TermMatch "User" "db/migrate/20160101120000_create_users.rb" Nothing 1 let result = resultsFromMatches [model, migration] - railsAutoLowLikelihood result `shouldReturn` False+ railsAutoLowLikelihood result `shouldBe` False it "allows matches intermixed with other results" $ do let appToken = TermMatch "ApplicationHelper" "app/helpers/application_helper.rb" Nothing 1 let testToken = TermMatch "ApplicationHelper" "spec/helpers/application_helper_spec.rb" Nothing 10 let result = resultsFromMatches [appToken, testToken] - railsAutoLowLikelihood result `shouldReturn` True+ railsAutoLowLikelihood result `shouldBe` True describe "elixirAutoLowLikelihood" $ do it "disallows controllers" $ do let match = TermMatch "PageController" "web/controllers/page_controller.rb" Nothing 1 let result = resultsFromMatches [match] - elixirAutoLowLikelihood result `shouldReturn` False+ elixirAutoLowLikelihood result `shouldBe` False it "allows views" $ do let match = TermMatch "PageView" "web/views/page_view.rb" Nothing 1 let result = resultsFromMatches [match] - elixirAutoLowLikelihood result `shouldReturn` True+ elixirAutoLowLikelihood result `shouldBe` True it "allows migrations" $ do let match = TermMatch "CreateUsers" "priv/repo/migrations/20160101120000_create_users.exs" Nothing 1 let result = resultsFromMatches [match] - elixirAutoLowLikelihood result `shouldReturn` True+ elixirAutoLowLikelihood result `shouldBe` True it "allows tests" $ do let match = TermMatch "UserTest" "test/models/user_test.exs" Nothing 1 let result = resultsFromMatches [match] - elixirAutoLowLikelihood result `shouldReturn` True+ elixirAutoLowLikelihood result `shouldBe` True it "allows Mixfile" $ do let match = TermMatch "Mixfile" "mix.exs" Nothing 1 let result = resultsFromMatches [match] - elixirAutoLowLikelihood result `shouldReturn` True+ elixirAutoLowLikelihood result `shouldBe` True it "allows __using__" $ do let match = TermMatch "__using__" "web/web.ex" Nothing 1 let result = resultsFromMatches [match] - elixirAutoLowLikelihood result `shouldReturn` True+ elixirAutoLowLikelihood result `shouldBe` True it "disallows service modules" $ do let match = TermMatch "CreatePostWithNotifications" "web/services/create_post_with_notifications.ex" Nothing 1 let result = resultsFromMatches [match] - elixirAutoLowLikelihood result `shouldReturn` False+ elixirAutoLowLikelihood result `shouldBe` False it "disallows functions" $ do let match = TermMatch "my_function" "web/services/create_post_with_notifications.ex" Nothing 1 let result = resultsFromMatches [match] - elixirAutoLowLikelihood result `shouldReturn` False+ elixirAutoLowLikelihood result `shouldBe` False it "allows matches intermixed with other results" $ do let appToken = TermMatch "UserView" "web/views/user_view.ex" Nothing 1 let testToken = TermMatch "UserView" "test/views/user_view_test.exs" Nothing 10 let result = resultsFromMatches [appToken, testToken] - elixirAutoLowLikelihood result `shouldReturn` True+ elixirAutoLowLikelihood result `shouldBe` True describe "haskellAutoLowLikelihood" $ do it "allows instance" $ do let match = TermMatch "instance" "src/Lib/Types.hs" Nothing 1 let result = resultsFromMatches [match] - haskellAutoLowLikelihood result `shouldReturn` True+ haskellAutoLowLikelihood result `shouldBe` True it "allows items in the *.cabal file" $ do let match = TermMatch "Lib.SomethingSpec" "lib.cabal" Nothing 1 let result = resultsFromMatches [match] - haskellAutoLowLikelihood result `shouldReturn` True+ haskellAutoLowLikelihood result `shouldBe` True describe "autoLowLikelihood" $ it "doesn't qualify as low when no matchers are present in a language config" $ do@@ -136,18 +136,17 @@ autoLowLikelihood languageConfig result `shouldBe` False -configByName :: String -> IO LanguageConfiguration-configByName s = do- (Right config) <- loadConfig- let (Just config') = find ((==) s . lcName) config-- return config'+configByName :: String -> LanguageConfiguration+configByName s = config'+ where+ (Right config) = loadConfig+ (Just config') = find ((==) s . lcName) config -railsAutoLowLikelihood :: TermResults -> IO Bool-railsAutoLowLikelihood r = (`autoLowLikelihood` r) <$> configByName "Rails"+railsAutoLowLikelihood :: TermResults -> Bool+railsAutoLowLikelihood = autoLowLikelihood (configByName "Rails") -elixirAutoLowLikelihood :: TermResults -> IO Bool-elixirAutoLowLikelihood r = (`autoLowLikelihood` r) <$> configByName "Phoenix"+elixirAutoLowLikelihood :: TermResults -> Bool+elixirAutoLowLikelihood = autoLowLikelihood (configByName "Phoenix") -haskellAutoLowLikelihood :: TermResults -> IO Bool-haskellAutoLowLikelihood r = (`autoLowLikelihood` r) <$> configByName "Haskell"+haskellAutoLowLikelihood :: TermResults -> Bool+haskellAutoLowLikelihood = autoLowLikelihood (configByName "Haskell")
unused.cabal view
@@ -1,5 +1,5 @@ name: unused-version: 0.6.0.0+version: 0.6.1.0 synopsis: A command line tool to identify unused code. description: Please see README.md homepage: https://github.com/joshuaclayton/unused#readme@@ -12,7 +12,6 @@ build-type: Simple -- extra-source-files: cabal-version: >=1.10-data-files: data/config.yml library hs-source-dirs: src@@ -60,7 +59,7 @@ , Unused.CLI.ProgressIndicator , Unused.CLI.ProgressIndicator.Internal , Unused.CLI.ProgressIndicator.Types- other-modules: Paths_unused+ , Common build-depends: base >= 4.7 && < 5 , process , containers@@ -81,6 +80,7 @@ , transformers , megaparsec , inflections+ , file-embed ghc-options: -Wall default-language: Haskell2010 @@ -114,7 +114,6 @@ , Unused.UtilSpec , Unused.Cache.FindArgsFromIgnoredPathsSpec , Unused.AliasesSpec- , Paths_unused ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall default-language: Haskell2010