Class: RuboCop::Cop::Style::RbsInline::KeywordSeparator
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Style::RbsInline::KeywordSeparator
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp, CommentParser
- Defined in:
- lib/rubocop/cop/style/rbs_inline/keyword_separator.rb,
sig/rubocop/cop/style/rbs_inline/keyword_separator.rbs
Overview
IRB::Inline expects annotations comments for keywords are not separeted with :.
This cop checks for comments that do not match the expected pattern.
Constant Summary collapse
- MSG =
'Do not use `:` after the keyword.'- RBS_INLINE_KEYWORDS =
%w[inherits override use module-self generic skip module class].freeze
- NO_ARGUMENT_KEYWORDS =
overrideandskipare standalone markers that take no arguments. Even before a method definition they may not be followed by:alone, because# @rbs override:(no type) is not a valid parameter annotation. Only# @rbs override: SomeType(with a type) is valid as a parameter annotation. %w[override skip].freeze
Instance Attribute Summary
Attributes included from CommentParser
Instance Method Summary collapse
-
#collect_method_annotation_comments(def_line) ⇒ void
Collect line numbers of leading annotation comments for a method definition into @method_annotation_lines.
- #invalid_location_for(comment, matched) ⇒ Parser::Source::Range
-
#no_argument_keyword_without_type?(comment) ⇒ Boolean
Returns true when
commentis a no-argument keyword (overrideorskip) followed by:but no type. - #on_def(node) ⇒ void
- #on_defs(node) ⇒ void
-
#on_investigation_end ⇒ void
: void.
- #on_new_investigation ⇒ void
-
#valid_method_annotation?(comment) ⇒ Boolean
Returns true when comment is a leading annotation for a method definition and is not a no-argument keyword (
overrideorskip) followed by:without a type.
Methods included from CommentParser
#find_doc_style_param_annotations, #find_doc_style_return_annotation, #find_last_consecutive_comment, #find_leading_annotation, #find_method_type_signature_comments, #find_trailing_comment, #overload_type_signatures?, #parse_comments
Instance Method Details
#collect_method_annotation_comments(def_line) ⇒ void
This method returns an undefined value.
Collect line numbers of leading annotation comments for a method definition into @method_annotation_lines.
69 70 71 72 73 74 |
# File 'lib/rubocop/cop/style/rbs_inline/keyword_separator.rb', line 69 def collect_method_annotation_comments(def_line) #: void result = find_leading_annotation(def_line) or return result.comments.each do |prism_comment| @method_annotation_lines.add(prism_comment.location.start_line) end end |
#invalid_location_for(comment, matched) ⇒ Parser::Source::Range
94 95 96 97 98 |
# File 'lib/rubocop/cop/style/rbs_inline/keyword_separator.rb', line 94 def invalid_location_for(comment, matched) #: Parser::Source::Range captured = matched[0] or raise begin_pos = comment.source_range.begin_pos + captured.length - 1 range_between(begin_pos, begin_pos + 1) end |
#no_argument_keyword_without_type?(comment) ⇒ Boolean
Returns true when comment is a no-argument keyword (override or skip)
followed by : but no type.
88 89 90 |
# File 'lib/rubocop/cop/style/rbs_inline/keyword_separator.rb', line 88 def no_argument_keyword_without_type?(comment) #: bool comment.text.match?(/\A#\s+@rbs\s+(#{NO_ARGUMENT_KEYWORDS.join('|')}):\s*\z/) end |
#on_def(node) ⇒ void
This method returns an undefined value.
41 42 43 |
# File 'lib/rubocop/cop/style/rbs_inline/keyword_separator.rb', line 41 def on_def(node) #: void collect_method_annotation_comments(node.loc.line) end |
#on_defs(node) ⇒ void
This method returns an undefined value.
46 47 48 |
# File 'lib/rubocop/cop/style/rbs_inline/keyword_separator.rb', line 46 def on_defs(node) #: void collect_method_annotation_comments(node.loc.line) end |
#on_investigation_end ⇒ void
This method returns an undefined value.
: void
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rubocop/cop/style/rbs_inline/keyword_separator.rb', line 50 def on_investigation_end #: void processed_source.comments.each do |comment| matched = comment.text.match(/\A#\s+@rbs\s+(#{RBS_INLINE_KEYWORDS.join('|')}):/) next unless matched next if valid_method_annotation?(comment) range = invalid_location_for(comment, matched) add_offense(range) do |corrector| corrector.remove(range) end end super end |
#on_new_investigation ⇒ void
This method returns an undefined value.
34 35 36 37 38 |
# File 'lib/rubocop/cop/style/rbs_inline/keyword_separator.rb', line 34 def on_new_investigation #: void super parse_comments @method_annotation_lines = Set.new #: Set[Integer] end |
#valid_method_annotation?(comment) ⇒ Boolean
Returns true when comment is a leading annotation for a method definition and
is not a no-argument keyword (override or skip) followed by : without a type.
79 80 81 82 83 |
# File 'lib/rubocop/cop/style/rbs_inline/keyword_separator.rb', line 79 def valid_method_annotation?(comment) #: bool return false unless @method_annotation_lines.include?(comment.loc.line) !no_argument_keyword_without_type?(comment) end |