diff --git a/data/config.yml b/data/config.yml index f573c4c..c1b59f4 100644 --- a/data/config.yml +++ b/data/config.yml @@ -1,12 +1,5 @@ - name: Rails allowedTerms: - # migrations - - up - - down - - change - - index - # i18n - - t # serialization - as_json # inflection @@ -22,6 +15,18 @@ 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 @@ -47,9 +52,7 @@ termEndsWith: Test classOrModule: true - name: Haskell - allowedTerms: - - instance - - spec + allowedTerms: [] autoLowLikelihood: - name: Spec pathStartsWith: test/ @@ -58,3 +61,9 @@ - name: Cabalfile pathEndsWith: .cabal appOccurrences: 1 + - name: TypeClasses + termEquals: instance + pathEndsWith: .hs + - name: Spec functions + termEquals: spec + pathStartsWith: test/ diff --git a/src/Unused/ResponseFilter.hs b/src/Unused/ResponseFilter.hs index 2c682fd..c7fce37 100644 --- a/src/Unused/ResponseFilter.hs +++ b/src/Unused/ResponseFilter.hs @@ -57,10 +57,12 @@ matcherToBool :: Matcher -> TermResults -> Bool matcherToBool (Path p v) = any (positionToRegex p v) . paths matcherToBool (Term p v) = positionToRegex p v . trTerm matcherToBool (AppOccurrences i) = (== i) . appOccurrenceCount +matcherToBool (AllowedTerms ts) = flip isAllowedTerm ts positionToRegex :: Position -> (String -> String -> Bool) positionToRegex StartsWith = \v -> matchRegex ("^" ++ v) positionToRegex EndsWith = \v -> matchRegex (v ++ "$") +positionToRegex Equals = (==) paths :: TermResults -> [String] paths r = tmPath <$> trMatches r diff --git a/src/Unused/ResultsClassifier/Types.hs b/src/Unused/ResultsClassifier/Types.hs index fa2edd2..c0e939b 100644 --- a/src/Unused/ResultsClassifier/Types.hs +++ b/src/Unused/ResultsClassifier/Types.hs @@ -28,8 +28,8 @@ data LowLikelihoodMatch = LowLikelihoodMatch , smClassOrModule :: Bool } deriving Show -data Position = StartsWith | EndsWith deriving Show -data Matcher = Term Position String | Path Position String | AppOccurrences Int deriving Show +data Position = StartsWith | EndsWith | Equals deriving Show +data Matcher = Term Position String | Path Position String | AppOccurrences Int | AllowedTerms [String] deriving Show instance FromJSON LanguageConfiguration where parseJSON (Y.Object o) = LanguageConfiguration @@ -61,7 +61,7 @@ intHandler = MatchHandler stringHandler :: MatchHandler String stringHandler = MatchHandler - { mhKeys = ["pathStartsWith", "pathEndsWith", "termStartsWith", "termEndsWith"] + { mhKeys = ["pathStartsWith", "pathEndsWith", "termStartsWith", "termEndsWith", "termEquals"] , mhKeyToMatcher = keyToMatcher } where @@ -69,11 +69,21 @@ stringHandler = MatchHandler keyToMatcher "pathEndsWith" = Right $ Path EndsWith keyToMatcher "termStartsWith" = Right $ Term StartsWith keyToMatcher "termEndsWith" = Right $ Term EndsWith + keyToMatcher "termEquals" = Right $ Term Equals keyToMatcher t = Left t +stringListHandler :: MatchHandler [String] +stringListHandler = MatchHandler + { mhKeys = ["allowedTerms"] + , mhKeyToMatcher = keyToMatcher + } + where + keyToMatcher "allowedTerms" = Right $ AllowedTerms + keyToMatcher t = Left t + parseMatchers :: Y.Object -> Y.Parser [Matcher] parseMatchers o = - myFold (++) [buildMatcherList o intHandler, buildMatcherList o stringHandler] + myFold (++) [buildMatcherList o intHandler, buildMatcherList o stringHandler, buildMatcherList o stringListHandler] where myFold :: (Foldable t, Monad m) => (a -> a -> a) -> t (m a) -> m a myFold f = foldl1 (\acc i -> acc >>= (\l -> f l <$> i)) diff --git a/test/Unused/LikelihoodCalculatorSpec.hs b/test/Unused/LikelihoodCalculatorSpec.hs index e13d91c..af65456 100644 --- a/test/Unused/LikelihoodCalculatorSpec.hs +++ b/test/Unused/LikelihoodCalculatorSpec.hs @@ -51,6 +51,11 @@ spec = parallel $ removalLikelihood matches `shouldReturn` Medium + it "doesn't mis-categorize allowed terms from different languages" $ do + let matches = [ TermMatch "t" "web/models/foo.ex" 1 ] + + removalLikelihood matches `shouldReturn` High + removalLikelihood :: [TermMatch] -> IO RemovalLikelihood removalLikelihood ms = do (Right config) <- loadConfig