diff options
author | Yusuke Endoh <mame@ruby-lang.org> | 2021-07-12 16:47:24 +0900 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2021-07-12 16:48:15 +0900 |
commit | 8b01d16ad661a02157311a6a24f415713d69a8a4 (patch) | |
tree | ac885f04ba4371aabafe30c0331d4fb129a97fd8 /lib/error_highlight | |
parent | 028441d22fee121d129126c55938690be38bd3d9 (diff) | |
download | ruby-8b01d16ad661a02157311a6a24f415713d69a8a4.tar.gz |
[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
Diffstat (limited to 'lib/error_highlight')
-rw-r--r-- | lib/error_highlight/base.rb | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/error_highlight/base.rb b/lib/error_highlight/base.rb index ee6545b372..fc23508a5d 100644 --- a/lib/error_highlight/base.rb +++ b/lib/error_highlight/base.rb @@ -21,6 +21,9 @@ module ErrorHighlight end class Spotter + class NonAscii < Exception; end + private_constant :NonAscii + def initialize(node, point_type: :name, name: nil) @node = node @point_type = point_type @@ -31,7 +34,14 @@ module ErrorHighlight @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 @@ -115,6 +125,9 @@ module ErrorHighlight else return nil end + + rescue NonAscii + nil end private |