Sha256: 75c69ea2dba098ed14d5f636c2e92b097b3ba1aa5db2ff3d008848ed2d55d406

Contents?: true

Size: 1.3 KB

Versions: 21

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true
module ThemeCheck
  # Recommends using {% liquid ... %} if 5 or more consecutive {% ... %} are found.
  class LiquidTag < LiquidCheck
    severity :suggestion
    category :liquid
    doc docs_url(__FILE__)

    def initialize(min_consecutive_statements: 5)
      @first_statement = nil
      @consecutive_statements = 0
      @min_consecutive_statements = min_consecutive_statements
    end

    def on_tag(node)
      if node.inside_liquid_tag?
        reset_consecutive_statements
      # Ignore comments
      elsif !node.comment?
        increment_consecutive_statements(node)
      end
    end

    def on_string(node)
      # Only reset the counter on outputted strings, and ignore empty line-breaks
      if node.parent.block? && !node.value.strip.empty?
        reset_consecutive_statements
      end
    end

    def after_document(_node)
      reset_consecutive_statements
    end

    def increment_consecutive_statements(node)
      @first_statement ||= node
      @consecutive_statements += 1
    end

    def reset_consecutive_statements
      if @consecutive_statements >= @min_consecutive_statements
        add_offense("Use {% liquid ... %} to write multiple tags", node: @first_statement)
      end
      @first_statement = nil
      @consecutive_statements = 0
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
theme-check-1.15.0 lib/theme_check/checks/liquid_tag.rb
theme-check-1.14.0 lib/theme_check/checks/liquid_tag.rb
theme-check-1.13.0 lib/theme_check/checks/liquid_tag.rb
theme-check-1.12.1 lib/theme_check/checks/liquid_tag.rb
theme-check-1.12.0 lib/theme_check/checks/liquid_tag.rb
theme-check-1.11.0 lib/theme_check/checks/liquid_tag.rb
theme-check-1.10.3 lib/theme_check/checks/liquid_tag.rb
theme-check-1.10.2 lib/theme_check/checks/liquid_tag.rb
theme-check-1.10.1 lib/theme_check/checks/liquid_tag.rb
theme-check-1.10.0 lib/theme_check/checks/liquid_tag.rb
theme-check-1.9.2 lib/theme_check/checks/liquid_tag.rb
theme-check-1.9.1 lib/theme_check/checks/liquid_tag.rb
theme-check-1.9.0 lib/theme_check/checks/liquid_tag.rb
theme-check-1.8.0 lib/theme_check/checks/liquid_tag.rb
theme-check-1.7.2 lib/theme_check/checks/liquid_tag.rb
theme-check-1.7.1 lib/theme_check/checks/liquid_tag.rb
theme-check-1.7.0 lib/theme_check/checks/liquid_tag.rb
theme-check-1.6.2 lib/theme_check/checks/liquid_tag.rb
theme-check-1.6.1 lib/theme_check/checks/liquid_tag.rb
theme-check-1.6.0 lib/theme_check/checks/liquid_tag.rb