Skip to content

Commit c20efd3

Browse files
committed
Stop showing a code snippet if it has non-ascii characters
See #4
1 parent b157276 commit c20efd3

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/error_highlight/base.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def self.spot(...)
2121
end
2222

2323
class Spotter
24+
class NonAscii < Exception; end
25+
private_constant :NonAscii
26+
2427
def initialize(node, point_type: :name, name: nil)
2528
@node = node
2629
@point_type = point_type
@@ -31,7 +34,14 @@ def initialize(node, point_type: :name, name: nil)
3134
@multiline = false # Allow multiline spot
3235

3336
@fetch = -> (lineno, last_lineno = lineno) do
34-
@node.script_lines[lineno - 1 .. last_lineno - 1].join("")
37+
snippet = @node.script_lines[lineno - 1 .. last_lineno - 1].join("")
38+
39+
# It require some work to support Unicode (or multibyte) characters.
40+
# Tentatively, we stop highlighting if the code snippet has non-ascii characters.
41+
# See https://github.com/ruby/error_highlight/issues/4
42+
raise NonAscii unless snippet.ascii_only?
43+
44+
snippet
3545
end
3646
end
3747

@@ -115,6 +125,9 @@ def spot
115125
else
116126
return nil
117127
end
128+
129+
rescue NonAscii
130+
nil
118131
end
119132

120133
private

0 commit comments

Comments
 (0)