Module: RuboCop::Cop::YARD::Helper

Included in:
CollectionStyle, CollectionType, MeaninglessTag, MismatchName, TagTypePosition, TagTypeSyntax
Defined in:
lib/rubocop/cop/yard/helper.rb

Instance Method Summary collapse

Instance Method Details

#build_docstring(preceding_lines) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rubocop/cop/yard/helper.rb', line 81

def build_docstring(preceding_lines)
  comment_texts = preceding_lines.map { |l| l.text.gsub(/\A#/, '') }
  minimum_space = comment_texts.map { |t| t.index(/[^\s]/) }.compact.min
  yard_docstring = comment_texts.map { |t| t[minimum_space..-1] }.join("\n")
  begin
    ::YARD::Logger.instance.enter_level(::YARD::Logger::ERROR) do
      ::YARD::DocstringParser.new.parse(yard_docstring)
    end
  rescue
    nil
  end
end

#each_types_explainer(docstring, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/yard/helper.rb', line 22

def each_types_explainer(docstring, &block)
  docstring.tags.each do |tag|
    types = extract_tag_types(tag) or next

    begin
      types_explainers = parse_type(types.join(', '))
      next if types.size != types_explainers.size

      types.zip(types_explainers).each do |type, types_explainer|
        block.call(type, types_explainer)
      end
    rescue SyntaxError
    end
  end
end

#extract_tag_types(tag) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/rubocop/cop/yard/helper.rb', line 7

def extract_tag_types(tag)
  case tag
  when ::YARD::Tags::OptionTag
    tag.pair.types
  when ::YARD::Tags::OverloadTag
    tag.types
  else
    tag.types
  end
end

#inline_comment?(comment) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/rubocop/cop/yard/helper.rb', line 77

def inline_comment?(comment)
  !comment_line?(comment.source_range.source_line)
end

#parse_type(type) ⇒ Object



18
19
20
# File 'lib/rubocop/cop/yard/helper.rb', line 18

def parse_type(type)
  ::YARD::Tags::TypesExplainer::Parser.parse(type)
end

#styled_string(types_explainer) ⇒ Object



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
# File 'lib/rubocop/cop/yard/helper.rb', line 38

def styled_string(types_explainer)
  case types_explainer
  when ::YARD::Tags::TypesExplainer::HashCollectionType
    tname = case [style, types_explainer.name]
    when [:short, 'Hash']
      ''
    when [:long, 'Hash']
      'Hash'
    else
      types_explainer.name
    end
    "#{tname}{#{types_explainer.key_types.map { styled_string(_1) }.join(', ')} => #{types_explainer.value_types.map { styled_string(_1) }.join(', ')}}"
  when ::YARD::Tags::TypesExplainer::FixedCollectionType
    tname = case [style, types_explainer.name]
    when [:short, 'Array']
      ''
    when [:long, 'Array']
      'Array'
    else
      types_explainer.name
    end
    "#{tname}(#{types_explainer.types.map { styled_string(_1) }.join(', ')})"
  when ::YARD::Tags::TypesExplainer::CollectionType
    tname = case [style, types_explainer.name]
    when [:short, 'Array']
      ''
    when [:long, 'Array']
      'Array'
    else
      types_explainer.name
    end
    "#{tname}<#{types_explainer.types.map { styled_string(_1) }.join(', ')}>"
  when ::YARD::Tags::TypesExplainer::Type
    types_explainer.name
  else
    raise "#{types_explainer.class} is not supported"
  end
end