Included Modules

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::StatementModifier

Common functionality for modifier cops.

Public Instance Methods

body_has_comment?(body, comments) click to toggle source
# File lib/rubocop/cop/mixin/statement_modifier.rb, line 52
def body_has_comment?(body, comments)
  comment_lines = comments.map(&:location).map(&:line)
  body_line = body.loc.expression.line
  comment_lines.include?(body_line)
end
body_length(body) click to toggle source
# File lib/rubocop/cop/mixin/statement_modifier.rb, line 44
def body_length(body)
  if body && body.loc.expression
    body.loc.expression.size
  else
    0
  end
end
check(sexp, comments) click to toggle source

TODO: Extremely ugly solution that needs lots of polish.

# File lib/rubocop/cop/mixin/statement_modifier.rb, line 9
def check(sexp, comments)
  case sexp.loc.keyword.source
  when 'if'     then cond, body, _else = *sexp
  when 'unless' then cond, _else, body = *sexp
  else               cond, body = *sexp
  end

  return false if length(sexp) > 3

  body_length = body_length(body)

  return false if body_length == 0

  on_node(:lvasgn, cond) do
    return false
  end

  indentation = sexp.loc.keyword.column
  kw_length = sexp.loc.keyword.size
  cond_length = cond.loc.expression.size
  space = 1
  total = indentation + body_length + space + kw_length + space +
    cond_length
  total <= max_line_length && !body_has_comment?(body, comments)
end
length(sexp) click to toggle source
# File lib/rubocop/cop/mixin/statement_modifier.rb, line 40
def length(sexp)
  sexp.loc.expression.source.lines.to_a.size
end
max_line_length() click to toggle source
# File lib/rubocop/cop/mixin/statement_modifier.rb, line 35
def max_line_length
  cop_config && cop_config['MaxLineLength'] ||
  config.for_cop('Style/LineLength')['Max']
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.