Sha256: 560a9eca2265dbd04edf097e0756630e4df1a5a904a9e33208c54b883887eb9c

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 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"

        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_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)
          elsif option_pairs = render_with_options?(node)
            if option_pairs.any? { |pair| ignore_key?(pair) }
              return
            end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-standard-1.12.0 lib/rubocop/cop/standard/rails_view_render_literal.rb