Sha256: 2d5d7a2524927861e7fcfbe4075c52ff2ec8197d9dc2fd157fe14a570b587b6e

Contents?: true

Size: 1.55 KB

Versions: 29

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require "rubocop"

module RuboCop
  module Cop
    module GitHub
      class RailsRenderObjectCollection < Cop
        MSG = "Avoid `render object:`"

        def_node_matcher :render_with_options?, <<-PATTERN
          (send nil? :render (hash $...) ...)
        PATTERN

        def_node_matcher :partial_key?, <<-PATTERN
          (pair (sym :partial) $_)
        PATTERN

        def_node_matcher :object_key?, <<-PATTERN
          (pair (sym ${:object :collection :spacer_template}) $_)
        PATTERN

        def on_send(node)
          if option_pairs = render_with_options?(node)
            partial_pair = option_pairs.detect { |pair| partial_key?(pair) }
            object_pair  = option_pairs.detect { |pair| object_key?(pair) }

            if partial_pair && object_pair
              partial_name = partial_key?(partial_pair)
              object_sym, object_node = object_key?(object_pair)

              case object_sym
              when :object
                if partial_name.children[0].is_a?(String)
                  suggestion = ", instead `render partial: #{partial_name.source}, locals: { #{File.basename(partial_name.children[0], '.html.erb')}: #{object_node.source} }`"
                end
                add_offense(node, location: :expression, message: "Avoid `render object:`#{suggestion}")
              when :collection, :spacer_template
                add_offense(node, location: :expression, message: "Avoid `render collection:`")
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
rubocop-springest-0.7.5 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.7.4 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.7.3 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.7.2 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-github-0.16.0 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-github-0.15.0 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.7.1 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-github-0.14.0 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.7.0 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.6.9 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.6.8 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-github-0.13.0 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.6.7 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.6.6 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.6.5 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.6.4 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.6.3 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-github-0.12.0 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-github-0.11.0 lib/rubocop/cop/github/rails_render_object_collection.rb
rubocop-springest-0.6.2 lib/rubocop/cop/github/rails_render_object_collection.rb