Sha256: ac216aac6602d007c4d74b7f7aa9367942c9b64161d9ca269901fae9261a7af0

Contents?: true

Size: 1.57 KB

Versions: 186

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

186 entries across 179 versions & 17 rubygems

Version Path
rubocop-1.68.0 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.67.0 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.66.1 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.66.0 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.65.1 lib/rubocop/cop/correctors/if_then_corrector.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.65.0 lib/rubocop/cop/correctors/if_then_corrector.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/if_then_corrector.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/if_then_corrector.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/if_then_corrector.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.64.1 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.63.4 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.63.3 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.63.2 lib/rubocop/cop/correctors/if_then_corrector.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.63.1 lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.63.0 lib/rubocop/cop/correctors/if_then_corrector.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/correctors/if_then_corrector.rb
rubocop-1.62.1 lib/rubocop/cop/correctors/if_then_corrector.rb