Sha256: cc50bcf694023262724eb78c7a326aed880dcdfd369684223223b1349da928a6

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require "rubocop"

module RuboCop
  module Cop
    module GitHub
      class RailsControllerRenderActionSymbol < Base
        extend AutoCorrector

        MSG = "Prefer `render` with string instead of symbol"

        def_node_matcher :render_sym?, <<-PATTERN
          (send nil? {:render :render_to_string} $(sym _))
        PATTERN

        def_node_matcher :render_with_options?, <<-PATTERN
          (send nil? {:render :render_to_string} (hash $...))
        PATTERN

        def_node_matcher :action_key?, <<-PATTERN
          (pair (sym {:action :template}) $(sym _))
        PATTERN

        def on_send(node)
          if sym_node = render_sym?(node)
            add_offense(sym_node) do |corrector|
              register_offense(sym_node, node)
            end
          elsif option_pairs = render_with_options?(node)
            option_pairs.each do |pair|
              if sym_node = action_key?(pair)
                register_offense(sym_node, node)
              end
            end
          end
        end

        private

        def register_offense(sym_node, node)
          add_offense(sym_node) do |corrector|
            corrector.replace(node.source_range, "\"#{node.children[0]}\"")
          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_controller_render_action_symbol.rb
rubocop-github-0.19.0 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-github-0.18.0 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb