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. rbs_annotation_after returns the trailing RBS
annotation attached to an expression and its comment node.
Defined Under Namespace
Classes: Signature
Constant Summary collapse
- RBS_SIGNATURE_PREFIX =
#:begins an RBS signature (each repeated#:line is a new overload). /\A#:/- RBS_CONTINUATION_PREFIX =
#|continues the preceding#:signature (e.g. multiline params/returns). /\A#\|/
Class Method Summary collapse
-
.rbs_annotation_after(processed_source, node) ⇒ Object
The trailing RBS annotation attached to
node, as[comment, text]. -
.rbs_signatures_before(processed_source, node) ⇒ Object
The RBS signatures attached to
node, oneSignatureper overload.
Class Method Details
.rbs_annotation_after(processed_source, node) ⇒ Object
The trailing RBS annotation attached to node, as [comment, text].
The comment must follow the expression on its final line with only
horizontal whitespace between them.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rubocop/sorbet/rbs_parser.rb', line 36 def rbs_annotation_after(processed_source, node) last_line = node.source_range.last_line comment = processed_source.each_comment_in_lines(last_line..last_line).find do |candidate| next false if candidate.source_range.begin_pos < node.source_range.end_pos gap = processed_source.buffer.source[node.source_range.end_pos...candidate.source_range.begin_pos] gap.match?(/\A[ \t]*\z/) end return unless comment&.text&.match?(RBS_SIGNATURE_PREFIX) annotation = comment.text.sub(RBS_SIGNATURE_PREFIX, "").strip return if annotation.empty? [comment, annotation] end |
.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 |