Skip to content

Commit 47171d9

Browse files
v0idpwnjosevalim
authored andcommitted
Change approach: never show error if line is empty (#11466)
1 parent edafcc4 commit 47171d9

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/elixir/src/elixir_errors.erl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ snippet(InputString, Location, StartLine, StartColumn) ->
8787
case lists:keyfind(column, 1, Location) of
8888
{column, Column} ->
8989
Lines = string:split(InputString, "\n", all),
90-
Snippet = elixir_utils:characters_to_binary(lists:nth(Line - StartLine + 1, Lines)),
90+
Snippet = (lists:nth(Line - StartLine + 1, Lines)),
9191
Offset = if Line == StartLine -> Column - StartColumn; true -> Column - 1 end,
92-
#{content => Snippet, offset => Offset};
92+
case string:trim(Snippet, leading) of
93+
[] -> nil;
94+
_ -> #{content => elixir_utils:characters_to_binary(Snippet), offset => Offset}
95+
end;
9396

9497
false ->
9598
nil

lib/elixir/test/elixir/kernel/errors_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,21 @@ defmodule Kernel.ErrorsTest do
491491
assert_eval_raise TokenMissingError, ~r/nofile:1:3: invalid escape \\ at end of file/, '1 \\'
492492
end
493493

494+
test "show snippet on missing tokens" do
495+
assert_eval_raise TokenMissingError,
496+
"nofile:1:25: missing terminator: end (for \"do\" starting at line 1)\n" <>
497+
" |\n" <>
498+
" 1 | defmodule ShowSnippet do\n" <>
499+
" | ^",
500+
'defmodule ShowSnippet do'
501+
end
502+
503+
test "don't show snippet when error line is empty" do
504+
assert_eval_raise TokenMissingError,
505+
"nofile:3:1: missing terminator: end (for \"do\" starting at line 1)",
506+
'defmodule ShowSnippet do\n\n'
507+
end
508+
494509
test "function local conflict" do
495510
assert_eval_raise CompileError,
496511
"nofile:3: imported Kernel.&&/2 conflicts with local function",

0 commit comments

Comments
 (0)