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

Instance Method Details

#annotation_range(annotation) ⇒ Parser::Source::Range?

Convert RBS::Inline annotation to Parser::Source::Range

Parameters:

  • annotation (RBS::Inline::AST::Annotations::t)

Returns:

  • (Parser::Source::Range, nil)


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

Parameters:

  • line_number (Integer)

Returns:

  • (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?

Parameters:

  • pos (Integer)

Returns:

  • (String, nil)


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

Parameters:

  • byte_offset (Integer)

Returns:

  • (Integer)


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?

Parameters:

  • line (Integer)

Returns:



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

Parameters:

  • comment (Prism::Comment)

Returns:

  • (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

Parameters:

  • line_number (Integer)

Returns:

  • (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

Parameters:

  • location (Prism::Location)

Returns:

  • (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

Parameters:

  • line_number (Integer)

Returns:

  • (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