Sha256: fe887bdb53d14095143d6b3f35abfaf102ed65c42d719b08c3fc87df730fe297

Contents?: true

Size: 1.11 KB

Versions: 6

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true
module ThemeCheck
  class TemplateLength < LiquidCheck
    severity :suggestion
    category :liquid
    doc docs_url(__FILE__)

    def initialize(max_length: 600, exclude_schema: true, exclude_stylesheet: true, exclude_javascript: true)
      @max_length = max_length
      @exclude_schema = exclude_schema
      @exclude_stylesheet = exclude_stylesheet
      @exclude_javascript = exclude_javascript
    end

    def on_document(_node)
      @excluded_lines = 0
    end

    def on_schema(node)
      exclude_node_lines(node) if @exclude_schema
    end

    def on_stylesheet(node)
      exclude_node_lines(node) if @exclude_stylesheet
    end

    def on_javascript(node)
      exclude_node_lines(node) if @exclude_javascript
    end

    def after_document(node)
      lines = node.template.source.count("\n") - @excluded_lines
      if lines > @max_length
        add_offense("Template has too many lines [#{lines}/#{@max_length}]", template: node.template)
      end
    end

    private

    def exclude_node_lines(node)
      @excluded_lines += node.value.nodelist.join.count("\n")
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
theme-check-1.6.1 lib/theme_check/checks/template_length.rb
theme-check-1.6.0 lib/theme_check/checks/template_length.rb
theme-check-1.5.2 lib/theme_check/checks/template_length.rb
theme-check-1.5.1 lib/theme_check/checks/template_length.rb
theme-check-1.5.0 lib/theme_check/checks/template_length.rb
theme-check-1.4.0 lib/theme_check/checks/template_length.rb