Class: SorbetErb::CodeProcessor

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeCodeProcessor

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_classObject

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

#localsObject

Returns the value of attribute locals.



40
41
42
# File 'lib/sorbet_erb/code_extractor.rb', line 40

def locals
  @locals
end

#locals_sigObject

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

#outputObject

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