diff options
| author | Vinicius Stock <vinistock@users.noreply.github.com> | 2023-11-21 11:39:23 -0500 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2023-11-21 16:39:27 +0000 |
| commit | 7a9bb6d2a7d3d75cb8c3d83d09819d5bcea09796 (patch) | |
| tree | abfc12f705ce6d5202f77e4f9f971819cb60e826 | |
| parent | 8966d06b96f2c6396d4c7e58b7a51c9bdebbb694 (diff) | |
[ruby/prism] Fix constant path full name when parent is not a
constant
(https://github.com/ruby/prism/pull/1742)
* Raise if constant path parts contains nodes that can't be used to build full name
* Fix typo in constant path error documentation
Co-authored-by: Tim Morgan <tim@timmorgan.org>
---------
https://github.com/ruby/prism/commit/d73a053262
Co-authored-by: Tim Morgan <tim@timmorgan.org>
| -rw-r--r-- | lib/prism/node_ext.rb | 12 | ||||
| -rw-r--r-- | test/prism/constant_path_node_test.rb | 27 |
2 files changed, 39 insertions, 0 deletions
diff --git a/lib/prism/node_ext.rb b/lib/prism/node_ext.rb index 4febc615c1..5fc13eb06d 100644 --- a/lib/prism/node_ext.rb +++ b/lib/prism/node_ext.rb @@ -74,6 +74,14 @@ module Prism end class ConstantPathNode < Node + # An error class raised when dynamic parts are found while computing a + # constant path's full name. For example: + # Foo::Bar::Baz -> does not raise because all parts of the constant path are + # simple constants + # var::Bar::Baz -> raises because the first part of the constant path is a + # local variable + class DynamicPartsInConstantPathError < StandardError; end + # Returns the list of parts for the full name of this constant path. # For example: [:Foo, :Bar] def full_name_parts @@ -85,6 +93,10 @@ module Prism current = current.parent end + unless current.is_a?(ConstantReadNode) + raise DynamicPartsInConstantPathError, "Constant path contains dynamic parts. Cannot compute full name" + end + parts.unshift(current&.name || :"") end diff --git a/test/prism/constant_path_node_test.rb b/test/prism/constant_path_node_test.rb index 5bb9830687..9842be0c49 100644 --- a/test/prism/constant_path_node_test.rb +++ b/test/prism/constant_path_node_test.rb @@ -15,6 +15,33 @@ module Prism assert_equal("Foo::Bar::Baz::Qux", constant_path.full_name) end + def test_full_name_for_constant_path_with_self + source = <<~RUBY + self:: # comment + Bar::Baz:: + Qux + RUBY + + constant_path = Prism.parse(source).value.statements.body.first + assert_raise(ConstantPathNode::DynamicPartsInConstantPathError) do + constant_path.full_name + end + end + + def test_full_name_for_constant_path_with_variable + source = <<~RUBY + foo:: # comment + Bar::Baz:: + Qux + RUBY + + constant_path = Prism.parse(source).value.statements.body.first + + assert_raise(ConstantPathNode::DynamicPartsInConstantPathError) do + constant_path.full_name + end + end + def test_full_name_for_constant_path_target source = <<~RUBY Foo:: # comment |
