Sha256: 1275f0336322a329d05bcf6b8f4ac56c7eb88719f56c861163244aed741b11a4

Contents?: true

Size: 1.18 KB

Versions: 29

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

require "rubocop"

module RuboCop
  module Cop
    module GitHub
      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)
          if 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
end

Version data entries

29 entries across 29 versions & 2 rubygems

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