Class: RuboCop::Cop::Style::RbsInline::MissingStructClassAnnotation

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

Overview

Checks that Struct.new attributes have inline type annotations.

Each attribute passed to Struct.new should have a trailing #: type annotation comment on the same line. A leading string argument (the struct name) and the keyword_init: keyword argument are not attributes and are ignored.

Examples:

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

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

Constant Summary collapse

MSG =

Returns:

  • (::String)
"Missing inline type annotation for Struct attribute (e.g., `#: Type`)."

Instance Method Summary collapse

Methods included from MissingClassAnnotation

#add_offenses_for_folded, #check_missing_annotations, #check_multiline, #correct_multiline, #insert_annotation

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)


49
50
51
# File 'lib/rubocop/cop/style/rbs_inline/missing_struct_class_annotation.rb', line 49

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

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


31
32
33
34
35
# File 'lib/rubocop/cop/style/rbs_inline/missing_struct_class_annotation.rb', line 31

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

  check_missing_annotations(node)
end

#struct_like_class?(node) ⇒ Boolean

Parameters:

  • node (RuboCop::AST::SendNode)

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/rubocop/cop/style/rbs_inline/missing_struct_class_annotation.rb', line 40

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