Class: RuboCop::Cop::GitHub::RailsControllerRenderLiteral

Inherits:
Base
  • Object
show all
Includes:
RenderLiteralHelpers
Defined in:
lib/rubocop/cop/github/rails_controller_render_literal.rb

Constant Summary collapse

MSG =
"render must be used with a string literal or an instance of a Class, 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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rubocop/cop/github/rails_controller_render_literal.rb', line 54

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

  return if render_view_component?(node) || render_const?(node)

  if render_literal?(node)
  elsif option_pairs = render_with_options?(node)
    option_pairs = option_pairs.reject { |pair| options_key?(pair) }

    if option_pairs.any? { |pair| ignore_key?(pair) }
      return
    end

    if template_node = option_pairs.map { |pair| template_key?(pair) }.compact.first
      if !literal?(template_node)
        add_offense(node)
        return
      end
    else
      add_offense(node)
      return
    end

    if layout_node = option_pairs.map { |pair| layout_key?(pair) }.compact.first
      if !literal?(layout_node)
        add_offense(node)
        return
      end
    end
  else
    add_offense(node)
    return
  end

  if render_literal?(node)
    option_hash = node.arguments[1]
    if option_hash && !option_hash.hash_type?
      add_offense(node)
      return
    end
    option_pairs = option_hash && option_hash.pairs
  else
    option_pairs = node.arguments[0].pairs
  end

  if option_pairs
    locals = option_pairs.map { |pair| locals_key?(pair) }.compact.first
    if locals && (!locals.hash_type? || !hash_with_literal_keys?(locals))
      add_offense(node)
    end
  end
end