Class: Rubocop::Cop::DefWithParentheses

Inherits:
Cop
  • Object
show all
Includes:
DefParentheses
Defined in:
lib/rubocop/cop/def_parentheses.rb

Constant Summary

Constants included from DefParentheses

Rubocop::Cop::DefParentheses::EMPTY_PARAMS

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods included from DefParentheses

#inspect

Methods inherited from Cop

#add_offence, cop_name, #has_report?, inherited, #initialize, #name

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#check(tokens, def_sexp) ⇒ Object



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

def check(tokens, def_sexp)
  if def_sexp[2][0] == :paren && def_sexp[2][1] == EMPTY_PARAMS
    pos = def_sexp[1][-1]
    method_name_ix = tokens.index { |t| t.pos == pos }
    start = method_name_ix + 1
    rparen_ix = start + tokens[start..-1].index { |t| t.text == ')' }
    first_body_token = tokens[(rparen_ix + 1)..-1].find do |t|
      !whitespace?(t)
    end
    if first_body_token.pos.lineno > pos.lineno
      # Only report offence if there's a line break after
      # the empty parens.
      add_offence(:convention, pos.lineno, error_message)
    end
  end
end

#error_messageObject



18
19
20
21
# File 'lib/rubocop/cop/def_parentheses.rb', line 18

def error_message
  "Omit the parentheses in defs when the method doesn't accept any " +
    'arguments.'
end