Class: RuboCop::Cop::Yardoc::ValidTypes

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ParamHelp, RuboCop::Cop::YardHelp
Defined in:
lib/rubocop/cop/yardoc/valid_types.rb

Overview

Ensures all types are separated by a space

Types are parsed with YARD.

Examples:

# bad
# @param name [.method, Array[String]] the name to greet
def greet(name); end

# good
# @param [String, List(item, item2), Array<String>, Hash{String, Integer}, ClassName, #method, nil, true]
def greet(name); end

Constant Summary collapse

MSG =
'Types should be parseable by YARD'

Instance Method Summary collapse

Methods included from ParamHelp

#param_tags_and_positions, #start_tag_position, #start_type_position

Instance Method Details

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

Executed for every method definition

Parameters:

  • node (RuboCop::AST::Node)

    The AST node



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/yardoc/valid_types.rb', line 29

def on_def(node)
  return unless documented?(node)

  param_tags_and_positions(node, tags: [:param, :return]).each do |param|
    param.types.each do |type|
      YARD::Tags::TypesExplainer::Parser.parse type
    rescue SyntaxError
      add_offense(param.type_range || param.tag_range, message: MSG)
      break
    end
  end
end