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

ruby-changes:66642

From: Yusuke <ko1@a...>
Date: Wed, 30 Jun 2021 12:49:36 +0900 (JST)
Subject: [ruby-changes:66642] ca4e5b1eb3 (master): [ruby/error_highlight] Reconsider the API of ErrorHighlight.spot

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

From ca4e5b1eb33f3bae9ced2e7643ae7db3e11fa65d Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Wed, 30 Jun 2021 12:31:55 +0900
Subject: [ruby/error_highlight] Reconsider the API of ErrorHighlight.spot

https://github.com/ruby/error_highlight/commit/acb2046a82
---
 lib/error_highlight/base.rb     | 25 +++++++++++++------------
 lib/error_highlight/core_ext.rb |  9 +++------
 2 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/lib/error_highlight/base.rb b/lib/error_highlight/base.rb
index 5f3a86b..ee6545b 100644
--- a/lib/error_highlight/base.rb
+++ b/lib/error_highlight/base.rb
@@ -4,10 +4,9 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L4
   # Identify the code fragment that seems associated with a given error
   #
   # Arguments:
-  #  node: RubyVM::AbstractSyntaxTree::Node
-  #  point: :name | :args
+  #  node: RubyVM::AbstractSyntaxTree::Node (script_lines should be enabled)
+  #  point_type: :name | :args
   #  name: The name associated with the NameError/NoMethodError
-  #  fetch: A block to fetch a specified code line (or lines)
   #
   # Returns:
   #  {
@@ -22,16 +21,18 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L21
   end
 
   class Spotter
-    def initialize(node, point, name: nil, &fetch)
+    def initialize(node, point_type: :name, name: nil)
       @node = node
-      @point = point
+      @point_type = point_type
       @name = name
 
       # Not-implemented-yet options
       @arg = nil # Specify the index or keyword at which argument caused the TypeError/ArgumentError
       @multiline = false # Allow multiline spot
 
-      @fetch = fetch
+      @fetch = -> (lineno, last_lineno = lineno) do
+        @node.script_lines[lineno - 1 .. last_lineno - 1].join("")
+      end
     end
 
     def spot
@@ -40,7 +41,7 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L41
       case @node.type
 
       when :CALL, :QCALL
-        case @point
+        case @point_type
         when :name
           spot_call_for_name
         when :args
@@ -48,7 +49,7 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L49
         end
 
       when :ATTRASGN
-        case @point
+        case @point_type
         when :name
           spot_attrasgn_for_name
         when :args
@@ -56,7 +57,7 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L57
         end
 
       when :OPCALL
-        case @point
+        case @point_type
         when :name
           spot_opcall_for_name
         when :args
@@ -64,7 +65,7 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L65
         end
 
       when :FCALL
-        case @point
+        case @point_type
         when :name
           spot_fcall_for_name
         when :args
@@ -75,7 +76,7 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L76
         spot_vcall
 
       when :OP_ASGN1
-        case @point
+        case @point_type
         when :name
           spot_op_asgn1_for_name
         when :args
@@ -83,7 +84,7 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L84
         end
 
       when :OP_ASGN2
-        case @point
+        case @point_type
         when :name
           spot_op_asgn2_for_name
         when :args
diff --git a/lib/error_highlight/core_ext.rb b/lib/error_highlight/core_ext.rb
index 3cfc729..d8d2175 100644
--- a/lib/error_highlight/core_ext.rb
+++ b/lib/error_highlight/core_ext.rb
@@ -21,16 +21,13 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/core_ext.rb#L21
 
         case self
         when NoMethodError, NameError
-          point = :name
+          opts[:point_type] = :name
           opts[:name] = name
         when TypeError, ArgumentError
-          point = :args
+          opts[:point_type] = :args
         end
 
-        spot = ErrorHighlight.spot(node, point, **opts) do |lineno, last_lineno|
-          last_lineno ||= lineno
-          node.script_lines[lineno - 1 .. last_lineno - 1].join("")
-        end
+        spot = ErrorHighlight.spot(node, **opts)
 
       rescue Errno::ENOENT
       end
-- 
cgit v1.1


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

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