Sha256: dba7360aa6c36846728832fb79313ed3309c7d84bdc482736b65cc7dfeca84bd

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true
module ThemeCheck
  # Reports missing include/render/section liquid file
  class MissingTemplate < LiquidCheck
    severity :suggestion
    category :liquid
    doc docs_url(__FILE__)
    single_file false

    def initialize(ignore_missing: [])
      @ignore_missing = ignore_missing
    end

    def on_include(node)
      snippet = node.value.template_name_expr
      if snippet.is_a?(String)
        add_missing_offense("snippets/#{snippet}", node: node)
      end
    end

    alias_method :on_render, :on_include

    def on_section(node)
      section = node.value.section_name
      add_missing_offense("sections/#{section}", node: node)
    end

    private

    def ignore?(path)
      all_ignored_patterns.any? { |pattern| File.fnmatch?(pattern, path) }
    end

    def all_ignored_patterns
      @all_ignored_patterns ||= @ignore_missing + ignored_patterns
    end

    def add_missing_offense(name, node:)
      path = "#{name}.liquid"
      unless ignore?(path) || theme[name]
        add_offense("'#{path}' is not found", node: node) do |corrector|
          corrector.create_file(@theme.storage, "#{name}.liquid", "")
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
theme-check-1.15.0 lib/theme_check/checks/missing_template.rb
theme-check-1.14.0 lib/theme_check/checks/missing_template.rb
theme-check-1.13.0 lib/theme_check/checks/missing_template.rb
theme-check-1.12.1 lib/theme_check/checks/missing_template.rb
theme-check-1.12.0 lib/theme_check/checks/missing_template.rb
theme-check-1.11.0 lib/theme_check/checks/missing_template.rb