Class: RbsInlineData::Parser

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/rbs_inline_data/parser.rb

Defined Under Namespace

Classes: Comments

Constant Summary collapse

TypedDefinition =
Data.define(
  :class_name, #:: String
  :fields #:: Array[RbsInlineData::Parser::TypedField]
)
TypedField =
Data.define(
  :field_name, #:: String
  :type #:: String
)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definitions, comments) ⇒ Parser

rubocop:disable Lint/MissingSuper

(Array, RbsInlineData::Parser::Comments) -> void



45
46
47
48
49
# File 'lib/rbs_inline_data/parser.rb', line 45

def initialize(definitions, comments)
  @definitions = definitions
  @comments = comments
  @surronding_class_or_module = []
end

Class Method Details

.parse(result) ⇒ Object

(Prism::ParseResult) -> Array



53
54
55
56
57
58
59
60
# File 'lib/rbs_inline_data/parser.rb', line 53

def self.parse(result)
  # @type var definitions: Array[RbsInlineData::Parser::TypedDefinition]
  definitions = []
  comments = Comments.from_prism_comments(result.comments)
  instance = new(definitions, comments)
  result.value.accept(instance)
  definitions
end

Instance Method Details

#visit_class_node(node) ⇒ Object



63
64
65
# File 'lib/rbs_inline_data/parser.rb', line 63

def visit_class_node(node)
  record_surrounding_class_or_module(node) { super }
end

#visit_constant_write_node(node) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/rbs_inline_data/parser.rb', line 73

def visit_constant_write_node(node)
  if define_data?(node)
    definition = extract_definition(node)
    @definitions << definition if definition
  end

  super
end

#visit_module_node(node) ⇒ Object



68
69
70
# File 'lib/rbs_inline_data/parser.rb', line 68

def visit_module_node(node)
  record_surrounding_class_or_module(node) { super }
end