Sha256: ac216aac6602d007c4d74b7f7aa9367942c9b64161d9ca269901fae9261a7af0

Contents?: true

Size: 1.57 KB

Versions: 187

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # This class autocorrects `if...then` structures to a multiline `if` statement
    class IfThenCorrector
      DEFAULT_INDENTATION_WIDTH = 2

      def initialize(if_node, indentation: nil)
        @if_node = if_node
        @indentation = indentation || DEFAULT_INDENTATION_WIDTH
      end

      def call(corrector)
        corrector.replace(if_node, replacement)
      end

      private

      attr_reader :if_node, :indentation

      def replacement(node = if_node, indentation = nil)
        indentation = ' ' * node.source_range.column if indentation.nil?
        if_branch_source = node.if_branch&.source || 'nil'
        elsif_indentation = indentation if node.respond_to?(:elsif?) && node.elsif?

        if_branch = <<~RUBY
          #{elsif_indentation}#{node.keyword} #{node.condition.source}
          #{indentation}#{branch_body_indentation}#{if_branch_source}
        RUBY

        else_branch = rewrite_else_branch(node.else_branch, indentation)
        if_branch + else_branch
      end

      def rewrite_else_branch(else_branch, indentation)
        if else_branch.nil?
          'end'
        elsif else_branch.if_type? && else_branch.elsif?
          replacement(else_branch, indentation)
        else
          <<~RUBY.chomp
            #{indentation}else
            #{indentation}#{branch_body_indentation}#{else_branch.source}
            #{indentation}end
          RUBY
        end
      end

      def branch_body_indentation
        @branch_body_indentation ||= (' ' * indentation).freeze
      end
    end
  end
end

Version data entries

187 entries across 180 versions & 17 rubygems

Version Path
rubocop-1.57.1 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.57.0 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.56.4 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.56.3 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.56.2 lib/rubocop/cop/correctors/if_then_corrector.rb
synctera_ruby_sdk-1.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/correctors/if_then_corrector.rb
synctera_ruby_sdk-1.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/correctors/if_then_corrector.rb
synctera_ruby_sdk-1.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/correctors/if_then_corrector.rb
sampero-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.1/lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.56.1 lib/rubocop/cop/correctors/if_then_corrector.rb
tursodb-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/correctors/if_then_corrector.rb
synctera_ruby_sdk-1.0.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.56.0 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.55.1 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.55.0 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.54.2 lib/rubocop/cop/correctors/if_then_corrector.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/correctors/if_then_corrector.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/correctors/if_then_corrector.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.54.1 lib/rubocop/cop/correctors/if_then_corrector.rb