[ruby/error_highlight] Identify which node in Foo::Bar::Baz causes a NameError

In Ruby 3.2 or later, a nested constant access like `Foo::Bar::Baz` is
compiled to one instruction by the optimization https://github.com/ruby/ruby/pull/6187

We try to spot which sub-node caues a NameError in question based on the
constant name. We will give up if the same constant name is accessed in
a nested access (`Foo::Foo`).

Fixes https://github.com/ruby/error_highlight/pull/31

0a4db7da0a
This commit is contained in:
Yusuke Endoh 2023-01-13 17:02:24 +09:00 committed by git
parent 5b36fefc4e
commit 94d6d6d93f
2 changed files with 79 additions and 0 deletions

View file

@ -818,6 +818,54 @@ uninitialized constant ErrorHighlightTest::NotDefined
end
end
def test_COLON2_3
assert_error_message(NameError, <<~END) do
uninitialized constant ErrorHighlightTest::NotDefined
ErrorHighlightTest::NotDefined::Foo
^^^^^^^^^^^^
END
ErrorHighlightTest::NotDefined::Foo
end
end
def test_COLON2_4
assert_error_message(NameError, <<~END) do
uninitialized constant ErrorHighlightTest::NotDefined
::ErrorHighlightTest::NotDefined::Foo
^^^^^^^^^^^^
END
::ErrorHighlightTest::NotDefined::Foo
end
end
if ErrorHighlight.const_get(:Spotter).const_get(:OPT_GETCONSTANT_PATH)
def test_COLON2_5
# Unfortunately, we cannot identify which `NotDefined` caused the NameError
assert_error_message(NameError, <<~END) do
uninitialized constant ErrorHighlightTest::NotDefined
END
ErrorHighlightTest::NotDefined::NotDefined
end
end
else
def test_COLON2_5
assert_error_message(NameError, <<~END) do
uninitialized constant ErrorHighlightTest::NotDefined
ErrorHighlightTest::NotDefined::NotDefined
^^^^^^^^^^^^
END
ErrorHighlightTest::NotDefined::NotDefined
end
end
end
def test_COLON3
assert_error_message(NameError, <<~END) do
uninitialized constant NotDefined