instance method
ErrorHighlight::Spotter#spot
spot
lib/error_highlight/base.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/error_highlight/base.rb, line 125
def spot
return nil unless @node
# In Ruby 3.2 or later, a nested constant access (like `Foo::Bar::Baz`)
# is compiled to one instruction (opt_getconstant_path).
# @node points to the node of the whole `Foo::Bar::Baz` even if `Foo`
# or `Foo::Bar` causes NameError.
# So we try to spot the sub-node that causes the NameError by using
# `NameError#name`.
case @node.type
when :COLON2
subnodes = []
node = @node
while node.type == :COLON2
node2, const = node.children
subnodes << node if const == @name
node = node2
end
if node.type == :CONST || node.type == :COLON3
if node.children.first == @name
subnodes << node
end
# If we found only one sub-node whose name is equal to @name, use it
return nil if subnodes.size != 1
@node = subnodes.first
else
# Do nothing; opt_getconstant_path is used only when the const base is
# NODE_CONST (`Foo`) or NODE_COLON3 (`::Foo`)
end
when :constant_path_node
subnodes = []
node = @node
begin
subnodes << node if node.name == @name
end while (node = node.parent).is_a?(Prism::ConstantPathNode)
if node.is_a?(Prism::ConstantReadNode) && node.name == @name
subnodes << node
end
# If we found only one sub-node whose name is equal to @name, use it
return nil if subnodes.size != 1
@node = subnodes.first
end
case @node.type
when :CALL, :QCALL
case @point_type
when :name
spot_call_for_name
when :args
spot_call_for_args
end
when :ATTRASGN
case @point_type
when :name
spot_attrasgn_for_name
when :args
spot_attrasgn_for_args
end
when :OPCALL
case @point_type
when :name
spot_opcall_for_name
when :args
spot_opcall_for_args
end
when :FCALL
case @point_type
when :name
spot_fcall_for_name
when :args
spot_fcall_for_args
end
when :VCALL
spot_vcall
when :OP_ASGN1
case @point_type
when :name
spot_op_asgn1_for_name
when :args
spot_op_asgn1_for_args
end
when :OP_ASGN2
case @point_type
when :name
spot_op_asgn2_for_name
when :args
spot_op_asgn2_for_args
end
when :CONST
spot_vcall
when :COLON2
spot_colon2
when :COLON3
spot_vcall
when :OP_CDECL
spot_op_cdecl
when :DEFN
raise NotImplementedError if @point_type != :name
spot_defn
when :DEFS
raise NotImplementedError if @point_type != :name
spot_defs
when :LAMBDA
spot_lambda
when :ITER
spot_iter
when :call_node
case @point_type
when :name
prism_spot_call_for_name
when :args
prism_spot_call_for_args
end
when :local_variable_operator_write_node
case @point_type
when :name
prism_spot_local_variable_operator_write_for_name
when :args
prism_spot_local_variable_operator_write_for_args
end
when :call_operator_write_node
case @point_type
when :name
prism_spot_call_operator_write_for_name
when :args
prism_spot_call_operator_write_for_args
end
when :index_operator_write_node
case @point_type
when :name
prism_spot_index_operator_write_for_name
when :args
prism_spot_index_operator_write_for_args
end
when :constant_read_node
prism_spot_constant_read
when :constant_path_node
prism_spot_constant_path
when :constant_path_operator_write_node
prism_spot_constant_path_operator_write
when :def_node
case @point_type
when :name
prism_spot_def_for_name
when :args
raise NotImplementedError
end
when :lambda_node
case @point_type
when :name
prism_spot_lambda_for_name
when :args
raise NotImplementedError
end
when :block_node
case @point_type
when :name
prism_spot_block_for_name
when :args
raise NotImplementedError
end
end
if @snippet && @beg_column && @end_column && @beg_column < @end_column
return {
first_lineno: @beg_lineno,
first_column: @beg_column,
last_lineno: @end_lineno,
last_column: @end_column,
snippet: @snippet,
script_lines: @node.script_lines,
}
else
return nil
end
rescue NonAscii
nil
end
No documentation available