Class: RuboCop::Cop::YARD::MeaninglessTag

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
DocumentationComment, RangeHelp, Helper
Defined in:
lib/rubocop/cop/yard/meaningless_tag.rb

Overview

Examples:

meaningless tag

# bad
# @param [String] foo
# @option bar baz [String]
class Foo

# bad
# @param [String] foo
# @option bar baz [String]
CONST = 1

# good
class Foo

# good
CONST = 1

# good (Struct/Data constant assignments accept @param)
# @param name [String]
# @param age [Integer]
Person = Struct.new(:name, :age, keyword_init: true)

Instance Method Summary collapse

Methods included from Helper

#build_docstring, #each_types_explainer, #extract_tag_types, #inline_comment?, #parse_type, #styled_string

Instance Method Details

#check(node) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubocop/cop/yard/meaningless_tag.rb', line 48

def check(node)
  preceding_lines = preceding_lines(node)
  return false unless preceding_comment?(node, preceding_lines.last)

  docstring = build_docstring(preceding_lines)
  return false unless docstring

  docstring.tags.each do |tag|
    next unless tag.tag_name == 'param' || tag.tag_name == 'option'
    next if tag.tag_name == 'param' && struct_or_data_definition?(node)

    comment = preceding_lines.find { |line| line.text.include?("@#{tag.tag_name}") }
    next unless comment

    add_offense(comment, message: "`@#{tag.tag_name}` is meaningless tag on #{node.type}") do |corrector|
      corrector.replace(comment, comment.text.gsub("@#{tag.tag_name}", tag.tag_name))
    end
  end
end

#on_class(node) ⇒ Object Also known as: on_module, on_casgn



42
43
44
# File 'lib/rubocop/cop/yard/meaningless_tag.rb', line 42

def on_class(node)
  check(node)
end

#struct_or_data_definition?(node) ⇒ Object

Parameters:

  • node (RuboCop::AST::Node)


35
36
37
38
39
40
# File 'lib/rubocop/cop/yard/meaningless_tag.rb', line 35

def_node_matcher :struct_or_data_definition?, <<~PATTERN
  (casgn _ _ {
    (block (send (const _ {:Struct :Data}) {:new :define} ...) ...)
    (send (const _ {:Struct :Data}) {:new :define} ...)
  })
PATTERN