Module: RuboCop::Cop::ParamHelp

Included in:
Yardoc::ColumnParams, Yardoc::ParamDocumentation, Yardoc::SupportedTags
Defined in:
lib/rubocop/cop/mixin/param_help.rb

Overview

Helper methods to work with node parameters

Instance Method Summary collapse

Instance Method Details

#param_tags_and_positions(node) ⇒ Array<Hash>

Extracts "@param" tags and their position in comment block

Parameters:

  • node (RuboCop::AST::Node)

    The AST node

Returns:

  • (Array<Hash>)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/mixin/param_help.rb', line 26

def param_tags_and_positions(node) # rubocop:disable Metrics/MethodLength
  node_comments = processed_source.ast_with_comments.fetch(node, [])
  param_tags = yard_tags(node).select { |t| t.tag_name == 'param' }

  # Try to match extracted yard tags and source comments
  param_tags.map do |param|
    source_comment = node_comments.find { |comment| comment.text.match?(/@param\s+#{param.name}/) }

    {
      name:     param.name,
      source:   source_comment.source,
      comment:  source_comment,
      yard_tag: param,
      range:    start_tag_position(source_comment, 'param'),
    }
  end
end

#start_tag_position(comment_line, tag_name) ⇒ Object

Returns the location of "@<tag_name>" of the comment_line in the source buffer

Parameters:

  • comment_line (Parser::Source::Comment)

    Comment line

  • tag_name (String)

    Name of the tag



12
13
14
15
16
17
18
19
# File 'lib/rubocop/cop/mixin/param_help.rb', line 12

def start_tag_position(comment_line, tag_name)
  comment_range = comment_line.loc.expression
  at_pos        = comment_line.text.index('@')

  Parser::Source::Range.new processed_source.buffer,
                            comment_range.begin_pos + at_pos,
                            comment_range.begin_pos + at_pos + tag_name.length + 1
end