Sha256: 812315e16fc6fd41a5452e7511f02d6ada092d69facfc1d940cbd87142bd4433

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require "rubocop"
require "rubocop/cop/github/render_literal_helpers"

module RuboCop
  module Cop
    module GitHub
      class RailsViewRenderLiteral < Base
        include RenderLiteralHelpers

        MSG = "render must be used with a literal template and use literals for locals keys"

        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)

          # Ignore "component"-style renders
          return if render_view_component?(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)
                return
              end
            else
              add_offense(node)
              return
            end
          else
            add_offense(node)
            return
          end

          if render_literal?(node) && node.arguments.count > 1
            locals = node.arguments[1]
          elsif options_pairs = render_with_options?(node)
            locals = option_pairs.map { |pair| locals_key?(pair) }.compact.first
          end

          if locals
            if locals.hash_type?
              if !hash_with_literal_keys?(locals)
                add_offense(node)
              end
            else
              add_offense(node)
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-github-0.20.0 lib/rubocop/cop/github/rails_view_render_literal.rb
rubocop-github-0.19.0 lib/rubocop/cop/github/rails_view_render_literal.rb
rubocop-github-0.18.0 lib/rubocop/cop/github/rails_view_render_literal.rb