1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/error_highlight] Stop showing a code snippet if it has non-ascii characters

See https://github.com/ruby/error_highlight/issues/4

https://github.com/ruby/error_highlight/commit/c20efd3961
This commit is contained in:
Yusuke Endoh 2021-07-12 16:47:24 +09:00 committed by git
parent 028441d22f
commit 8b01d16ad6

View file

@ -21,6 +21,9 @@ module ErrorHighlight
end end
class Spotter class Spotter
class NonAscii < Exception; end
private_constant :NonAscii
def initialize(node, point_type: :name, name: nil) def initialize(node, point_type: :name, name: nil)
@node = node @node = node
@point_type = point_type @point_type = point_type
@ -31,7 +34,14 @@ module ErrorHighlight
@multiline = false # Allow multiline spot @multiline = false # Allow multiline spot
@fetch = -> (lineno, last_lineno = lineno) do @fetch = -> (lineno, last_lineno = lineno) do
@node.script_lines[lineno - 1 .. last_lineno - 1].join("") snippet = @node.script_lines[lineno - 1 .. last_lineno - 1].join("")
# It require some work to support Unicode (or multibyte) characters.
# Tentatively, we stop highlighting if the code snippet has non-ascii characters.
# See https://github.com/ruby/error_highlight/issues/4
raise NonAscii unless snippet.ascii_only?
snippet
end end
end end
@ -115,6 +125,9 @@ module ErrorHighlight
else else
return nil return nil
end end
rescue NonAscii
nil
end end
private private