Class: RuboCop::Cop::GitHub::RailsViewRenderLiteral

Inherits:
RuboCop::Cop
  • Object
show all
Includes:
RenderLiteralHelpers
Defined in:
lib/rubocop/cop/github/rails_view_render_literal.rb

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from RenderLiteralHelpers

#hash_with_literal_keys?, #render_view_component?

Instance Method Details

#on_send(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubocop/cop/github/rails_view_render_literal.rb', line 29

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, location: :expression)
        return
      end
    else
      add_offense(node, location: :expression)
      return
    end
  else
    add_offense(node, location: :expression)
    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, location: :expression)
      end
    else
      add_offense(node, location: :expression)
    end
  end
end