Sha256: 9d2a4e6b604e010f9782b9a8cb9cad51c42cfc549a4888e048d1c081b495e1bf

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require "rubocop"

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

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

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

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

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

          if args = render_str?(node)
            node, path = args
            unless resolve_template(path.to_s)
              add_offense(node, :expression, "Template could not be found")
            end
          elsif pairs = render_options?(node)
            if pair = pairs.detect { |p| render_key?(p) }
              key, node, path = render_key?(pair)

              case key
              when :action, :template
                unless resolve_template(path.to_s)
                  add_offense(node, :expression, "Template could not be found")
                end
              when :partial
                unless resolve_partial(path.to_s)
                  add_offense(node, :expression, "Partial template could not be found")
                end
              end
            end
          end
        end

        def resolve_template(path)
          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

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

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
rubocop-shakr-0.6.0 lib/rubocop/cop/github/rails_controller_render_paths_exist.rb
rubocop-shakr-0.5.0 lib/rubocop/cop/github/rails_controller_render_paths_exist.rb
rubocop-github-0.5.0 lib/rubocop/cop/github/rails_controller_render_paths_exist.rb
rubocop-shakr-0.1.0 lib/rubocop/cop/github/rails_controller_render_paths_exist.rb
rubocop-github-0.4.2 lib/rubocop/cop/github/rails_controller_render_paths_exist.rb
rubocop-github-0.4.1 lib/rubocop/cop/github/rails_controller_render_paths_exist.rb
rubocop-github-0.4.0 lib/rubocop/cop/github/rails_controller_render_paths_exist.rb