Sha256: ff39245cb613acbd1ad5db0655ebfba7603b4918bb0190e41c4f0d2410c90dec

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'rubocop'

module RuboCop
  module Cop
    module Standard
      class RailsControllerRenderShorthand < Cop
        MSG = 'Prefer `render` template shorthand'

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

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

        def_node_matcher :str, <<-PATTERN
          ({str sym} $_)
        PATTERN

        def investigate(*)
          @autocorrect = {}
        end

        def autocorrect(node)
          @autocorrect[node]
        end

        def on_send(node)
          if option_pairs = render_with_options?(node)
            option_pairs.each do |pair|
              if value_node = action_key?(pair)
                comma = option_pairs.length > 1 ? ', ' : ''
                corrected_source = node.source
                  .sub(/#{pair.source}(,\s*)?/, '')
                  .sub('render ', "render \"#{str(value_node)}\"#{comma}")

                @autocorrect[node] = lambda do |corrector|
                  corrector.replace(node.source_range, corrected_source)
                end
                add_offense(node, location: :expression, message: "Use `#{corrected_source}` instead")
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-standard-1.15.1 lib/rubocop/cop/standard/rails_controller_render_shorthand.rb
rubocop-standard-1.15.0 lib/rubocop/cop/standard/rails_controller_render_shorthand.rb
rubocop-standard-1.14.0 lib/rubocop/cop/standard/rails_controller_render_shorthand.rb