Sha256: 1db2335a160d9154a8986ec5c5c03ce3b0bc7011a162db9183dad6d53de94b79

Contents?: true

Size: 1.17 KB

Versions: 12

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require 'rubocop'

module RuboCop
  module Cop
    module Standard
      class RailsViewRenderShorthand < Cop
        MSG = 'Prefer `render` partial shorthand'

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

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

        def_node_matcher :locals_key?, <<-PATTERN
          (pair (sym :locals) $_)
        PATTERN

        def on_send(node)
          return unless (option_pairs = render_with_options?(node))

          partial_key = option_pairs.map { |pair| partial_key?(pair) }.compact.first
          locals_key = option_pairs.map { |pair| locals_key?(pair) }.compact.first

          if option_pairs.length == 1 && partial_key
            add_offense(node, location: :expression, message: "Use `render #{partial_key.source}` instead")
          elsif option_pairs.length == 2 && partial_key && locals_key
            add_offense(node, location: :expression, message: "Use `render #{partial_key.source}, #{locals_key.source}` instead")
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-standard-4.2.0 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
rubocop-standard-4.1.0 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
rubocop-standard-4.0.4 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
rubocop-standard-4.0.3 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
rubocop-standard-4.0.2 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
rubocop-standard-4.0.1 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
rubocop-standard-4.0.0 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
rubocop-standard-3.2.0 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
rubocop-standard-3.1.2 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
rubocop-standard-3.1.1 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
rubocop-standard-3.1.0 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb
rubocop-standard-3.0.0 lib/rubocop/cop/standard/rails/rails_view_render_shorthand.rb