diff --git a/src/Review/FilePattern.elm b/src/Review/FilePattern.elm index ad2accc4..512e0cb4 100644 --- a/src/Review/FilePattern.elm +++ b/src/Review/FilePattern.elm @@ -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 diff --git a/tests/Review/FilePatternTest.elm b/tests/Review/FilePatternTest.elm index adb5a40c..8afdf4f9 100644 --- a/tests/Review/FilePatternTest.elm +++ b/tests/Review/FilePatternTest.elm @@ -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 ]