Support excluding folders

This commit is contained in:
Jeroen Engels 2024-04-07 09:46:07 +02:00
parent 56e75c931f
commit abb2dce7a7
2 changed files with 38 additions and 2 deletions

View File

@ -1,6 +1,7 @@
module Review.FilePattern exposing (FilePattern, exclude, excludeFolder, include, match)
import Glob exposing (Glob)
import Review.Rule exposing (globalError)
type FilePattern
@ -32,7 +33,7 @@ exclude globStr =
excludeFolder : String -> FilePattern
excludeFolder globStr =
case Glob.fromString globStr of
case Glob.fromString (toFolder globStr) of
Ok glob ->
ExcludeFolder glob
@ -40,6 +41,15 @@ excludeFolder globStr =
InvalidGlob globStr
toFolder : String -> String
toFolder globStr =
if String.endsWith "/" globStr then
globStr ++ "**/*"
else
globStr ++ "/**/*"
match : List FilePattern -> String -> Bool
match filePatterns str =
matchHelp filePatterns str False
@ -52,7 +62,17 @@ matchHelp filePatterns str acc =
acc
(Include glob) :: rest ->
matchHelp rest str (Glob.match glob str)
matchHelp rest str (acc || Glob.match glob str)
(Exclude glob) :: rest ->
matchHelp rest str (acc && not (Glob.match glob str))
(ExcludeFolder glob) :: rest ->
if Glob.match glob str then
False
else
matchHelp rest str acc
_ ->
False

View File

@ -55,5 +55,21 @@ all =
, FilePattern.include "some/file/path.ext"
]
"some/file/path.ext"
|> Expect.equal True
, test "should return False when excluding the folder even when re-including the target file" <|
\() ->
FilePattern.match
[ FilePattern.excludeFolder "some"
, FilePattern.include "some/file/path.ext"
]
"some/file/path.ext"
|> Expect.equal False
, test "should return False when excluding the folder (with trailing /) even when re-including the target file" <|
\() ->
FilePattern.match
[ FilePattern.excludeFolder "some/"
, FilePattern.include "some/file/path.ext"
]
"some/file/path.ext"
|> Expect.equal False
]