@@ -4,22 +4,20 @@ module Reporting.Warning
4 ( Warning(..)
4 ( Warning(..)
5 , Context(..)
5 , Context(..)
11−import Data.Aeson ((.=))
11+import Data.Monoid ((<>))
12−import qualified Data.Aeson as Json
13−import Data.Text (Text)
15import qualified AST.Canonical as Can
13import qualified AST.Canonical as Can
14+import qualified AST.Utils.Type as Type
16import qualified Elm.Name as N
15import qualified Elm.Name as N
17−import qualified Reporting.Annotation as A
18import qualified Reporting.Region as R
16import qualified Reporting.Region as R
19import qualified Reporting.Report as Report
17import qualified Reporting.Report as Report
20−import qualified Reporting.Render.Type as RenderType
18+import qualified Reporting.Render.Code as Code
19+import qualified Reporting.Render.Type as RT
21−import qualified Reporting.Helpers as Help
20+import qualified Reporting.Helpers as H
22−import Reporting.Helpers ((<>), text)
@@ -27,9 +25,9 @@ import Reporting.Helpers ((<>), text)
29data Warning
27data Warning
30− = UnusedImport N.Name
28+ = UnusedImport R.Region N.Name
31 | UnusedVariable R.Region Context N.Name
29 | UnusedVariable R.Region Context N.Name
32− | MissingTypeAnnotation Text Can.Type
30+ | MissingTypeAnnotation R.Region N.Name Can.Type
35data Context = Def | Pattern
33data Context = Def | Pattern
@@ -39,47 +37,63 @@ data Context = Def | Pattern
39-- TO REPORT
37-- TO REPORT
42−toReport :: RenderType.Localizer -> Warning -> Report.Report
40+toReport :: Code.Source -> Warning -> Report.Report
43−toReport localizer warning =
41+toReport source warning =
44 case warning of
42 case warning of
45− UnusedImport moduleName ->
43+ UnusedImport region moduleName ->
47− "unused import"
44+ Report.Report "unused import" region [] $
48− Nothing
45+ Report.toCodeSnippet source region Nothing
49− ("Nothing from the `" <> moduleName <> "` module is used in this file.")
48+ "Nothing from the `" <> N.toString moduleName <> "` module is used in this file."
50− (text "I recommend removing unused imports.")
50+ "I recommend removing unused imports."
52− UnusedVariable _ context name ->
53+ UnusedVariable region context name ->
54− (defOrPat context "unused definition" "unused variable")
54+ let title = defOrPat context "unused definition" "unused variable" in
55− Nothing
55+ Report.Report title region [] $
56+ Report.toCodeSnippet source region Nothing
56− ("You are not using `" <> name <> "` anywhere.")
59+ "You are not using `" <> N.toString name <> "` anywhere."
57− ( Help.stack
61+ H.stack
58− [ Help.reflowParagraph $
62+ [ H.reflow $
59− "Is there a typo? Maybe you intended to use `" <> name
63+ "Is there a typo? Maybe you intended to use `" <> N.toString name
60− <> "` but typed a similar name instead?"
64+ <> "` somewhere but typed another name instead?"
61− , Help.reflowParagraph $
65+ , H.reflow $
62− defOrPat context
66+ defOrPat context
63− ( "If you are sure there is no typo, remove the definition.\
67+ ( "If you are sure there is no typo, remove the definition.\
64− \ This way future readers will not have to wonder!"
68+ \ This way future readers will not have to wonder why it is there!"
66− ( "If you are sure there is no typo, replace `" <> name
70+ ( "If you are sure there is no typo, replace `" <> N.toString name
67− <> "` with _ so future readers will not have to wonder!"
71+ <> "` with _ so future readers will not have to wonder why it is there!"
72− MissingTypeAnnotation name inferredType ->
74− "missing type annotation"
76− ("Top-level value " <> Help.functionName name <> " does not have a type annotation.")
78− [ text "I inferred the type annotation so you can copy it into your code:"
79− , RenderType.annotation localizer name inferredType
76+ MissingTypeAnnotation region name inferredType ->
77+ Report.Report "missing type annotation" region [] $
78+ Report.toCodeSnippet source region Nothing
81+ case Type.deepDealias inferredType of
83+ "The `" <> N.toString name <> "` function has no type annotation."
86+ "The `" <> N.toString name <> "` definition has no type annotation."
89+ [ "I inferred the type annotation myself though! You can copy it into your code:"
90+ , H.green $ H.hang 4 $ H.sep $
91+ [ H.nameToDoc name <> " :"
92+ , RT.canToDoc RT.None inferredType
84defOrPat :: Context -> a -> a -> a
98defOrPat :: Context -> a -> a -> a
85defOrPat context def pat =
99defOrPat context def pat =
@@ -87,20 +101,3 @@ defOrPat context def pat =
87 Def -> def
101 Def -> def
88 Pattern -> pat
102 Pattern -> pat
95−toJson :: RenderType.Localizer -> FilePath -> A.Located Warning -> Json.Value
96−toJson localizer filePath (A.At region warning) =
98− (maybeRegion, additionalFields) =
99− Report.toJson [] (toReport localizer warning)
102− [ "file" .= filePath
103− , "region" .= maybe region id maybeRegion
104− , "type" .= ("warning" :: Text)