Class: RbsActivesupport::Parser

Inherits:
RBS::Prototype::RB
  • Object
show all
Defined in:
lib/rbs_activesupport/parser.rb,
lib/rbs_activesupport/parser/comment_parser.rb,
sig/rbs_activesupport/parser.rbs,
sig/rbs_activesupport/parser/comment_parser.rbs

Defined Under Namespace

Classes: CommentParser, MethodCall

Constant Summary collapse

METHODS =

@rbs! type t = :class_attribute | :delegate | :cattr_accessor | :mattr_accessor | :cattr_reader | :mattr_reader | :cattr_writer | :mattr_writer | :include

Returns:

  • (Array[t])
%i[
  class_attribute delegate cattr_accessor mattr_accessor cattr_reader mattr_reader cattr_writer mattr_writer include
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parse_included_block: false) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • parse_included_block: (Object) (defaults to: false)


52
53
54
55
56
57
58
# File 'lib/rbs_activesupport/parser.rb', line 52

def initialize(parse_included_block: false) #: void
  super()
  @comment_parser = CommentParser.new
  @parse_included_block = parse_included_block
  @method_calls = Hash.new { |hash, key| hash[key] = [] }
  @in_included_block = false
end

Instance Attribute Details

#comment_parserCommentParser (readonly)

: CommentParser

Returns:



46
47
48
# File 'lib/rbs_activesupport/parser.rb', line 46

def comment_parser
  @comment_parser
end

#method_callsHash[RBS::Namespace, Array[MethodCall]] (readonly)

: Hash[RBS::Namespace, Array[MethodCall]]

Returns:



47
48
49
# File 'lib/rbs_activesupport/parser.rb', line 47

def method_calls
  @method_calls
end

#parse_included_blockBoolean (readonly)

: bool

Returns:

  • (Boolean)


48
49
50
# File 'lib/rbs_activesupport/parser.rb', line 48

def parse_included_block
  @parse_included_block
end

Instance Method Details

#in_included_block?Boolean

: bool

Returns:

  • (Boolean)


106
107
108
# File 'lib/rbs_activesupport/parser.rb', line 106

def in_included_block? #: bool
  @in_included_block
end

#parse(string) ⇒ void

This method returns an undefined value.

Parameters:

  • string (String)


61
62
63
64
# File 'lib/rbs_activesupport/parser.rb', line 61

def parse(string) #: void
  comment_parser.parse(string)
  super
end

#private?(decls) ⇒ Boolean

Parameters:

  • decls (Array[RBS::AST::Declarations::t | RBS::AST::Members::t])

Returns:

  • (Boolean)


102
103
104
# File 'lib/rbs_activesupport/parser.rb', line 102

def private?(decls) #: bool
  current_accessibility(decls) == private
end

#process(node, decls:, comments:, context:) ⇒ void

This method returns an undefined value.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rbs_activesupport/parser.rb', line 67

def process(node, decls:, comments:, context:) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  case node.type
  when :DEFN, :DEFS
    # ignore definitions inside methods
  when :FCALL, :VCALL
    args = node.children[1]&.children || []
    case node.children[0]
    when *METHODS
      @method_calls[context.namespace] << MethodCall.new(node.children[0], args, private?(decls),
                                                         included: in_included_block?,
                                                         trailing_comment: trailing_comment_for(node))
    else
      process_orig(node, decls:, comments:, context:)
    end
  when :ITER
    call = node.children[0]
    if call.type == :FCALL && call.children[0] == :included && parse_included_block && !in_included_block?
      body = node.children[1].children[2]
      with_included_block do
        process(body, decls:, comments:, context:)
      end
    else
      process_orig(node, decls:, comments:, context:)
    end
  else
    process_orig(node, decls:, comments:, context:)
  end
end

#process_origObject

: Array # steep:ignore IncompatibleAssignment



44
# File 'lib/rbs_activesupport/parser.rb', line 44

alias process_orig process

#trailing_comment_for(node) ⇒ String?

Parameters:

  • node (RubyVM::AbstractSyntaxTree::Node)

Returns:

  • (String, nil)


97
98
99
# File 'lib/rbs_activesupport/parser.rb', line 97

def trailing_comment_for(node) #: String?
  comment_parser.trailing_comments[node.last_lineno]
end

#with_included_block { ... } ⇒ void

This method returns an undefined value.

Yields:

Yield Returns:

  • (void)


111
112
113
114
115
116
# File 'lib/rbs_activesupport/parser.rb', line 111

def with_included_block(&block) #: void
  @in_included_block = true
  block.call
ensure
  @in_included_block = false
end