Skip to content

Commit

Permalink
Count file and global errors separately
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Oct 30, 2024
1 parent cc29cc2 commit c834a86
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 20 deletions.
77 changes: 60 additions & 17 deletions template/src/Elm/Review/Reporter.elm
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,10 @@ formatReport :
-> List TextContent
formatReport { suppressedErrors, unsuppressMode, originalNumberOfSuppressedErrors, detailsMode, mode, errorsHaveBeenFixedPreviously } files =
let
numberOfErrors : Int
numberOfErrors =
totalNumberOfErrors files
{ numberOfFileErrors, numberOfGlobalErrors } =
countErrors files
in
if numberOfErrors == 0 then
if numberOfFileErrors + numberOfGlobalErrors == 0 then
formatNoErrors suppressedErrors originalNumberOfSuppressedErrors errorsHaveBeenFixedPreviously

else
Expand Down Expand Up @@ -189,20 +188,50 @@ formatReport { suppressedErrors, unsuppressMode, originalNumberOfSuppressedError

else
Nothing
, [ Text.from "I found "
, pluralize numberOfErrors "error" |> Text.from |> Text.inRed
, Text.from " in "
, pluralize (List.length filesWithErrors) "file" |> Text.from |> Text.inYellow
, Text.from "."
]
|> Just
, Just (formatTally filesWithErrors numberOfFileErrors numberOfGlobalErrors)
]
|> List.filterMap identity
|> Text.join "\n\n"
|> Text.simplify
|> List.map Text.toRecord


formatTally : List a -> Int -> Int -> List Text
formatTally filesWithErrors numberOfFileErrors numberOfGlobalErrors =
Text.join ""
[ [ Text.from "I found " ]
, [ if numberOfFileErrors > 0 then
let
numberOfFilesWithErrors : Int
numberOfFilesWithErrors =
if numberOfGlobalErrors > 0 then
List.length filesWithErrors - 1

else
List.length filesWithErrors
in
Just
[ pluralize numberOfFileErrors "error" |> Text.from |> Text.inRed
, Text.from " in "
, pluralize numberOfFilesWithErrors "file" |> Text.from |> Text.inYellow
]

else
Nothing
, if numberOfGlobalErrors > 0 then
Just
[ pluralize numberOfGlobalErrors "global error" |> Text.from |> Text.inRed
]

else
Nothing
]
|> List.filterMap identity
|> Text.join " and "
, [ Text.from "." ]
]


classifyFixes : List Error -> { rulesWithInvalidFixes : Set String, hasIgnoredFixableErrors : Bool }
classifyFixes errors =
classifyFixesHelp
Expand Down Expand Up @@ -753,19 +782,33 @@ underline gutterLength { start, end, lineContent } =
]


totalNumberOfErrors : List FileWithError -> Int
totalNumberOfErrors files =
totalNumberOfErrorsHelp files 0
countErrors : List FileWithError -> { numberOfFileErrors : Int, numberOfGlobalErrors : Int }
countErrors files =
countErrorsHelp files { numberOfFileErrors = 0, numberOfGlobalErrors = 0 }


totalNumberOfErrorsHelp : List FileWithError -> Int -> Int
totalNumberOfErrorsHelp files acc =
countErrorsHelp : List FileWithError -> { numberOfFileErrors : Int, numberOfGlobalErrors : Int } -> { numberOfFileErrors : Int, numberOfGlobalErrors : Int }
countErrorsHelp files acc =
case files of
[] ->
acc

file :: xs ->
totalNumberOfErrorsHelp xs (acc + List.length file.errors)
case file.path of
FilePath _ ->
countErrorsHelp xs
{ numberOfFileErrors = acc.numberOfFileErrors + List.length file.errors
, numberOfGlobalErrors = acc.numberOfGlobalErrors
}

Global ->
countErrorsHelp xs
{ numberOfFileErrors = acc.numberOfFileErrors
, numberOfGlobalErrors = acc.numberOfGlobalErrors + List.length file.errors
}

ConfigurationError ->
countErrorsHelp xs acc


fixableErrors : List FileWithError -> List Error
Expand Down
108 changes: 105 additions & 3 deletions template/tests/ReporterTest.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module ReporterTest exposing (suite)
module ReporterTest exposing (multipleErrorsIncludingGlobalErrorTest, suite)

import Elm.Review.Reporter as Reporter
import Elm.Review.SuppressedErrors as SuppressedErrors exposing (SuppressedErrors)
Expand Down Expand Up @@ -815,12 +815,114 @@ globalErrorTest =
NoDebug: Do not use Debug
I found 1 error in 1 file."""
I found 1 global error."""
, withColors = """[-- ELM-REVIEW ERROR ----------------------------------------------- GLOBAL ERROR](#33BBC8)
[NoDebug](#FF0000): Do not use Debug
I found [1 error](#FF0000) in [1 file](#E8C338)."""
I found [1 global error](#FF0000)."""
}


multipleErrorsIncludingGlobalErrorTest : Test
multipleErrorsIncludingGlobalErrorTest =
test "report a global error that has no source code" <|
\() ->
[ { path = Reporter.Global
, source = Reporter.Source ""
, errors =
[ { ruleName = "NoDebug"
, ruleLink = Just "https://package.elm-lang.org/packages/author/package/1.0.0/NoDebug"
, message = "Do not use Debug"
, details =
[ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum cursus erat ullamcorper, commodo leo quis, sollicitudin eros. Sed semper mattis ex, vitae dignissim lectus. Integer eu risus augue. Nam egestas lacus non lacus molestie mattis. Phasellus magna dui, ultrices eu massa nec, interdum tincidunt eros. Aenean rutrum a purus nec cursus. Integer ullamcorper leo non lectus dictum, in vulputate justo vulputate. Donec ullamcorper finibus quam sed dictum."
, "Donec sed ligula ac mi pretium mattis et in nisi. Nulla nec ex hendrerit, sollicitudin eros at, mattis tortor. Ut lacinia ornare lectus in vestibulum. Nam congue ultricies dolor, in venenatis nulla sagittis nec. In ac leo sit amet diam iaculis ornare eu non odio. Proin sed orci et urna tincidunt tincidunt quis a lacus. Donec euismod odio nulla, sit amet iaculis lorem interdum sollicitudin. Vivamus bibendum quam urna, in tristique lacus iaculis id. In tempor lectus ipsum, vehicula bibendum magna pretium vitae. Cras ullamcorper rutrum nunc non sollicitudin. Curabitur tempus eleifend nunc, sed ornare nisl tincidunt vel. Maecenas eu nisl ligula."
]
, range =
{ start = { row = 0, column = 0 }
, end = { row = 0, column = 0 }
}
, providesFix = False
, fixFailure = Nothing
, suppressed = False
}
]
}
, { path = Reporter.FilePath "src/FileA.elm"
, source = Reporter.Source """module FileA exposing (a)
a = Debug.log "debug" 1"""
, errors =
[ { ruleName = "NoDebug"
, ruleLink = Just "https://package.elm-lang.org/packages/author/package/1.0.0/NoDebug"
, message = "Do not use Debug"
, details =
[ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum cursus erat ullamcorper, commodo leo quis, sollicitudin eros. Sed semper mattis ex, vitae dignissim lectus. Integer eu risus augue. Nam egestas lacus non lacus molestie mattis. Phasellus magna dui, ultrices eu massa nec, interdum tincidunt eros. Aenean rutrum a purus nec cursus. Integer ullamcorper leo non lectus dictum, in vulputate justo vulputate. Donec ullamcorper finibus quam sed dictum."
, "Donec sed ligula ac mi pretium mattis et in nisi. Nulla nec ex hendrerit, sollicitudin eros at, mattis tortor. Ut lacinia ornare lectus in vestibulum. Nam congue ultricies dolor, in venenatis nulla sagittis nec. In ac leo sit amet diam iaculis ornare eu non odio. Proin sed orci et urna tincidunt tincidunt quis a lacus. Donec euismod odio nulla, sit amet iaculis lorem interdum sollicitudin. Vivamus bibendum quam urna, in tristique lacus iaculis id. In tempor lectus ipsum, vehicula bibendum magna pretium vitae. Cras ullamcorper rutrum nunc non sollicitudin. Curabitur tempus eleifend nunc, sed ornare nisl tincidunt vel. Maecenas eu nisl ligula."
]
, range =
{ start = { row = 2, column = 5 }
, end = { row = 2, column = 10 }
}
, providesFix = True
, fixFailure = Nothing
, suppressed = True
}
]
}
]
|> Reporter.formatReport
{ suppressedErrors = SuppressedErrors.empty
, unsuppressMode = UnsuppressMode.UnsuppressNone
, originalNumberOfSuppressedErrors = 0
, detailsMode = Reporter.WithoutDetails
, mode = Reporter.Reviewing
, errorsHaveBeenFixedPreviously = False
}
|> expect
{ withoutColors = """-- ELM-REVIEW ERROR ----------------------------------------------- GLOBAL ERROR
NoDebug: Do not use Debug
====o======================================================================o====
↓ src/FileA.elm
-- ELM-REVIEW ERROR ------------------------------------------ src/FileA.elm:2:5
(unsuppressed) (fix) NoDebug: Do not use Debug
1| module FileA exposing (a)
2| a = Debug.log "debug" 1
^^^^^
Errors marked with (unsuppressed) were previously suppressed, but you introduced new errors for the same rule and file. There are now more of those than what I previously allowed. Please fix them until you have at most as many errors as before. Maybe fix a few more while you're there?
Errors marked with (fix) can be fixed automatically using `elm-review --fix`.
I found 1 error in 1 file and 1 global error."""
, withColors = """[-- ELM-REVIEW ERROR ----------------------------------------------- GLOBAL ERROR](#33BBC8)
[NoDebug](#FF0000): Do not use Debug
[ ↑
====o======================================================================o====
↓ src/FileA.elm](#FF0000)
[-- ELM-REVIEW ERROR ------------------------------------------ src/FileA.elm:2:5](#33BBC8)
[(unsuppressed) ](#FFA500)[(fix) ](#33BBC8)[NoDebug](#FF0000): Do not use Debug
1| module FileA exposing (a)
2| a = Debug.log "debug" 1
[^^^^^](#FF0000)
[Errors marked with (unsuppressed) were previously suppressed, but you introduced new errors for the same rule and file. There are now more of those than what I previously allowed. Please fix them until you have at most as many errors as before. Maybe fix a few more while you're there?](#FFA500)
[Errors marked with (fix) can be fixed automatically using `elm-review --fix`.](#33BBC8)
I found [1 error](#FF0000) in [1 file](#E8C338) and [1 global error](#FF0000)."""
}


Expand Down

0 comments on commit c834a86

Please sign in to comment.