Move allowedTerms into auto low-likelihood classification

Why?
====

This ensures no method/function bleed between languages, which may cause
confusing miscalculation when methods/functions are reused across
different types of projects (e.g. index from Rails migrations and index
action from Phoenix controllers).
This commit is contained in:
Joshua Clayton 2016-05-27 14:39:17 -04:00
parent 6eb2e38882
commit 965cc0a178
4 changed files with 40 additions and 14 deletions

View File

@ -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/

View File

@ -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

View File

@ -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))

View File

@ -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