Sha256: fabc79d70af2a7da4e5beca75a13a88b14e79521abb4fda2c57c61cf0786a859

Contents?: true

Size: 1.54 KB

Versions: 28

Compression:

Stored size: 1.54 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 configured_indentation_width
          super || 2
        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

28 entries across 24 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/multiline_if_modifier.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/multiline_if_modifier.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/multiline_if_modifier.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/multiline_if_modifier.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/multiline_if_modifier.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/multiline_if_modifier.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/multiline_if_modifier.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/multiline_if_modifier.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.43.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.42.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.41.1 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.41.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.40.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.39.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.38.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.37.1 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.37.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.36.0 lib/rubocop/cop/style/multiline_if_modifier.rb
rubocop-1.35.1 lib/rubocop/cop/style/multiline_if_modifier.rb