Skip to content

Commit

Permalink
Stop showing a code snippet if it has non-ascii characters
Browse files Browse the repository at this point in the history
See #4
  • Loading branch information
mame committed Jul 12, 2021
1 parent b157276 commit c20efd3
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/error_highlight/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def self.spot(...)
end

class Spotter
class NonAscii < Exception; end
private_constant :NonAscii

def initialize(node, point_type: :name, name: nil)
@node = node
@point_type = point_type
Expand All @@ -31,7 +34,14 @@ def initialize(node, point_type: :name, name: nil)
@multiline = false # Allow multiline spot

@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

Expand Down Expand Up @@ -115,6 +125,9 @@ def spot
else
return nil
end

rescue NonAscii
nil
end

private
Expand Down

0 comments on commit c20efd3

Please sign in to comment.