From 8b01d16ad661a02157311a6a24f415713d69a8a4 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Mon, 12 Jul 2021 16:47:24 +0900 Subject: [PATCH] [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 --- lib/error_highlight/base.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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