Module: RuboCop::Cop::Style::RbsInline::SourceCodeHelper

Includes:
RangeHelp
Included in:
DataClassCommentAlignment, EmbeddedRbsSpacing, InvalidTypes, MethodCommentSpacing, MissingDataClassAnnotation, RedundantAnnotationWithSkip, RedundantInstanceVariableAnnotation, RedundantTypeAnnotation, UnmatchedAnnotations, VariableCommentSpacing
Defined in:
lib/rubocop/cop/style/rbs_inline/source_code_helper.rb,
sig/rubocop/cop/style/rbs_inline/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)


62
63
64
65
66
67
68
69
# File 'lib/rubocop/cop/style/rbs_inline/source_code_helper.rb', line 62

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)


38
39
40
41
# File 'lib/rubocop/cop/style/rbs_inline/source_code_helper.rb', line 38

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)


23
24
25
# File 'lib/rubocop/cop/style/rbs_inline/source_code_helper.rb', line 23

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
19
20
# File 'lib/rubocop/cop/style/rbs_inline/source_code_helper.rb', line 14

def character_offset(byte_offset) #: Integer
  source = processed_source.buffer.source.dup.force_encoding('ASCII')
  text = source[...byte_offset] or raise
  text.force_encoding(processed_source.buffer.source.encoding).size
rescue StandardError
  byte_offset
end

#comment_at(line) ⇒ Parser::Source::Comment?

Parameters:

  • line (Integer)

Returns:



28
29
30
# File 'lib/rubocop/cop/style/rbs_inline/source_code_helper.rb', line 28

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)


56
57
58
# File 'lib/rubocop/cop/style/rbs_inline/source_code_helper.rb', line 56

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)


44
45
46
# File 'lib/rubocop/cop/style/rbs_inline/source_code_helper.rb', line 44

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)


50
51
52
# File 'lib/rubocop/cop/style/rbs_inline/source_code_helper.rb', line 50

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)


33
34
35
# File 'lib/rubocop/cop/style/rbs_inline/source_code_helper.rb', line 33

def source_code_at(line_number) #: String
  processed_source.buffer.source.lines[line_number - 1] || ''
end