Sha256: 0b8bf3a603bfbffe108b10900b4d78b49dbe2fb0f8dc052613a270083401a28d

Contents?: true

Size: 1.41 KB

Versions: 31

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Prevents usage of `"*"` on an Arel::Table column reference.
      #
      # Using `arel_table["*"]` causes the outputted string to be a literal
      # quoted asterisk (e.g. <tt>`my_model`.`*`</tt>). This causes the
      # database to look for a column named <tt>`*`</tt> (or `"*"`) as opposed
      # to expanding the column list as one would likely expect.
      #
      # @safety
      #   This cop's autocorrection is unsafe because it turns a quoted `*` into
      #   an SQL `*`, unquoted. `*` is a valid column name in certain databases
      #   supported by Rails, and even though it is usually a mistake,
      #   it might denote legitimate access to a column named `*`.
      #
      # @example
      #   # bad
      #   MyTable.arel_table["*"]
      #
      #   # good
      #   MyTable.arel_table[Arel.star]
      #
      class ArelStar < Base
        extend AutoCorrector

        MSG = 'Use `Arel.star` instead of `"*"` for expanded column lists.'

        RESTRICT_ON_SEND = %i[[]].freeze

        def_node_matcher :star_bracket?, <<~PATTERN
          (send {const (send _ :arel_table)} :[] $(str "*"))
        PATTERN

        def on_send(node)
          return unless (star = star_bracket?(node))

          add_offense(star) do |corrector|
            corrector.replace(star, 'Arel.star')
          end
        end
      end
    end
  end
end

Version data entries

31 entries across 30 versions & 6 rubygems

Version Path
rubocop-rails-2.29.0 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.28.0 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.27.0 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/arel_star.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/arel_star.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/arel_star.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/arel_star.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.22.1 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.22.0 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.21.2 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.21.1 lib/rubocop/cop/rails/arel_star.rb