Sha256: bfb8ddc41dba3c1a7df93178b75c6c825a587cad2f726d6ac6890ca1efd9453a

Contents?: true

Size: 1.57 KB

Versions: 12

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require 'rubocop'

module RuboCop
  module Cop
    module Standard
      class RailsViewRenderPathsExist < Cop
        def_node_matcher :render?, <<-PATTERN
          (send nil? :render $...)
        PATTERN

        def_node_matcher :render_str?, <<-PATTERN
          (send nil? :render $(str $_) ...)
        PATTERN

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

        def_node_matcher :partial_key?, <<-PATTERN
          (pair (sym :partial) $(str $_))
        PATTERN

        def on_send(node)
          return unless cop_config['ViewPath']

          if (args = render_str?(node))
            node, path = args
            add_offense(node, location: :expression, message: 'Partial template could not be found') unless resolve_partial(path.to_s)
          elsif (pairs = render_options?(node))
            if (pair = pairs.detect { |p| partial_key?(p) })
              node, path = partial_key?(pair)

              add_offense(node, location: :expression, message: 'Partial template could not be found') unless resolve_partial(path.to_s)
            end
          end
        end

        def resolve_partial(path)
          parts = path.split(File::SEPARATOR)
          parts << "_#{parts.pop}"
          path = parts.join(File::SEPARATOR)

          cop_config['ViewPath'].each do |view_path|
            if (m = Dir[File.join(config.path_relative_to_config(view_path), path) + '*'].first)
              return m
            end
          end
          nil
        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_view_render_paths_exist.rb
rubocop-standard-4.1.0 lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb
rubocop-standard-4.0.4 lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb
rubocop-standard-4.0.3 lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb
rubocop-standard-4.0.2 lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb
rubocop-standard-4.0.1 lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb
rubocop-standard-4.0.0 lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb
rubocop-standard-3.2.0 lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb
rubocop-standard-3.1.2 lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb
rubocop-standard-3.1.1 lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb
rubocop-standard-3.1.0 lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb
rubocop-standard-3.0.0 lib/rubocop/cop/standard/rails/rails_view_render_paths_exist.rb