Sha256: 831a1543f64e47d304db559fb359a46e7e15d6b92fd10ebece3f7b2b7fdbf431

Contents?: true

Size: 1.84 KB

Versions: 5

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require 'rubocop'

module RuboCop
  module Cop
    module Standard
      class RailsViewRenderLiteral < Cop
        MSG = 'render must be used with a string literal or an instance of a Class'

        def_node_matcher :literal?, <<-PATTERN
          ({str sym true false nil?} ...)
        PATTERN

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

        def_node_matcher :render_literal?, <<-PATTERN
          (send nil? :render ({str sym} $_) $...)
        PATTERN

        def_node_matcher :render_inst?, <<-PATTERN
          (send nil? :render (send _ :new ...) ...)
        PATTERN

        def_node_matcher :render_const?, <<-PATTERN
          (send nil? :render (const _ _) ...)
        PATTERN

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

        def_node_matcher :ignore_key?, <<-PATTERN
          (pair (sym {
            :inline
          }) $_)
        PATTERN

        def_node_matcher :partial_key?, <<-PATTERN
          (pair (sym {
            :file
            :template
            :layout
            :partial
          }) $_)
        PATTERN

        def on_send(node)
          return unless render?(node)

          if render_literal?(node) || render_inst?(node) || render_const?(node)
          elsif (option_pairs = render_with_options?(node))
            return if option_pairs.any? { |pair| ignore_key?(pair) }

            if (partial_node = option_pairs.map { |pair| partial_key?(pair) }.compact.first)
              add_offense(node, location: :expression) unless literal?(partial_node)
            else
              add_offense(node, location: :expression)
            end
          else
            add_offense(node, location: :expression)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-standard-3.2.0 lib/rubocop/cop/standard/rails/rails_view_render_literal.rb
rubocop-standard-3.1.2 lib/rubocop/cop/standard/rails/rails_view_render_literal.rb
rubocop-standard-3.1.1 lib/rubocop/cop/standard/rails/rails_view_render_literal.rb
rubocop-standard-3.1.0 lib/rubocop/cop/standard/rails/rails_view_render_literal.rb
rubocop-standard-3.0.0 lib/rubocop/cop/standard/rails/rails_view_render_literal.rb