on master help sign in
Update warnings

Drop JSON format for now. Can rebuild later on.

commit7ae440051122930a022a8365ffea20f8078c20cc authorEvan Czaplicki <evancz@users.noreply.github.com> dateSat 10 Feb 2018, 18:34 (9 years ago) parent3fe6d87
1 file changed+61−64
src/Reporting/Warning.hs+61−64
src/Reporting/Warning.hs+61−64
@@ -4,22 +4,20 @@ module Reporting.Warning
44 ( Warning(..)
55 , Context(..)
66 , toReport
7− , toJson
87 )
98 where
109
11−import Data.Aeson ((.=))
12−import qualified Data.Aeson as Json
13−import Data.Text (Text)
10+
11+import Data.Monoid ((<>))
1412
1513import qualified AST.Canonical as Can
14+import qualified AST.Utils.Type as Type
1615import qualified Elm.Name as N
17−import qualified Reporting.Annotation as A
1816import qualified Reporting.Region as R
1917import qualified Reporting.Report as Report
20−import qualified Reporting.Render.Type as RenderType
21−import qualified Reporting.Helpers as Help
22−import Reporting.Helpers ((<>), text)
18+import qualified Reporting.Render.Code as Code
19+import qualified Reporting.Render.Type as RT
20+import qualified Reporting.Helpers as H
2321
2422
2523
@@ -27,9 +25,9 @@ import Reporting.Helpers ((<>), text)
2725
2826
2927data Warning
30− = UnusedImport N.Name
28+ = UnusedImport R.Region N.Name
3129 | UnusedVariable R.Region Context N.Name
32− | MissingTypeAnnotation Text Can.Type
30+ | MissingTypeAnnotation R.Region N.Name Can.Type
3331
3432
3533data Context = Def | Pattern
@@ -39,47 +37,63 @@ data Context = Def | Pattern
3937-- TO REPORT
4038
4139
42−toReport :: RenderType.Localizer -> Warning -> Report.Report
43−toReport localizer warning =
40+toReport :: Code.Source -> Warning -> Report.Report
41+toReport source warning =
4442 case warning of
45− UnusedImport moduleName ->
46− Report.report
47− "unused import"
48− Nothing
49− ("Nothing from the `" <> moduleName <> "` module is used in this file.")
50− (text "I recommend removing unused imports.")
51−
52− UnusedVariable _ context name ->
53− Report.report
54− (defOrPat context "unused definition" "unused variable")
55− Nothing
56− ("You are not using `" <> name <> "` anywhere.")
57− ( Help.stack
58− [ Help.reflowParagraph $
59− "Is there a typo? Maybe you intended to use `" <> name
60− <> "` but typed a similar name instead?"
61− , Help.reflowParagraph $
62− defOrPat context
63− ( "If you are sure there is no typo, remove the definition.\
64− \ This way future readers will not have to wonder!"
65− )
66− ( "If you are sure there is no typo, replace `" <> name
67− <> "` with _ so future readers will not have to wonder!"
68− )
69− ]
70− )
71−
72− MissingTypeAnnotation name inferredType ->
73− Report.report
74− "missing type annotation"
75− Nothing
76− ("Top-level value " <> Help.functionName name <> " does not have a type annotation.")
77− ( Help.stack
78− [ text "I inferred the type annotation so you can copy it into your code:"
79− , RenderType.annotation localizer name inferredType
43+ UnusedImport region moduleName ->
44+ Report.Report "unused import" region [] $
45+ Report.toCodeSnippet source region Nothing
46+ (
47+ H.reflow $
48+ "Nothing from the `" <> N.toString moduleName <> "` module is used in this file."
49+ ,
50+ "I recommend removing unused imports."
51+ )
52+
53+ UnusedVariable region context name ->
54+ let title = defOrPat context "unused definition" "unused variable" in
55+ Report.Report title region [] $
56+ Report.toCodeSnippet source region Nothing
57+ (
58+ H.reflow $
59+ "You are not using `" <> N.toString name <> "` anywhere."
60+ ,
61+ H.stack
62+ [ H.reflow $
63+ "Is there a typo? Maybe you intended to use `" <> N.toString name
64+ <> "` somewhere but typed another name instead?"
65+ , H.reflow $
66+ defOrPat context
67+ ( "If you are sure there is no typo, remove the definition.\
68+ \ This way future readers will not have to wonder why it is there!"
69+ )
70+ ( "If you are sure there is no typo, replace `" <> N.toString name
71+ <> "` with _ so future readers will not have to wonder why it is there!"
72+ )
8073 ]
8174 )
8275
76+ MissingTypeAnnotation region name inferredType ->
77+ Report.Report "missing type annotation" region [] $
78+ Report.toCodeSnippet source region Nothing
79+ (
80+ H.reflow $
81+ case Type.deepDealias inferredType of
82+ Can.TLambda _ _ ->
83+ "The `" <> N.toString name <> "` function has no type annotation."
84+
85+ _ ->
86+ "The `" <> N.toString name <> "` definition has no type annotation."
87+ ,
88+ H.stack
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
93+ ]
94+ ]
95+ )
96+
8397
8498defOrPat :: Context -> a -> a -> a
8599defOrPat context def pat =
@@ -87,20 +101,3 @@ defOrPat context def pat =
87101 Def -> def
88102 Pattern -> pat
89103
90−
91−
92−-- TO JSON
93−
94−
95−toJson :: RenderType.Localizer -> FilePath -> A.Located Warning -> Json.Value
96−toJson localizer filePath (A.At region warning) =
97− let
98− (maybeRegion, additionalFields) =
99− Report.toJson [] (toReport localizer warning)
100− in
101− Json.object $
102− [ "file" .= filePath
103− , "region" .= maybe region id maybeRegion
104− , "type" .= ("warning" :: Text)
105− ]
106− ++ additionalFields