Skip to content

Count file and global errors separately #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions template/src/Elm/Review/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,11 @@ I recommend you take a look at the following documents:
abortForConfigurationErrors <|
case flags.reportMode of
HumanReadable ->
Reporter.formatReport
{ suppressedErrors = SuppressedErrors.empty
, unsuppressMode = flags.unsuppressMode
, originalNumberOfSuppressedErrors = 0
, detailsMode = flags.detailsMode
, errorsHaveBeenFixedPreviously = False
Reporter.formatConfigurationErrors
{ detailsMode = flags.detailsMode
, mode = Reporter.Reviewing
, configurationErrors = configurationErrors
}
[ { path = Reporter.ConfigurationError
, source = Reporter.Source ""
, errors = configurationErrors
}
]
|> encodeReport

Json ->
Expand Down
115 changes: 88 additions & 27 deletions template/src/Elm/Review/Reporter.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Elm.Review.Reporter exposing
( Error, File, FilePath(..), Source(..), TextContent
, Mode(..), DetailsMode(..), formatReport, formatIndividualError
, formatFixProposal, formatFixProposals
, FileWithError, Range
, FileWithError, Range, formatConfigurationErrors
)

{-| Formats the result of `elm-review` in a nice human-readable way.
Expand Down 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,31 +188,79 @@ 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


classifyFixes : List Error -> { rulesWithInvalidFixes : Set String, hasIgnoredFixableErrors : Bool }
classifyFixes errors =
{-| Reports configuration errors reported by `elm-review` in a nice human-readable way.
-}
formatConfigurationErrors : { detailsMode : DetailsMode, mode : Mode, configurationErrors : List Error } -> List TextContent
formatConfigurationErrors { detailsMode, mode, configurationErrors } =
let
{ rulesWithInvalidFixes, hasIgnoredFixableErrors } =
classifyFixesHelp
errors
{ rulesWithInvalidFixes = Set.empty, hasIgnoredFixableErrors = False }
filesWithErrors : List FileWithError
filesWithErrors =
[ { path = ConfigurationError
, source = Source ""
, errors = configurationErrors
}
]
in
{ rulesWithInvalidFixes = rulesWithInvalidFixes
, hasIgnoredFixableErrors = hasIgnoredFixableErrors
}
[ formatReports detailsMode mode filesWithErrors
, [ Text.from "I found "
, pluralize (List.length configurationErrors) "configuration error" |> Text.from |> Text.inRed
, Text.from "."
]
]
|> 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
errors
{ rulesWithInvalidFixes = Set.empty, hasIgnoredFixableErrors = False }


classifyFixesHelp : List Error -> { rulesWithInvalidFixes : Set String, hasIgnoredFixableErrors : Bool } -> { rulesWithInvalidFixes : Set String, hasIgnoredFixableErrors : Bool }
Expand Down Expand Up @@ -759,19 +806,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
2 changes: 1 addition & 1 deletion test/snapshots/review/config-configuration-error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Some.Rule: This is a configuration error message

These are configuration error details

I found 1 error in 1 file.
I found 1 configuration error.