Sha256: 85daf6a2e6c1125df616c92b3363900fc664adefcda4341a8d52adc0d9be2a76

Contents?: true

Size: 1.62 KB

Versions: 29

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require "rubocop"

module RuboCop
  module Cop
    module GitHub
      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
            unless resolve_partial(path.to_s)
              add_offense(node, location: :expression, message: "Partial template could not be found")
            end
          elsif pairs = render_options?(node)
            if pair = pairs.detect { |p| partial_key?(p) }
              node, path = partial_key?(pair)

              unless resolve_partial(path.to_s)
                add_offense(node, location: :expression, message: "Partial template could not be found")
              end
            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

29 entries across 29 versions & 2 rubygems

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