Class: Rubocop::Cop::Style::WhileUntilModifier

Inherits:
Cop
  • Object
show all
Includes:
FavorModifier
Defined in:
lib/rubocop/cop/style/favor_modifier.rb

Overview

Checks for while and until statements that would fit on one line if written as a modifier while/until.

Constant Summary collapse

MSG =
'Favor modifier while/until usage when you have a single-line body.'

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods included from FavorModifier

#body_has_comment?, #body_length, #check, #length

Methods inherited from Cop

#add_offence, #autocorrect_action, cop_name, #do_autocorrect, #ignore_node, inherited, #initialize, #name, rails?

Constructor Details

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

Instance Method Details

#inspect(source_buffer, source, tokens, ast, comments) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rubocop/cop/style/favor_modifier.rb', line 104

def inspect(source_buffer, source, tokens, ast, comments)
  return unless ast
  on_node([:while, :until], ast) do |node|
    # discard modifier while/until
    next unless node.loc.end

    if check(node, comments)
      add_offence(:convention, node.loc.expression, MSG)
    end
  end
end