Module: RuboCop::Sorbet::RBSParser

Defined in:
lib/rubocop/sorbet/rbs_parser.rb

Overview

Pure helpers for parsing RBS inline-comment signatures (#: / #|).

Every public method takes a processed_source and/or comment nodes explicitly, so this module is usable both from a Cop and from a plain service class such as RuboCop::Cop::Sorbet::EnforceSignatures::RBSSignatureChecker. No Cop::Base state is required.

rbs_signatures_before returns Signature objects that encapsulate one RBS overload each; ask them for return_type, return_type_range, and void? rather than reaching back into the parser for those facts.

Defined Under Namespace

Classes: Signature

Constant Summary collapse

RBS_SIGNATURE_PREFIX =

#: begins an RBS signature (each repeated #: line is a new overload).

/\A#\s*:/
RBS_CONTINUATION_PREFIX =

#| continues the preceding #: signature (e.g. multiline params/returns).

/\A#\s*\|/

Class Method Summary collapse

Class Method Details

.rbs_signatures_before(processed_source, node) ⇒ Object

The RBS signatures attached to node, one Signature per overload. Climbs private/public/other send wrappers, collects the contiguous comment block above the unwrapped node, and groups the RBS comments. A signature separated from the method by a blank line is not attached.



27
28
29
30
31
# File 'lib/rubocop/sorbet/rbs_parser.rb', line 27

def rbs_signatures_before(processed_source, node)
  node = node.parent while node.parent&.send_type?
  rbs_signature_groups(comments_above(processed_source, node))
    .map { |group| Signature.new(processed_source, group) }
end