<commit_before># frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This cop checks whether the rescue and ensure keywords are aligned
      # properly.
      #
      # @example
      #
      #   # bad
      #   begin
      #     something
      #     rescue
      #     puts 'error'
      #   end
      #
      #   # good
      #   begin
      #     something
      #   rescue
      #     puts 'error'
      #   end
      class RescueEnsureAlignment < Cop
        include RangeHelp

        MSG = '`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \
              'aligned with `%<beginning>s` at ' \
              '%<begin_loc_line>d, %<begin_loc_column>d.'
        ANCESTOR_TYPES = %i[kwbegin def defs class module].freeze
        RUBY_2_5_ANCESTOR_TYPES = (ANCESTOR_TYPES + %i[block]).freeze
        ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS = %i[def defs].freeze
        ALTERNATIVE_ACCESS_MODIFIERS = %i[public_class_method
                                          private_class_method].freeze

        def on_resbody(node)
          check(node) unless modifier?(node)
        end

        def on_ensure(node)
          check(node)
        end

        def autocorrect(node)
          whitespace = whitespace_range(node)
          # Some inline node is sitting before current node.
          return nil unless whitespace.source.strip.empty?

          alignment_node = alignment_node(node)
          return false if alignment_node.nil?

          new_column = alignment_node.loc.column
          ->(corrector) { corrector.replace(whitespace, ' ' * new_column) }
        end

        def investigate(processed_source)
          @modifier_locations =
            processed_source.tokens.each_with_object([]) do |token, locations|
              next unless token.rescue_modifier?

              locations << token.pos
            end
        end

        private

        # Check alignment of node with rescue or ensure modifiers.

        def check(node)
          alignment_node = alignment_node(node)
          return if alignment_node.nil?

          alignment_loc = alignment_node.loc.expression
          kw_loc        = node.loc.keyword

          return if
            alignment_loc.column == kw_loc.column ||
            alignment_loc.line   == kw_loc.line

          add_offense(
            node,
            location: kw_loc,
            message: format_message(alignment_node, alignment_loc, kw_loc)
          )
        end

        def format_message(alignment_node, alignment_loc, kw_loc)
          format(
            MSG,
            kw_loc: kw_loc.source,
            kw_loc_line: kw_loc.line,
            kw_loc_column: kw_loc.column,
            beginning: alignment_source(alignment_node, alignment_loc),
            begin_loc_line: alignment_loc.line,
            begin_loc_column: alignment_loc.column
          )
        end

        def alignment_source(node, starting_loc)
          ending_loc =
            case node.type
            when :block, :kwbegin
              node.loc.begin
            when :def, :defs, :class, :module,
                 :lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn
              node.loc.name
            when :masgn
              mlhs_node, = *node
              mlhs_node.loc.expression
            else
              # It is a wrapper with access modifier.
              node.child_nodes.first.loc.name
            end

          range_between(starting_loc.begin_pos, ending_loc.end_pos).source
        end<commit_msg>Ignore ABC size of Layout/RescueEnsureAlignment#alignment_source<commit_after># frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This cop checks whether the rescue and ensure keywords are aligned
      # properly.
      #
      # @example
      #
      #   # bad
      #   begin
      #     something
      #     rescue
      #     puts 'error'
      #   end
      #
      #   # good
      #   begin
      #     something
      #   rescue
      #     puts 'error'
      #   end
      class RescueEnsureAlignment < Cop
        include RangeHelp

        MSG = '`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \
              'aligned with `%<beginning>s` at ' \
              '%<begin_loc_line>d, %<begin_loc_column>d.'
        ANCESTOR_TYPES = %i[kwbegin def defs class module].freeze
        RUBY_2_5_ANCESTOR_TYPES = (ANCESTOR_TYPES + %i[block]).freeze
        ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS = %i[def defs].freeze
        ALTERNATIVE_ACCESS_MODIFIERS = %i[public_class_method
                                          private_class_method].freeze

        def on_resbody(node)
          check(node) unless modifier?(node)
        end

        def on_ensure(node)
          check(node)
        end

        def autocorrect(node)
          whitespace = whitespace_range(node)
          # Some inline node is sitting before current node.
          return nil unless whitespace.source.strip.empty?

          alignment_node = alignment_node(node)
          return false if alignment_node.nil?

          new_column = alignment_node.loc.column
          ->(corrector) { corrector.replace(whitespace, ' ' * new_column) }
        end

        def investigate(processed_source)
          @modifier_locations =
            processed_source.tokens.each_with_object([]) do |token, locations|
              next unless token.rescue_modifier?

              locations << token.pos
            end
        end

        private

        # Check alignment of node with rescue or ensure modifiers.

        def check(node)
          alignment_node = alignment_node(node)
          return if alignment_node.nil?

          alignment_loc = alignment_node.loc.expression
          kw_loc        = node.loc.keyword

          return if
            alignment_loc.column == kw_loc.column ||
            alignment_loc.line   == kw_loc.line

          add_offense(
            node,
            location: kw_loc,
            message: format_message(alignment_node, alignment_loc, kw_loc)
          )
        end

        def format_message(alignment_node, alignment_loc, kw_loc)
          format(
            MSG,
            kw_loc: kw_loc.source,
            kw_loc_line: kw_loc.line,
            kw_loc_column: kw_loc.column,
            beginning: alignment_source(alignment_node, alignment_loc),
            begin_loc_line: alignment_loc.line,
            begin_loc_column: alignment_loc.column
          )
        end

        # rubocop:disable Metrics/AbcSize
        def alignment_source(node, starting_loc)
          ending_loc =
            case node.type
            when :block, :kwbegin
              node.loc.begin
            when :def, :defs, :class, :module,
                 :lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn
              node.loc.name
            when :masgn
              mlhs_node, = *node
              mlhs_node.loc.expression
            else
              # It is a wrapper with access modifier.
              node.child_nodes.first.loc.name
            end

          range_between(starting_loc.begin_pos, ending_loc.end_pos).source
        end
        # rubocop:enable Metrics/AbcSize
{'fraction_non_alphanumeric': 0.07312308121685739, 'fraction_numerical': 0.0005581914596706671, 'mean_word_length': 1.6722595078299776, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6953828', 'n_tokens_mistral': 2073, 'n_tokens_neox': 2035, 'n_words': 439}