Sha256: c9e1bb771e223733def37735aa312e0025165247f3d5d247ba1f67ff2d805db2

Contents?: true

Size: 1.85 KB

Versions: 6824

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # This class handles autocorrection for code that needs to be moved
    # to new lines.
    class LineBreakCorrector
      extend Alignment
      extend TrailingBody
      extend Util

      class << self
        attr_reader :processed_source

        def correct_trailing_body(configured_width:, corrector:, node:,
                                  processed_source:)
          @processed_source = processed_source
          range = first_part_of(node.to_a.last)
          eol_comment = end_of_line_comment(node.source_range.line)

          break_line_before(range: range, node: node, corrector: corrector,
                            configured_width: configured_width)
          move_comment(eol_comment: eol_comment, node: node,
                       corrector: corrector)
          remove_semicolon(node, corrector)
        end

        def break_line_before(range:, node:, corrector:, indent_steps: 1,
                              configured_width:)
          corrector.insert_before(
            range,
            "\n" + ' ' * (node.loc.keyword.column +
                          indent_steps * configured_width)
          )
        end

        def move_comment(eol_comment:, node:, corrector:)
          return unless eol_comment

          text = eol_comment.loc.expression.source
          corrector.insert_before(node.source_range,
                                  text + "\n" + (' ' * node.loc.keyword.column))
          corrector.remove(eol_comment.loc.expression)
        end

        private

        def remove_semicolon(node, corrector)
          return unless semicolon(node)

          corrector.remove(semicolon(node).pos)
        end

        def semicolon(node)
          @semicolon ||= {}
          @semicolon[node.object_id] ||= tokens(node).find(&:semicolon?)
        end
      end
    end
  end
end

Version data entries

6,824 entries across 6,799 versions & 26 rubygems

Version Path
cybrid_api_organization_ruby-0.123.124 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_id_ruby-0.123.124 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_bank_ruby-0.123.123 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_id_ruby-0.123.123 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_organization_ruby-0.123.123 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_bank_ruby-0.123.122 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_organization_ruby-0.123.122 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_id_ruby-0.123.122 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_bank_ruby-0.123.121 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_id_ruby-0.123.121 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_organization_ruby-0.123.121 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_bank_ruby-0.123.120 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_organization_ruby-0.123.120 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_id_ruby-0.123.120 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
avalara_sdk-24.12.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_bank_ruby-0.123.119 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_organization_ruby-0.123.119 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_id_ruby-0.123.119 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_bank_ruby-0.123.118 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb
cybrid_api_id_ruby-0.123.118 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/correctors/line_break_corrector.rb