Class: RuboCop::Cop::Style::RbsInline::ParametersSeparator

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/style/rbs_inline/parameters_separator.rb,
sig/rubocop/cop/style/rbs_inline/parameters_separator.rbs

Overview

IRB::Inline expects annotations comments for parameters are separated with :or allows annotation comments. This cop checks for comments that do not match the expected pattern.

Examples:

# bad
# @rbs param String

# bad
# @rbs :param String

# good
# @rbs param: String

# good
# @rbs %a{pure}

Constant Summary collapse

MSG =

Returns:

  • (::String)
'Use `:` as a separator between parameter name and type.'
RBS_INLINE_KEYWORDS =

Returns:

  • (Array[String])
%w[inherits override use module-self generic skip module class].freeze
RBS_INLINE_REGEXP_KEYWORDS =

: Array

Returns:

  • (Array[Regexp])
[/%a{(\w|-)+}/, /%a\((\w|-)+\)/, /%a\[(\w|-)+\]/].freeze

Instance Method Summary collapse

Instance Method Details

#corrected_keyword(keyword) ⇒ String

Parameters:

  • keyword (String)

Returns:

  • (String)


80
81
82
# File 'lib/rubocop/cop/style/rbs_inline/parameters_separator.rb', line 80

def corrected_keyword(keyword) #: String
  "#{keyword.delete_prefix(':')}:"
end

#invalid_location_for(comment, matched) ⇒ Parser::Source::Range

Parameters:

Returns:

  • (Parser::Source::Range)


62
63
64
65
66
# File 'lib/rubocop/cop/style/rbs_inline/parameters_separator.rb', line 62

def invalid_location_for(comment, matched) #: Parser::Source::Range
  range = comment.source_range
  prefix = matched[:prefix] || raise
  range_between(range.begin_pos + prefix.length, range.end_pos)
end

#keyword_range(comment, matched) ⇒ Parser::Source::Range

Parameters:

Returns:

  • (Parser::Source::Range)


70
71
72
73
74
75
76
77
# File 'lib/rubocop/cop/style/rbs_inline/parameters_separator.rb', line 70

def keyword_range(comment, matched) #: Parser::Source::Range
  prefix = matched[:prefix] || raise
  keyword = matched[:keyword] || raise

  range = comment.source_range
  keyword_begin = range.begin_pos + prefix.length
  range_between(keyword_begin, keyword_begin + keyword.length)
end

#on_new_investigationvoid

This method returns an undefined value.

: void



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rubocop/cop/style/rbs_inline/parameters_separator.rb', line 32

def on_new_investigation #: void
  processed_source.comments.each do |comment|
    matched = comment.text.match(/\A(?<prefix>#\s+@rbs\s+)(?<keyword>\S+)/)

    next unless matched
    next if valid_rbs_inline_comment?(matched[:keyword])

    add_offense(invalid_location_for(comment, matched)) do |corrector|
      keyword = matched[:keyword] || raise
      corrector.replace(keyword_range(comment, matched), corrected_keyword(keyword))
    end
  end
end

#valid_rbs_inline_comment?(matched) ⇒ Boolean

Parameters:

  • matched (String, nil)

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/style/rbs_inline/parameters_separator.rb', line 49

def valid_rbs_inline_comment?(matched) #: bool
  return true if matched.nil?
  return true if RBS_INLINE_KEYWORDS.include?(matched)
  return true if RBS_INLINE_REGEXP_KEYWORDS.any? { |regexp| matched =~ regexp }
  return true if matched.end_with?(':')
  # method type signature, e.g. `# @rbs (Integer) -> String` or `# @rbs [T] (T) -> T`
  return true if matched.start_with?('(', '[')

  false
end