Skip to content

Commit d3063cd

Browse files
committed
Remove a branch for Ruby 3.1
1 parent f154892 commit d3063cd

File tree

2 files changed

+40
-60
lines changed

2 files changed

+40
-60
lines changed

lib/error_highlight/base.rb

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -122,56 +122,51 @@ def initialize(node, point_type: :name, name: nil)
122122
end
123123
end
124124

125-
OPT_GETCONSTANT_PATH = (RUBY_VERSION.split(".").map {|s| s.to_i } <=> [3, 2]) >= 0
126-
private_constant :OPT_GETCONSTANT_PATH
127-
128125
def spot
129126
return nil unless @node
130127

131-
if OPT_GETCONSTANT_PATH
132-
# In Ruby 3.2 or later, a nested constant access (like `Foo::Bar::Baz`)
133-
# is compiled to one instruction (opt_getconstant_path).
134-
# @node points to the node of the whole `Foo::Bar::Baz` even if `Foo`
135-
# or `Foo::Bar` causes NameError.
136-
# So we try to spot the sub-node that causes the NameError by using
137-
# `NameError#name`.
138-
case @node.type
139-
when :COLON2
140-
subnodes = []
141-
node = @node
142-
while node.type == :COLON2
143-
node2, const = node.children
144-
subnodes << node if const == @name
145-
node = node2
146-
end
147-
if node.type == :CONST || node.type == :COLON3
148-
if node.children.first == @name
149-
subnodes << node
150-
end
151-
152-
# If we found only one sub-node whose name is equal to @name, use it
153-
return nil if subnodes.size != 1
154-
@node = subnodes.first
155-
else
156-
# Do nothing; opt_getconstant_path is used only when the const base is
157-
# NODE_CONST (`Foo`) or NODE_COLON3 (`::Foo`)
158-
end
159-
when :constant_path_node
160-
subnodes = []
161-
node = @node
162-
163-
begin
164-
subnodes << node if node.name == @name
165-
end while (node = node.parent).is_a?(Prism::ConstantPathNode)
166-
167-
if node.is_a?(Prism::ConstantReadNode) && node.name == @name
128+
# In Ruby 3.2 or later, a nested constant access (like `Foo::Bar::Baz`)
129+
# is compiled to one instruction (opt_getconstant_path).
130+
# @node points to the node of the whole `Foo::Bar::Baz` even if `Foo`
131+
# or `Foo::Bar` causes NameError.
132+
# So we try to spot the sub-node that causes the NameError by using
133+
# `NameError#name`.
134+
case @node.type
135+
when :COLON2
136+
subnodes = []
137+
node = @node
138+
while node.type == :COLON2
139+
node2, const = node.children
140+
subnodes << node if const == @name
141+
node = node2
142+
end
143+
if node.type == :CONST || node.type == :COLON3
144+
if node.children.first == @name
168145
subnodes << node
169146
end
170147

171148
# If we found only one sub-node whose name is equal to @name, use it
172149
return nil if subnodes.size != 1
173150
@node = subnodes.first
151+
else
152+
# Do nothing; opt_getconstant_path is used only when the const base is
153+
# NODE_CONST (`Foo`) or NODE_COLON3 (`::Foo`)
174154
end
155+
when :constant_path_node
156+
subnodes = []
157+
node = @node
158+
159+
begin
160+
subnodes << node if node.name == @name
161+
end while (node = node.parent).is_a?(Prism::ConstantPathNode)
162+
163+
if node.is_a?(Prism::ConstantReadNode) && node.name == @name
164+
subnodes << node
165+
end
166+
167+
# If we found only one sub-node whose name is equal to @name, use it
168+
return nil if subnodes.size != 1
169+
@node = subnodes.first
175170
end
176171

177172
case @node.type

test/test_error_highlight.rb

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -891,27 +891,13 @@ def test_COLON2_4
891891
end
892892
end
893893

894-
if ErrorHighlight.const_get(:Spotter).const_get(:OPT_GETCONSTANT_PATH)
895-
def test_COLON2_5
896-
# Unfortunately, we cannot identify which `NotDefined` caused the NameError
897-
assert_error_message(NameError, <<~END) do
898-
uninitialized constant ErrorHighlightTest::NotDefined
899-
END
900-
901-
ErrorHighlightTest::NotDefined::NotDefined
902-
end
903-
end
904-
else
905-
def test_COLON2_5
906-
assert_error_message(NameError, <<~END) do
894+
def test_COLON2_5
895+
# Unfortunately, we cannot identify which `NotDefined` caused the NameError
896+
assert_error_message(NameError, <<~END) do
907897
uninitialized constant ErrorHighlightTest::NotDefined
898+
END
908899

909-
ErrorHighlightTest::NotDefined::NotDefined
910-
^^^^^^^^^^^^
911-
END
912-
913-
ErrorHighlightTest::NotDefined::NotDefined
914-
end
900+
ErrorHighlightTest::NotDefined::NotDefined
915901
end
916902
end
917903

@@ -1617,7 +1603,6 @@ def test_wrong_number_of_arguments_for_lambda_method
16171603
end
16181604

16191605
def test_wrong_number_of_arguments_for_define_method
1620-
v = lambda { }
16211606
lineno = __LINE__
16221607
assert_error_message(ArgumentError, <<~END) do
16231608
wrong number of arguments (given 1, expected 2) (ArgumentError)

0 commit comments

Comments
 (0)