Module: Rfmt::PrismNodeExtractor
- Included in:
- PrismBridge
- Defined in:
- lib/rfmt/prism_node_extractor.rb
Overview
PrismNodeExtractor provides safe methods to extract information from Prism nodes This module encapsulates the logic for accessing Prism node properties, making the code resilient to Prism API changes
Instance Method Summary collapse
-
#extract_class_or_module_name(node) ⇒ String?
Extract full name from class or module node (handles namespaced names like Foo::Bar::Baz).
-
#extract_literal_value(node) ⇒ String?
Extract value from a literal node (Integer, Float, Symbol).
-
#extract_message_name(call_node) ⇒ String?
Extract message name from a call node.
-
#extract_node_name(node) ⇒ String?
Extract the name from a node.
-
#extract_parameter_count(def_node) ⇒ Integer
Extract parameter count from a method definition node.
-
#extract_string_content(string_node) ⇒ String?
Extract content from a string node.
-
#extract_superclass_name(class_node) ⇒ String?
Extract superclass name from a class node.
Instance Method Details
#extract_class_or_module_name(node) ⇒ String?
Extract full name from class or module node (handles namespaced names like Foo::Bar::Baz)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rfmt/prism_node_extractor.rb', line 20 def extract_class_or_module_name(node) return nil unless node.respond_to?(:constant_path) cp = node.constant_path return node.name.to_s if cp.nil? case cp when Prism::ConstantReadNode cp.name.to_s when Prism::ConstantPathNode if cp.respond_to?(:full_name) cp.full_name.to_s elsif cp.respond_to?(:slice) cp.slice else cp.location.slice end else node.name.to_s end end |
#extract_literal_value(node) ⇒ String?
Extract value from a literal node (Integer, Float, Symbol)
109 110 111 112 113 |
# File 'lib/rfmt/prism_node_extractor.rb', line 109 def extract_literal_value(node) return nil unless node.respond_to?(:value) node.value.to_s end |
#extract_message_name(call_node) ⇒ String?
Extract message name from a call node
91 92 93 94 95 |
# File 'lib/rfmt/prism_node_extractor.rb', line 91 def (call_node) return nil unless call_node.respond_to?(:message) call_node..to_s end |
#extract_node_name(node) ⇒ String?
Extract the name from a node
11 12 13 14 15 |
# File 'lib/rfmt/prism_node_extractor.rb', line 11 def extract_node_name(node) return nil unless node.respond_to?(:name) node.name.to_s end |
#extract_parameter_count(def_node) ⇒ Integer
Extract parameter count from a method definition node
80 81 82 83 84 85 86 |
# File 'lib/rfmt/prism_node_extractor.rb', line 80 def extract_parameter_count(def_node) return 0 unless def_node.respond_to?(:parameters) return 0 if def_node.parameters.nil? return 0 unless def_node.parameters.respond_to?(:child_nodes) def_node.parameters.child_nodes.compact.length end |
#extract_string_content(string_node) ⇒ String?
Extract content from a string node
100 101 102 103 104 |
# File 'lib/rfmt/prism_node_extractor.rb', line 100 def extract_string_content(string_node) return nil unless string_node.respond_to?(:content) string_node.content end |
#extract_superclass_name(class_node) ⇒ String?
Extract superclass name from a class node
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rfmt/prism_node_extractor.rb', line 45 def extract_superclass_name(class_node) return nil unless class_node.respond_to?(:superclass) sc = class_node.superclass return nil if sc.nil? case sc when Prism::ConstantReadNode sc.name.to_s when Prism::ConstantPathNode # Try full_name first, fall back to slice for original source if sc.respond_to?(:full_name) sc.full_name.to_s elsif sc.respond_to?(:slice) sc.slice else sc.location.slice end when Prism::CallNode # Handle cases like ActiveRecord::Migration[8.1] # Use slice to get the original source text sc.slice else # Fallback: try to get original source text if sc.respond_to?(:slice) sc.slice else sc.location.slice end end end |