Sha256: b944cb6a95549e0c2216d1c50eb68e6390c98f5d350be380749df377c58a879f

Contents?: true

Size: 1.12 KB

Versions: 12

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'rubocop'

module RuboCop
  module Cop
    module Standard
      class RailsControllerRenderActionSymbol < Cop
        MSG = 'Prefer `render` with string instead of symbol'

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

        def_node_matcher :render_with_options?, <<-PATTERN
          (send nil? :render (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, location: :expression)
          elsif (option_pairs = render_with_options?(node))
            option_pairs.each do |pair|
              if (sym_node = action_key?(pair))
                add_offense(sym_node, location: :expression)
              end
            end
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(node.source_range, "\"#{node.children[0]}\"")
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-standard-4.2.0 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
rubocop-standard-4.1.0 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
rubocop-standard-4.0.4 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
rubocop-standard-4.0.3 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
rubocop-standard-4.0.2 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
rubocop-standard-4.0.1 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
rubocop-standard-4.0.0 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
rubocop-standard-3.2.0 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
rubocop-standard-3.1.2 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
rubocop-standard-3.1.1 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
rubocop-standard-3.1.0 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb
rubocop-standard-3.0.0 lib/rubocop/cop/standard/rails/rails_controller_render_action_symbol.rb