Skip to content

Commit

Permalink
Improve module parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredramirez committed Dec 24, 2024
1 parent 1bae24b commit 99377b0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
33 changes: 33 additions & 0 deletions crates/compiler/load/tests/test_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6458,6 +6458,39 @@ All branches in an `if` must have the same type!
)
}

#[test]
fn unneeded_module_name() {
report_header_problem_as(
indoc!(
r"
module foobar []
"
),
indoc!(
r#"
── WEIRD MODULE NAME in /code/proj/Main.roc ────────────────────────────────────
I am partway through parsing a header, but I got stuck here:
1│ module foobar []
^
I am expecting a list of exports like
module [func, value]
or module params like
module { echo } -> [func, value]
If you're trying to specify a module name, recall that unlike
application names, module names are not specified in the header.
Instead, they are derived from the name of the module's filename.
"#
),
)
}

test_report!(
apply_unary_negative,
indoc!(
Expand Down
35 changes: 35 additions & 0 deletions crates/reporting/src/error/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4100,6 +4100,41 @@ fn to_exposes_report<'a>(

EExposes::Space(error, pos) => to_space_report(alloc, lines, filename, &error, pos),

EExposes::ListStart(pos@Position { offset: 7 }) => {
let surroundings = Region::new(start, pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(pos));

let doc = alloc.stack([
alloc.reflow(r"I am partway through parsing a header, but I got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region, severity),
alloc.concat([
alloc.reflow("I am expecting a list of exports like"),
]),
alloc
.parser_suggestion("module [func, value]")
.indent(4),
alloc.concat([
alloc.reflow("or module params like"),
]),
alloc
.parser_suggestion("module { echo } -> [func, value]")
.indent(4),
alloc.concat([
alloc.reflow("If you're trying to specify a module name, recall that unlike "),
alloc.reflow("application names, module names are not specified in the header. "),
alloc.reflow("Instead, they are derived from the name of the module's filename."),
]),
]);


Report {
filename,
doc,
title: "WEIRD MODULE NAME".to_string(),
severity,
}
},

// If you're adding or changing syntax, please handle the case with a
// good error message above instead of adding more unhandled cases below.
EExposes::Open(pos) |
Expand Down

0 comments on commit 99377b0

Please sign in to comment.