Sha256: 72139850dc611c363c09f459b901d672845d509ea9823044913743a2ab71e86f

Contents?: true

Size: 1.46 KB

Versions: 161

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for uses of if/unless modifiers with multiple-lines bodies.
      #
      # @example
      #
      #   # bad
      #   {
      #     result: 'this should not happen'
      #   } unless cond
      #
      #   # good
      #   { result: 'ok' } if cond
      class MultilineIfModifier < Base
        include StatementModifier
        include Alignment
        extend AutoCorrector

        MSG = 'Favor a normal %<keyword>s-statement over a modifier ' \
              'clause in a multiline statement.'

        def on_if(node)
          return unless node.modifier_form? && node.body.multiline?

          add_offense(node, message: format(MSG, keyword: node.keyword)) do |corrector|
            corrector.replace(node, to_normal_if(node))
          end
        end

        private

        def to_normal_if(node)
          indented_body = indented_body(node.body, node)
          condition = "#{node.keyword} #{node.condition.source}"
          indented_end = "#{offset(node)}end"

          [condition, indented_body, indented_end].join("\n")
        end

        def indented_body(body, node)
          body_source = "#{offset(node)}#{body.source}"
          body_source.each_line.map do |line|
            if line == "\n"
              line
            else
              line.sub(/^\s{#{offset(node).length}}/, indentation(node))
            end
          end.join
        end
      end
    end
  end
end

Version data entries

161 entries across 160 versions & 17 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/style/multiline_if_modifier.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.69.2 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.69.1 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.69.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.68.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.67.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.66.1 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.66.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.65.1 lib/rubocop/cop/style/multiline_if_modifier.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.65.0 lib/rubocop/cop/style/multiline_if_modifier.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.64.1 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.63.4 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.63.3 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.63.2 lib/rubocop/cop/style/multiline_if_modifier.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.63.1 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.63.0 lib/rubocop/cop/style/multiline_if_modifier.rb