Sha256: bc12f7150e5d50d535a0193187e4411b062dab1c37120653c4fb9e08227e9705

Contents?: true

Size: 1.91 KB

Versions: 17

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop checks for migrations using `add_column` that have an `index`
      # key. `add_column` does not accept `index`, but also does not raise an
      # error for extra keys, so it is possible to mistakenly add the key without
      # realizing it will not actually add an index.
      #
      # @example
      #   # bad (will not add an index)
      #   add_column :table, :column, :integer, index: true
      #
      #   # good
      #   add_column :table, :column, :integer
      #   add_index :table, :column
      #
      class AddColumnIndex < Base
        extend AutoCorrector
        include RangeHelp

        MSG = '`add_column` does not accept an `index` key, use `add_index` instead.'
        RESTRICT_ON_SEND = %i[add_column].freeze

        # @!method add_column_with_index(node)
        def_node_matcher :add_column_with_index, <<~PATTERN
          (
            send nil? :add_column $_table $_column
              <(hash <$(pair {(sym :index) (str "index")} $_) ...>) ...>
          )
        PATTERN

        def on_send(node)
          table, column, pair, value = add_column_with_index(node)
          return unless pair

          add_offense(pair) do |corrector|
            corrector.remove(index_range(pair))

            add_index = "add_index #{table.source}, #{column.source}"
            add_index_opts = ''

            if value.hash_type?
              hash = value.loc.expression.adjust(begin_pos: 1, end_pos: -1).source.strip
              add_index_opts = ", #{hash}"
            end

            corrector.insert_after(node, "\n#{add_index}#{add_index_opts}")
          end
        end

        private

        def index_range(pair_node)
          range_with_surrounding_comma(
            range_with_surrounding_space(range: pair_node.loc.expression, side: :left),
            :left
          )
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/add_column_index.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.14.2 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.14.1 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.14.0 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.13.2 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.13.1 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.13.0 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.12.4 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.12.3 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.12.2 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.12.1 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.12.0 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.11.3 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.11.2 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.11.1 lib/rubocop/cop/rails/add_column_index.rb
rubocop-rails-2.11.0 lib/rubocop/cop/rails/add_column_index.rb