diff options
author | felipe stival <14948182+v0idpwn@users.noreply.github.com> | 2021-12-10 10:27:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 09:27:25 +0100 |
commit | b13a9977d90c22ffce2aeaf1622b76789cadb2ff (patch) | |
tree | 26120e7928092aacc09cba7ff6742a1dcf6e0c89 | |
parent | a213cb46d394b202556926db9ff67209c14342c9 (diff) | |
download | elixir-b13a9977d90c22ffce2aeaf1622b76789cadb2ff.tar.gz |
Change approach: never show error if line is empty (#11466)
-rw-r--r-- | lib/elixir/src/elixir_errors.erl | 7 | ||||
-rw-r--r-- | lib/elixir/test/elixir/kernel/errors_test.exs | 15 |
2 files changed, 20 insertions, 2 deletions
diff --git a/lib/elixir/src/elixir_errors.erl b/lib/elixir/src/elixir_errors.erl index 01035d12e..7b3255f3e 100644 --- a/lib/elixir/src/elixir_errors.erl +++ b/lib/elixir/src/elixir_errors.erl @@ -87,9 +87,12 @@ snippet(InputString, Location, StartLine, StartColumn) -> case lists:keyfind(column, 1, Location) of {column, Column} -> Lines = string:split(InputString, "\n", all), - Snippet = elixir_utils:characters_to_binary(lists:nth(Line - StartLine + 1, Lines)), + Snippet = (lists:nth(Line - StartLine + 1, Lines)), Offset = if Line == StartLine -> Column - StartColumn; true -> Column - 1 end, - #{content => Snippet, offset => Offset}; + case string:trim(Snippet, leading) of + [] -> nil; + _ -> #{content => elixir_utils:characters_to_binary(Snippet), offset => Offset} + end; false -> nil diff --git a/lib/elixir/test/elixir/kernel/errors_test.exs b/lib/elixir/test/elixir/kernel/errors_test.exs index 55ceee9c3..dd46f3d9a 100644 --- a/lib/elixir/test/elixir/kernel/errors_test.exs +++ b/lib/elixir/test/elixir/kernel/errors_test.exs @@ -468,6 +468,21 @@ defmodule Kernel.ErrorsTest do assert_eval_raise TokenMissingError, ~r/nofile:1:3: invalid escape \\ at end of file/, '1 \\' end + test "show snippet on missing tokens" do + assert_eval_raise TokenMissingError, + "nofile:1:25: missing terminator: end (for \"do\" starting at line 1)\n" <> + " |\n" <> + " 1 | defmodule ShowSnippet do\n" <> + " | ^", + 'defmodule ShowSnippet do' + end + + test "don't show snippet when error line is empty" do + assert_eval_raise TokenMissingError, + "nofile:3:1: missing terminator: end (for \"do\" starting at line 1)", + 'defmodule ShowSnippet do\n\n' + end + test "function local conflict" do assert_eval_raise CompileError, "nofile:3: imported Kernel.&&/2 conflicts with local function", |