Sha256: d2790c7dd4ea7a1f7140bde710842313980f219e6242b7bc7d3d4b50ceaffbcf

Contents?: true

Size: 1.11 KB

Versions: 29

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require "rubocop"

module RuboCop
  module Cop
    module GitHub
      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

29 entries across 29 versions & 2 rubygems

Version Path
rubocop-springest-0.7.5 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.7.4 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.7.3 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.7.2 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-github-0.16.0 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-github-0.15.0 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.7.1 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-github-0.14.0 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.7.0 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.6.9 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.6.8 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-github-0.13.0 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.6.7 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.6.6 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.6.5 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.6.4 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.6.3 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-github-0.12.0 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-github-0.11.0 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
rubocop-springest-0.6.2 lib/rubocop/cop/github/rails_controller_render_action_symbol.rb