Skip to content
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
23 changes: 16 additions & 7 deletions compiler/src/Reporting/Render/Code.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,22 @@ toSource source =
-- CODE FORMATTING

toSnippet :: Source -> A.Region -> Maybe A.Region -> (D.Doc, D.Doc) -> D.Doc
toSnippet source region highlight (preHint, postHint) =
D.vcat
[ preHint,
"",
render source region highlight,
postHint
]
toSnippet source region@(A.Region (A.Position startLine _) (A.Position _ _)) highlight (preHint, postHint) =
if startLine > 0
then
D.vcat
[ preHint,
"",
render source region highlight,
postHint
]
else
-- The region doesn't point to actual source code. Don't render it.
D.vcat
[ preHint,
"",
postHint
]

toPair :: Source -> A.Region -> A.Region -> (D.Doc, D.Doc) -> (D.Doc, D.Doc, D.Doc) -> D.Doc
toPair source r1 r2 (oneStart, oneEnd) (twoStart, twoMiddle, twoEnd) =
Expand Down
6 changes: 5 additions & 1 deletion gren.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,17 @@ Test-Suite gren-tests
Parse.UnderscorePatternSpec
Parse.MultilineStringSpec
Parse.DeclSpec
Reporting.Error.ImportSpec

Build-Depends:
gren:common,
base >= 4.19 && <5,
containers >= 0.6 && < 0.7,
utf8-string,
bytestring >= 0.11 && < 0.12,
hspec >= 2.7.10 && < 3
hspec >= 2.7.10 && < 3,
prettyprinter >= 1.7.1 && < 2,
text >= 2.1.2 && < 3

Build-Tool-Depends:
hspec-discover:hspec-discover >= 2.7.10 && < 3
49 changes: 49 additions & 0 deletions tests/Reporting/Error/ImportSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Reporting.Error.ImportSpec (spec) where

import Data.Set qualified as Set
import Data.Text as T
import Data.Text.Encoding as T
import Data.Utf8 qualified as Utf8
import Prettyprinter qualified as P
import Prettyprinter.Render.Text (renderStrict)
import Reporting.Annotation qualified as A
import Reporting.Error.Import qualified as Import
import Reporting.Render.Code qualified as Code
import Reporting.Report qualified as Report
import Test.Hspec (Spec, describe, it)

spec :: Spec
spec = do
describe "Import error reporting" $ do
{-
When "gren-lang/core" module is not listed in gren.json, the
default imports that the compiler inserts into the code
fail, but there is no source code region to report.
We want to ensure the reporting code doesn't crash, and
actually reports that modules like Basics (and others) are reported
as not found.
-}
it "Give proper error if default package dependencies are not found" $ do
let -- Empty source code
source = Code.toSource (T.encodeUtf8 (T.pack ""))

-- The "Module Not Found" Error
err =
Import.Error
{ -- IMPORTANT: The region here is zero, not pointing to any
-- possible lines of source code. This happens when
-- "import Basics" is added by default by the compiler, without
-- it appearing in the actual source code.
Import._region = A.Region (A.Position 0 0) (A.Position 0 0),
Import._import = Utf8.fromChars "foo",
Import._unimported = Set.singleton (Utf8.fromChars "Basics"),
Import._problem = Import.NotFound
}

-- Create the report
report = Import.toReport source err

-- Convert the Prettyprinter Doc to a Data.Text
messageText = renderStrict (P.layoutCompact $ Report._message report)
in -- Does it have one of the missing modules reported in it?
T.isInfixOf (T.pack "Basics") messageText
Loading