[前][次][番号順一覧][スレッド一覧]

ruby-changes:66755

From: Yusuke <ko1@a...>
Date: Mon, 12 Jul 2021 16:48:28 +0900 (JST)
Subject: [ruby-changes:66755] 8b01d16ad6 (master): [ruby/error_highlight] Stop showing a code snippet if it has non-ascii characters

https://git.ruby-lang.org/ruby.git/commit/?id=8b01d16ad6

From 8b01d16ad661a02157311a6a24f415713d69a8a4 Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Mon, 12 Jul 2021 16:47:24 +0900
Subject: [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 ee6545b..fc23508 100644
--- a/lib/error_highlight/base.rb
+++ b/lib/error_highlight/base.rb
@@ -21,6 +21,9 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L21
   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 https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L34
       @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 https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L125
       else
         return nil
       end
+
+    rescue NonAscii
+      nil
     end
 
     private
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]