Module: RuboCop::Cop::Style::RbsInline::MissingClassAnnotation
- Includes:
- DataStructHelper
- Included in:
- MissingDataClassAnnotation, MissingStructClassAnnotation
- Defined in:
- lib/rubocop/cop/style/rbs_inline/missing_class_annotation.rb,
sig/rubocop/cop/style/rbs_inline/missing_class_annotation.rbs
Overview
Shared behavior for cops that ensure struct-like class attributes have
inline type annotations (Data.define / Struct.new).
The including cop matches the definition node (e.g. via struct_like_class?)
and calls #check_missing_annotations.
Instance Method Summary collapse
- #add_offenses_for_folded(node) ⇒ void
- #check_missing_annotations(node) ⇒ void
- #check_multiline(node) ⇒ void
- #correct_multiline(corrector, node, arg) ⇒ void
- #insert_annotation(corrector, node, arg) ⇒ void
Methods included from DataStructHelper
#annotation_column, #attr_argument?, #attr_arguments, #build_multiline_replacement, #find_regular_comment, #folded?, #inline_type_annotation?, #inline_type_comment, #longest_argname
Methods included from SourceCodeHelper
#annotation_range, #blank_line?, #char_at, #character_offset, #comment_at, #comment_range, #line_range, #location_to_range, #source_code_at
Methods included from ASTUtils
#end_line, #name_location, #source!, #value_to_sym
Instance Method Details
#add_offenses_for_folded(node) ⇒ void
This method returns an undefined value.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rubocop/cop/style/rbs_inline/missing_class_annotation.rb', line 29 def add_offenses_for_folded(node) #: void corrected = false attr_arguments(node).each do |arg| if corrected add_offense(arg) else replacement = build_multiline_replacement(node) loc = node.loc if loc.begin && loc.end add_offense(arg) { _1.replace(loc.begin.join(loc.end), replacement) } corrected = true end end end end |
#check_missing_annotations(node) ⇒ void
This method returns an undefined value.
18 19 20 21 22 23 24 |
# File 'lib/rubocop/cop/style/rbs_inline/missing_class_annotation.rb', line 18 def check_missing_annotations(node) #: void if folded?(node) add_offenses_for_folded(node) else check_multiline(node) end end |
#check_multiline(node) ⇒ void
This method returns an undefined value.
46 47 48 49 50 51 52 |
# File 'lib/rubocop/cop/style/rbs_inline/missing_class_annotation.rb', line 46 def check_multiline(node) #: void attr_arguments(node).each do |arg| next if inline_type_annotation?(arg.location.line) add_offense(arg) { correct_multiline(_1, node, arg) } end end |
#correct_multiline(corrector, node, arg) ⇒ void
This method returns an undefined value.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rubocop/cop/style/rbs_inline/missing_class_annotation.rb', line 57 def correct_multiline(corrector, node, arg) #: void existing_comment = find_regular_comment(arg.location.line) if existing_comment comment_text = existing_comment.text.sub(/\A#\s*/, "") corrector.replace(existing_comment.source_range, "#: untyped -- #{comment_text}") else insert_annotation(corrector, node, arg) end end |
#insert_annotation(corrector, node, arg) ⇒ void
This method returns an undefined value.
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rubocop/cop/style/rbs_inline/missing_class_annotation.rb', line 71 def insert_annotation(corrector, node, arg) #: void line = arg.location.line line_source = source_code_at(line) content_end_col = line_source.rstrip.length padding = [annotation_column(node) - content_end_col, 1].max || raise line_begin = processed_source.buffer.line_range(line).begin_pos insert_pos = line_begin + content_end_col corrector.insert_before(range_between(insert_pos, insert_pos), "#{" " * padding}#: untyped") end |