Class: SorbetErb::CodeProcessor
- Inherits:
-
Object
- Object
- SorbetErb::CodeProcessor
- Extended by:
- T::Sig
- Includes:
- AST::Processor::Mixin
- Defined in:
- lib/sorbet_erb/code_extractor.rb
Constant Summary collapse
- LOCALS_PREFIX =
'locals:'- LOCALS_SIG_PREFIX =
'locals_sig:'- CONTROLLER_CLASS_PREFIX =
'controller_class:'
Instance Attribute Summary collapse
-
#controller_class ⇒ Object
Returns the value of attribute controller_class.
-
#locals ⇒ Object
Returns the value of attribute locals.
-
#locals_sig ⇒ Object
Returns the value of attribute locals_sig.
-
#output ⇒ Object
Returns the value of attribute output.
Instance Method Summary collapse
- #handler_missing(node) ⇒ Object
-
#initialize ⇒ CodeProcessor
constructor
A new instance of CodeProcessor.
- #on_code(node) ⇒ Object
- #on_erb(node) ⇒ Object
Constructor Details
#initialize ⇒ CodeProcessor
Returns a new instance of CodeProcessor.
49 50 51 52 53 54 |
# File 'lib/sorbet_erb/code_extractor.rb', line 49 def initialize @output = T.let([], T::Array[String]) @locals = T.let(nil, T.nilable(String)) @locals_sig = T.let(nil, T.nilable(String)) @controller_class = T.let(nil, T.nilable(String)) end |
Instance Attribute Details
#controller_class ⇒ Object
Returns the value of attribute controller_class.
46 47 48 |
# File 'lib/sorbet_erb/code_extractor.rb', line 46 def controller_class @controller_class end |
#locals ⇒ Object
Returns the value of attribute locals.
40 41 42 |
# File 'lib/sorbet_erb/code_extractor.rb', line 40 def locals @locals end |
#locals_sig ⇒ Object
Returns the value of attribute locals_sig.
43 44 45 |
# File 'lib/sorbet_erb/code_extractor.rb', line 43 def locals_sig @locals_sig end |
#output ⇒ Object
Returns the value of attribute output.
37 38 39 |
# File 'lib/sorbet_erb/code_extractor.rb', line 37 def output @output end |
Instance Method Details
#handler_missing(node) ⇒ Object
57 58 59 60 61 |
# File 'lib/sorbet_erb/code_extractor.rb', line 57 def handler_missing(node) # Some children may be strings, so only look for AST nodes children = node.children.select { |c| c.is_a?(BetterHtml::AST::Node) } process_all(children) end |
#on_code(node) ⇒ Object
89 90 91 |
# File 'lib/sorbet_erb/code_extractor.rb', line 89 def on_code(node) @output += node.children end |
#on_erb(node) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sorbet_erb/code_extractor.rb', line 64 def on_erb(node) indicator_node = node.children.compact.find { |c| c.type == :indicator } code_node = node.children.compact.find { |c| c.type == :code } return process(code_node) if indicator_node.nil? indicator = indicator_node.children.first case indicator when '#' # Ignore comments unless they declare a recognized annotation. code_text = code_node.children.first.strip if code_text.start_with?(LOCALS_PREFIX) # No need to parse the locals @locals = code_text.delete_prefix(LOCALS_PREFIX).strip elsif code_text.start_with?(LOCALS_SIG_PREFIX) @locals_sig = code_text.delete_prefix(LOCALS_SIG_PREFIX).strip elsif code_text.start_with?(CONTROLLER_CLASS_PREFIX) @controller_class = code_text.delete_prefix(CONTROLLER_CLASS_PREFIX).strip end else process_all(node.children) end end |