Class: RuboCop::Cop::Style::RbsInline::StructClassCommentAlignment

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

Overview

Checks that #: inline type annotations in Struct.new blocks are aligned.

Each #: annotation comment should start at the same column, determined by the longest attribute name (plus trailing comma). Folded Struct.new calls (where attributes are not one per line) are excluded.

Examples:

# bad
Point = Struct.new(
  :x, #: Integer
  :y   #: Integer
)

# good
Point = Struct.new(
  :x,  #: Integer
  :y   #: Integer
)

Constant Summary collapse

MSG =

Returns:

  • (::String)
"Misaligned inline type annotation for Struct attribute."

Instance Method Summary collapse

Methods included from ClassCommentAlignment

#check_annotation_alignment, #check_comment_alignment, #correct_alignment

Methods included from DataStructHelper

#annotation_column, #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

#attr_argument?(arg) ⇒ Boolean

Struct.new treats a leading string argument as the struct name, so only symbol arguments are attributes.

Parameters:

  • arg (RuboCop::AST::Node)

Returns:

  • (Boolean)


51
52
53
# File 'lib/rubocop/cop/style/rbs_inline/struct_class_comment_alignment.rb', line 51

def attr_argument?(arg) #: bool
  arg.sym_type?
end

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


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

def on_send(node) #: void
  return unless struct_like_class?(node)

  check_comment_alignment(node)
end

#struct_like_class?(node) ⇒ Boolean

Parameters:

  • node (RuboCop::AST::SendNode)

Returns:

  • (Boolean)


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

def struct_like_class?(node) #: bool
  return false unless node.method_name == :new

  (r = node.receiver).is_a?(RuboCop::AST::ConstNode) && r.short_name == :Struct
end