Class: RuboCop::Cop::YARD::MismatchName

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

Overview

Examples:

mismatch name

# bad
# @param [void] baz
# @option opt aaa [void]
def foo(bar, opts = {})
end

# good
# @param [void] bar
# @param [Array] arg
# @option opts aaa [void]
def foo(bar, opts = {}, *arg)
end

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

#on_def(node) ⇒ Object Also known as: on_defs



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubocop/cop/yard/mismatch_name.rb', line 25

def on_def(node)
  return unless node.arguments?

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

  docstring = build_docstring(preceding_lines)
  return false unless docstring

  return false if include_overload_tag?(docstring)

  each_tags_by_docstring(['param', 'option'], docstring) do |tags|
    tags.each_with_index do |tag, i|
      comment = find_by_tag(preceding_lines, tag, i)
      next unless comment

      # YARD::Tags::RefTagList is not has name and types
      next if tag.instance_of?(::YARD::Tags::RefTagList)

      types = extract_tag_types(tag)
      unless tag.name && types
        if tag.name.nil?
          add_offense(comment, message: "No tag name is supplied in `@#{tag.tag_name}`")
        elsif types.nil?
          add_offense(comment, message: "No types are associated with the tag in `@#{tag.tag_name}`")
        end

        next
      end

      next unless node.arguments.none? { |arg_node| tag.name.to_sym == arg_node.name }
      next unless types

      begin
        parse_type(types.join(', '))
      rescue SyntaxError
        next
      end if types

      add_offense_to_tag(node, comment, tag)
    end
  end

  # Documentation only or just `@return` is a common form of documentation.
  # The subsequent features will be limited to cases where both `@param` and `@option` are present.
  unless docstring.tags.find { |tag| (tag.tag_name == 'param' && !(tag.instance_of?(::YARD::Tags::RefTagList) && tag.name.nil?)) || tag.tag_name == 'option' }
    return false
  end
  node.arguments.each do |argument|
    next if argument.type == :blockarg
    next if argument.name.nil?

    found = docstring.tags.find do |tag|
      next unless tag.tag_name == 'param' || tag.tag_name == 'option'
      tag.name&.to_sym == argument.name
    end

    unless found
      comment = preceding_lines.last
      return if part_of_ignored_node?(comment)
      add_offense(comment, message: "This method has argument `#{argument.name}`, But not documented") do |corrector|
        corrector.replace(
          comment.source_range.end,
          "#{comment.source_range.end.join(node.source_range.begin).source}# #{tag_prototype(argument)}"
        )
      end
    end
  end
end