Skip to content

Commit 9d67128

Browse files
committed
Support a file that has no final newline
1 parent 38f20fa commit 9d67128

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/error_highlight/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def initialize(node, point_type: :name, name: nil)
3535

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

3940
# It require some work to support Unicode (or multibyte) characters.
4041
# Tentatively, we stop highlighting if the code snippet has non-ascii characters.

test/test_error_highlight.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,4 +1016,20 @@ def test_hard_tabs
10161016
load tmp.path
10171017
end
10181018
end
1019+
1020+
def test_no_final_newline
1021+
tmp = Tempfile.new(["error_highlight_test", ".rb"])
1022+
tmp << "1.time {}"
1023+
tmp.close(false)
1024+
1025+
assert_error_message(NoMethodError, <<~END) do
1026+
undefined method `time' for 1:Integer
1027+
1028+
1.time {}
1029+
^^^^^
1030+
END
1031+
1032+
load tmp.path
1033+
end
1034+
end
10191035
end

0 commit comments

Comments
 (0)