summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVinicius Stock <vinicius.stock@shopify.com>2024-02-12 13:32:00 -0500
committergit <svn-admin@ruby-lang.org>2024-02-13 14:09:19 +0000
commitdedca31804510e42a89946d81c1834c9e5950d6f (patch)
tree3b9e229da2bcffa554fcf409cb1b31ba556db044
parent2f0f95235a1de489f50c7a9dbe3477f9f7e60cd1 (diff)
[ruby/prism] Fix `full_name` for constant path targets
https://github.com/ruby/prism/commit/84c10f3a2d
-rw-r--r--lib/prism/node_ext.rb27
-rw-r--r--test/prism/constant_path_node_test.rb24
2 files changed, 49 insertions, 2 deletions
diff --git a/lib/prism/node_ext.rb b/lib/prism/node_ext.rb
index f87714e552..8b92fe1fee 100644
--- a/lib/prism/node_ext.rb
+++ b/lib/prism/node_ext.rb
@@ -94,7 +94,7 @@ module Prism
# Returns the full name of this constant. For example: "Foo"
def full_name
- name.name
+ name.to_s
end
end
@@ -135,7 +135,17 @@ module Prism
# Returns the list of parts for the full name of this constant path.
# For example: [:Foo, :Bar]
def full_name_parts
- (parent&.full_name_parts || [:""]).push(child.name)
+ parts = case parent
+ when ConstantPathNode, ConstantReadNode
+ parent.full_name_parts
+ when nil
+ [:""]
+ else
+ raise ConstantPathNode::DynamicPartsInConstantPathError,
+ "Constant path target contains dynamic parts. Cannot compute full name"
+ end
+
+ parts.push(child.name)
end
# Returns the full name of this constant path. For example: "Foo::Bar"
@@ -144,6 +154,19 @@ module Prism
end
end
+ class ConstantTargetNode < Node
+ # Returns the list of parts for the full name of this constant.
+ # For example: [:Foo]
+ def full_name_parts
+ [name]
+ end
+
+ # Returns the full name of this constant. For example: "Foo"
+ def full_name
+ name.to_s
+ end
+ end
+
class ParametersNode < Node
# Mirrors the Method#parameters method.
def signature
diff --git a/test/prism/constant_path_node_test.rb b/test/prism/constant_path_node_test.rb
index aeab6e01e9..dffb55c0ff 100644
--- a/test/prism/constant_path_node_test.rb
+++ b/test/prism/constant_path_node_test.rb
@@ -63,5 +63,29 @@ module Prism
node = Prism.parse(source).value.statements.body.first
assert_equal("::Foo::Bar::Baz::Qux", node.lefts.first.full_name)
end
+
+ def test_full_name_for_constant_path_target_with_non_constant_parent
+ source = <<~RUBY
+ self::Foo, Bar = [1, 2]
+ RUBY
+
+ constant_target = Prism.parse(source).value.statements.body.first
+ dynamic, static = constant_target.lefts
+
+ assert_raise(ConstantPathNode::DynamicPartsInConstantPathError) do
+ dynamic.full_name
+ end
+
+ assert_equal("Bar", static.full_name)
+ end
+
+ def test_full_name_for_constant_read_node
+ source = <<~RUBY
+ Bar
+ RUBY
+
+ constant = Prism.parse(source).value.statements.body.first
+ assert_equal("Bar", constant.full_name)
+ end
end
end