Module: RuboCop::Cop::Style::RbsInline::ClassCommentAlignment

Includes:
DataStructHelper
Included in:
DataClassCommentAlignment, StructClassCommentAlignment
Defined in:
lib/rubocop/cop/style/rbs_inline/class_comment_alignment.rb,
sig/rubocop/cop/style/rbs_inline/class_comment_alignment.rbs

Overview

Shared behavior for cops that ensure #: inline type annotations on struct-like class attributes (Data.define / Struct.new) are aligned.

The including cop matches the definition node (e.g. via struct_like_class?) and calls #check_comment_alignment.

Instance Method Summary collapse

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

#check_annotation_alignment(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/style/rbs_inline/class_comment_alignment.rb', line 27

def check_annotation_alignment(node) #: void
  annotated = attr_arguments(node).filter_map do |arg|
    comment = inline_type_comment(arg.location.line)
    [arg, comment] if comment #: [RuboCop::AST::Node, Parser::Source::Comment]
  end
  return if annotated.size < 2

  expected_col = annotation_column(node)
  annotated.each do |arg, comment|
    actual_col = comment.location.column
    next if actual_col == expected_col

    add_offense(comment.source_range) do |corrector|
      correct_alignment(corrector, arg, comment, expected_col)
    end
  end
end

#check_comment_alignment(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


18
19
20
21
22
# File 'lib/rubocop/cop/style/rbs_inline/class_comment_alignment.rb', line 18

def check_comment_alignment(node) #: void
  return if folded?(node)

  check_annotation_alignment(node)
end

#correct_alignment(corrector, arg, comment, expected_col) ⇒ void

This method returns an undefined value.

Parameters:

  • corrector (RuboCop::Cop::Corrector)
  • arg (RuboCop::AST::Node)
  • comment (Parser::Source::Comment)
  • expected_col (Integer)


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

def correct_alignment(corrector, arg, comment, expected_col) #: void # rubocop:disable Metrics/AbcSize
  line = arg.location.line
  line_source = source_code_at(line)
  source = line_source[...comment.location.column] || raise
  content_end_col = source.rstrip.length
  padding = [expected_col - content_end_col, 1].max || raise

  line_begin = processed_source.buffer.line_range(line).begin_pos
  replace_start = line_begin + content_end_col
  replace_end = line_begin + comment.location.column

  corrector.replace(range_between(replace_start, replace_end), " " * padding)
end