Module: RuboCop::Cop::Style::RbsInline::SourceCodeHelper
- Includes:
- RangeHelp
- Included in:
- DataStructHelper, EmbeddedRbsSpacing, InvalidTypes, MethodCommentSpacing, RedundantAnnotationWithSkip, RedundantInstanceVariableAnnotation, RedundantTypeAnnotation, UnmatchedAnnotations, VariableCommentSpacing
- Defined in:
- lib/rubocop/cop/style/rbs_inline/mixin/source_code_helper.rb,
sig/rubocop/cop/style/rbs_inline/mixin/source_code_helper.rbs
Overview
Utility module for accessing processed source code information
Instance Method Summary collapse
-
#annotation_range(annotation) ⇒ Parser::Source::Range?
Convert RBS::Inline annotation to Parser::Source::Range.
- #blank_line?(line_number) ⇒ Boolean
- #char_at(pos) ⇒ String?
-
#character_offset(byte_offset) ⇒ Integer
Convert byte offset to character offset.
- #comment_at(line) ⇒ Parser::Source::Comment?
-
#comment_range(comment) ⇒ Parser::Source::Range
Convert Prism::Comment to Parser::Source::Range.
- #line_range(line_number) ⇒ Parser::Source::Range
-
#location_to_range(location) ⇒ Parser::Source::Range
Convert Prism::Location to Parser::Source::Range.
- #source_code_at(line_number) ⇒ String
Instance Method Details
#annotation_range(annotation) ⇒ Parser::Source::Range?
Convert RBS::Inline annotation to Parser::Source::Range
60 61 62 63 64 65 66 67 |
# File 'lib/rubocop/cop/style/rbs_inline/mixin/source_code_helper.rb', line 60 def annotation_range(annotation) #: Parser::Source::Range? comments = annotation.source.comments first = comments.first&.location last = comments.last&.location return unless first && last range_between(character_offset(first.start_offset), character_offset(last.end_offset)) end |
#blank_line?(line_number) ⇒ Boolean
36 37 38 39 |
# File 'lib/rubocop/cop/style/rbs_inline/mixin/source_code_helper.rb', line 36 def blank_line?(line_number) #: bool line = processed_source.buffer.source.lines[line_number - 1] line.nil? || line.strip.empty? end |
#char_at(pos) ⇒ String?
21 22 23 |
# File 'lib/rubocop/cop/style/rbs_inline/mixin/source_code_helper.rb', line 21 def char_at(pos) #: String? processed_source.buffer.source[pos] end |
#character_offset(byte_offset) ⇒ Integer
Convert byte offset to character offset
14 15 16 17 18 |
# File 'lib/rubocop/cop/style/rbs_inline/mixin/source_code_helper.rb', line 14 def character_offset(byte_offset) #: Integer source = processed_source.buffer.source prefix = source.byteslice(0, byte_offset) or raise prefix.size end |
#comment_at(line) ⇒ Parser::Source::Comment?
26 27 28 |
# File 'lib/rubocop/cop/style/rbs_inline/mixin/source_code_helper.rb', line 26 def comment_at(line) #: Parser::Source::Comment? processed_source.comments.find { _1.location.line == line } end |
#comment_range(comment) ⇒ Parser::Source::Range
Convert Prism::Comment to Parser::Source::Range
54 55 56 |
# File 'lib/rubocop/cop/style/rbs_inline/mixin/source_code_helper.rb', line 54 def comment_range(comment) #: Parser::Source::Range location_to_range(comment.location) end |
#line_range(line_number) ⇒ Parser::Source::Range
42 43 44 |
# File 'lib/rubocop/cop/style/rbs_inline/mixin/source_code_helper.rb', line 42 def line_range(line_number) #: Parser::Source::Range processed_source.buffer.line_range(line_number) end |
#location_to_range(location) ⇒ Parser::Source::Range
Convert Prism::Location to Parser::Source::Range
48 49 50 |
# File 'lib/rubocop/cop/style/rbs_inline/mixin/source_code_helper.rb', line 48 def location_to_range(location) #: Parser::Source::Range range_between(character_offset(location.start_offset), character_offset(location.end_offset)) end |
#source_code_at(line_number) ⇒ String
31 32 33 |
# File 'lib/rubocop/cop/style/rbs_inline/mixin/source_code_helper.rb', line 31 def source_code_at(line_number) #: String processed_source.buffer.source.lines[line_number - 1] || "" end |