Sha256: 7288034bd1ab3eba07abeb05dfe02b87aca096c5a2fe089ddb7e0c3a510b840c

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require "rubocop"

module RuboCop
  module Cop
    module GitHub
      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
            :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, :expression)
              end
            else
              add_offense(node, :expression)
            end
          else
            add_offense(node, :expression)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
rubocop-shakr-0.6.0 lib/rubocop/cop/github/rails_view_render_literal.rb
rubocop-shakr-0.5.0 lib/rubocop/cop/github/rails_view_render_literal.rb
rubocop-github-0.5.0 lib/rubocop/cop/github/rails_view_render_literal.rb
rubocop-shakr-0.1.0 lib/rubocop/cop/github/rails_view_render_literal.rb
rubocop-github-0.4.2 lib/rubocop/cop/github/rails_view_render_literal.rb
rubocop-github-0.4.1 lib/rubocop/cop/github/rails_view_render_literal.rb