Sha256: 116e49daa6fc3ee6efa7f9e4f510049fb16040bf2995e804c1d8fbcec0d67f47
Contents?: true
Size: 1.64 KB
Versions: 21
Compression:
Stored size: 1.64 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Style # If the branch of a conditional consists solely of a conditional node, # its conditions can be combined with the conditions of the outer branch. # This helps to keep the nesting level from getting too deep. # # @example # # bad # if condition_a # if condition_b # do_something # end # end # # # good # if condition_a && condition_b # do_something # end # # @example AllowModifier: false (default) # # bad # if condition_a # do_something if condition_b # end # # @example AllowModifier: true # # good # if condition_a # do_something if condition_b # end # class SoleNestedConditional < Base MSG = 'Consider merging nested conditions into '\ 'outer `%<conditional_type>s` conditions.' def on_if(node) return if node.ternary? || node.else? || node.elsif? branch = node.if_branch return unless offending_branch?(branch) message = format(MSG, conditional_type: node.keyword) add_offense(branch.loc.keyword, message: message) end private def offending_branch?(branch) return false unless branch branch.if_type? && !branch.else? && !branch.ternary? && !(branch.modifier_form? && allow_modifier?) end def allow_modifier? cop_config['AllowModifier'] end end end end end
Version data entries
21 entries across 21 versions & 3 rubygems