Skip to content

Commit

Permalink
Support a file that has no final newline
Browse files Browse the repository at this point in the history
  • Loading branch information
mame committed Jul 13, 2021
1 parent 38f20fa commit 9d67128
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/error_highlight/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def initialize(node, point_type: :name, name: nil)

@fetch = -> (lineno, last_lineno = lineno) do
snippet = @node.script_lines[lineno - 1 .. last_lineno - 1].join("")
snippet += "\n" unless snippet.end_with?("\n")

# It require some work to support Unicode (or multibyte) characters.
# Tentatively, we stop highlighting if the code snippet has non-ascii characters.
Expand Down
16 changes: 16 additions & 0 deletions test/test_error_highlight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1016,4 +1016,20 @@ def test_hard_tabs
load tmp.path
end
end

def test_no_final_newline
tmp = Tempfile.new(["error_highlight_test", ".rb"])
tmp << "1.time {}"
tmp.close(false)

assert_error_message(NoMethodError, <<~END) do
undefined method `time' for 1:Integer
1.time {}
^^^^^
END

load tmp.path
end
end
end

0 comments on commit 9d67128

Please sign in to comment.