Turn Lint.LintError into an opaque type

This will help make the type extensible later on without breaking 
changes
This commit is contained in:
Jeroen Engels 2019-07-29 23:56:23 +02:00
parent daf16f11d5
commit a67321e0a0

View File

@ -1,4 +1,7 @@
module Lint exposing (LintError, lintSource) module Lint exposing
( LintError, lintSource
, errorFile, errorRuleName, errorMessage, errorDetails, errorRange
)
{-| Module to configure your linting configuration and run it on a source file. {-| Module to configure your linting configuration and run it on a source file.
@ -7,6 +10,11 @@ module Lint exposing (LintError, lintSource)
@docs LintError, lintSource @docs LintError, lintSource
# Errors
@docs errorFile, errorRuleName, errorMessage, errorDetails, errorRange
-} -}
import Elm.Parser as Parser import Elm.Parser as Parser
@ -23,13 +31,18 @@ Note: This should not be confused with `Error` from the `Lint.Rule` module.
like the name of the rule that emitted it and the file name. like the name of the rule that emitted it and the file name.
-} -}
type alias LintError = type LintError
{ file : String = LintError
, ruleName : String { file : String
, message : String , ruleName : String
, details : List String , message : String
, range : Range , details : List String
} , range : Range
}
-- LINTING
{-| Lints a file and gives back the errors raised by the given rules. {-| Lints a file and gives back the errors raised by the given rules.
@ -54,15 +67,16 @@ lintSource config { fileName, source } =
|> List.sortWith compareErrorPositions |> List.sortWith compareErrorPositions
Err _ -> Err _ ->
[ { file = fileName [ LintError
, ruleName = "ParsingError" { file = fileName
, message = fileName ++ " is not a correct Elm file" , ruleName = "ParsingError"
, details = , message = fileName ++ " is not a correct Elm file"
, details =
[ "I could not understand the contents of this file, and this prevents me from analyzing it. It's highly likely that the contents of the file is not correct Elm code." [ "I could not understand the contents of this file, and this prevents me from analyzing it. It's highly likely that the contents of the file is not correct Elm code."
, "Hint: Try running `elm make`. The compiler should give you better hints on how to resolve the problem." , "Hint: Try running `elm make`. The compiler should give you better hints on how to resolve the problem."
] ]
, range = { start = { row = 0, column = 0 }, end = { row = 0, column = 0 } } , range = { start = { row = 0, column = 0 }, end = { row = 0, column = 0 } }
} }
] ]
@ -73,7 +87,7 @@ lintSourceWithRule fileName file rule =
compareErrorPositions : LintError -> LintError -> Order compareErrorPositions : LintError -> LintError -> Order
compareErrorPositions a b = compareErrorPositions (LintError a) (LintError b) =
compareRange a.range b.range compareRange a.range b.range
@ -121,12 +135,13 @@ compareRange a b =
ruleErrorToLintError : String -> Rule -> Rule.Error -> LintError ruleErrorToLintError : String -> Rule -> Rule.Error -> LintError
ruleErrorToLintError file rule error = ruleErrorToLintError file rule error =
{ file = file LintError
, ruleName = Rule.name rule { file = file
, message = Rule.errorMessage error , ruleName = Rule.name rule
, details = Rule.errorDetails error , message = Rule.errorMessage error
, range = Rule.errorRange error , details = Rule.errorDetails error
} , range = Rule.errorRange error
}
{-| Parse source code into a AST {-| Parse source code into a AST
@ -137,3 +152,42 @@ parseSource source =
|> Parser.parse |> Parser.parse
|> Result.mapError (\error -> "Parsing error") |> Result.mapError (\error -> "Parsing error")
|> Result.map (process init) |> Result.map (process init)
-- ERRORS
{-| Get the file of an error.
-}
errorFile : LintError -> String
errorFile (LintError error) =
error.file
{-| Get the name of the rule of an error.
-}
errorRuleName : LintError -> String
errorRuleName (LintError error) =
error.ruleName
{-| Get the message of an error.
-}
errorMessage : LintError -> String
errorMessage (LintError error) =
error.message
{-| Get the details of an error.
-}
errorDetails : LintError -> List String
errorDetails (LintError error) =
error.details
{-| Get the range of an error.
-}
errorRange : LintError -> Range
errorRange (LintError error) =
error.range