diff options
author | Vinicius Stock <[email protected]> | 2023-10-03 15:29:29 -0400 |
---|---|---|
committer | git <[email protected]> | 2023-10-06 01:57:34 +0000 |
commit | 69b024d7ccb8d42bb0387a244dce4d444f619987 (patch) | |
tree | ec96df436aaa15ee9647bda8e43e330eaceadace /lib/prism/node_ext.rb | |
parent | 58fc45325f25b64526ef2c467c37537a69aac4ac (diff) |
[ruby/prism] Add full_name to ConstantPathNode and ConstantPathTargetNode
https://github.com/ruby/prism/commit/b390553028
Diffstat (limited to 'lib/prism/node_ext.rb')
-rw-r--r-- | lib/prism/node_ext.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/prism/node_ext.rb b/lib/prism/node_ext.rb index 08d9e9f36a..264d3a5c1e 100644 --- a/lib/prism/node_ext.rb +++ b/lib/prism/node_ext.rb @@ -52,4 +52,48 @@ module Prism o end end + + class ConstantReadNode < 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.name + end + end + + class ConstantPathNode < Node + # Returns the list of parts for the full name of this constant path. For example: [:Foo, :Bar] + def full_name_parts + parts = [child.name] + current = parent + + while current.is_a?(ConstantPathNode) + parts.unshift(current.child.name) + current = current.parent + end + + parts.unshift(current&.name || :"") + end + + # Returns the full name of this constant path. For example: "Foo::Bar" + def full_name + full_name_parts.join("::") + end + end + + class ConstantPathTargetNode < Node + # 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) + end + + # Returns the full name of this constant path. For example: "Foo::Bar" + def full_name + full_name_parts.join("::") + end + end end |