Class: Rubocop::Cop::DefParentheses
- Defined in:
- lib/rubocop/cop/def_parentheses.rb
Constant Summary collapse
- ERROR_MESSAGE =
['Use def with parentheses when there are arguments.', 'Omit the parentheses in defs when the method ' + "doesn't accept any arguments."]
- EMPTY_PARAMS =
[:params, nil, nil, nil, nil, nil]
Instance Attribute Summary
Attributes inherited from Cop
Instance Method Summary collapse
Methods inherited from Cop
#add_offence, enabled?, #has_report?, inherited, #initialize
Constructor Details
This class inherits a constructor from Rubocop::Cop::Cop
Instance Method Details
#inspect(file, source, tokens, sexp) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rubocop/cop/def_parentheses.rb', line 11 def inspect(file, source, tokens, sexp) each(:def, sexp) do |def_sexp| pos = def_sexp[1][-1] case def_sexp[2][0] when :params if def_sexp[2] != EMPTY_PARAMS add_offence(:convention, pos.lineno, ERROR_MESSAGE[0]) end when :paren if def_sexp[2][1] == EMPTY_PARAMS 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| not 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[1]) end end end end end |