class RuboCop::Cop::Style::IfUnlessModifier
end
do_something_in_a_method_with_a_long_name(arg)
if long_condition
Foo.do_something unless qux.empty?
do_stuff(bar) if condition
# good
do_something_in_a_method_with_a_long_name(arg) if long_condition
end
Foo.do_something
unless qux.empty?
end
do_stuff(bar)
if condition
# bad
@example
`Layout/Tab` cop.
cop. The tab size is configured in the ‘IndentationWidth` of the
The maximum line length is configured in the `Metrics/LineLength`
`if`/`unless` lines that exceed the maximum line length.
written as modifier `if`/`unless`. The cop also checks for modifier
Checks for `if` and `unless` statements that would fit on one line if
def autocorrect(node)
def autocorrect(node) replacement = if node.modifier_form? to_normal_form(node) else to_modifier_form(node) end ->(corrector) { corrector.replace(node.source_range, replacement) } end
def eligible_node?(node)
def eligible_node?(node) !non_eligible_if?(node) && !node.chained? && !node.nested_conditional? && single_line_as_modifier?(node) end
def first_line_comment(node)
def first_line_comment(node) comment = processed_source.find_comment { |c| c.loc.line == node.loc.line } comment ? comment.loc.expression.source : nil end
def line_length_enabled_at_line?(line)
def line_length_enabled_at_line?(line) processed_source.comment_config .cop_enabled_at_line?('Metrics/LineLength', line) end
def named_capture_in_condition?(node)
def named_capture_in_condition?(node) node.condition.match_with_lvasgn_type? end
def non_eligible_if?(node)
def non_eligible_if?(node) node.ternary? || node.modifier_form? || node.elsif? || node.else? end
def on_if(node)
def on_if(node) msg = if eligible_node?(node) MSG_USE_MODIFIER unless named_capture_in_condition?(node) elsif node.modifier_form? && too_long_single_line?(node) MSG_USE_NORMAL end return unless msg add_offense(node, location: :keyword, message: format(msg, keyword: node.keyword)) end
def parenthesize?(node)
def parenthesize?(node) # Parenthesize corrected expression if changing to modifier-if form # would change the meaning of the parent expression # (due to the low operator precedence of modifier-if) return false if node.parent.nil? return true if ASSIGNMENT_TYPES.include?(node.parent.type) node.parent.send_type? && !node.parent.parenthesized? end
def to_modifier_form(node)
def to_modifier_form(node) expression = [node.body.source, node.keyword, node.condition.source, first_line_comment(node)].compact.join(' ') parenthesize?(node) ? "(#{expression})" : expression end
def to_normal_form(node)
def to_normal_form(node) indentation = ' ' * node.source_range.column <<~RUBY.chomp #{node.keyword} #{node.condition.source} #{indentation} #{node.body.source} #{indentation}end RUBY end
def too_long_single_line?(node)
def too_long_single_line?(node) return false unless max_line_length range = node.source_range return false unless range.first_line == range.last_line return false unless line_length_enabled_at_line?(range.first_line) range.last_column > max_line_length end