From ca4e5b1eb33f3bae9ced2e7643ae7db3e11fa65d Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Wed, 30 Jun 2021 12:31:55 +0900 Subject: [PATCH] [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 5f3a86bbe2..ee6545b372 100644 --- a/lib/error_highlight/base.rb +++ b/lib/error_highlight/base.rb @@ -4,10 +4,9 @@ module ErrorHighlight # 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 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 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 end when :ATTRASGN - case @point + case @point_type when :name spot_attrasgn_for_name when :args @@ -56,7 +57,7 @@ module ErrorHighlight end when :OPCALL - case @point + case @point_type when :name spot_opcall_for_name when :args @@ -64,7 +65,7 @@ module ErrorHighlight end when :FCALL - case @point + case @point_type when :name spot_fcall_for_name when :args @@ -75,7 +76,7 @@ module ErrorHighlight 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 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 3cfc72951d..d8d2175f8a 100644 --- a/lib/error_highlight/core_ext.rb +++ b/lib/error_highlight/core_ext.rb @@ -21,16 +21,13 @@ module ErrorHighlight 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