Sha256: ab17bde72e08b36555ad253bcc2856c0a910f7f89bd14513ddf64b73dbd9c984

Contents?: true

Size: 1.79 KB

Versions: 16

Compression:

Stored size: 1.79 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 = processed_source.comment_at_line(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:, configured_width:,
                              indent_steps: 1)
          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, "#{text}\n#{' ' * node.loc.keyword.column}")
          corrector.remove(eol_comment)
        end

        private

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

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

        def semicolon(node)
          @semicolon ||= {}.compare_by_identity
          @semicolon[node] ||= processed_source.tokens_within(node).find(&:semicolon?)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
rubocop-1.20.0 lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.19.1 lib/rubocop/cop/correctors/line_break_corrector.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.19.0 lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.18.4 lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.18.3 lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.18.2 lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.18.1 lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.18.0 lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.17.0 lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.16.1 lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.16.0 lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.15.0 lib/rubocop/cop/correctors/line_break_corrector.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.14.0 lib/rubocop/cop/correctors/line_break_corrector.rb
rubocop-1.13.0 lib/rubocop/cop/correctors/line_break_corrector.rb