Sha256: 41276ad58fb01fde54629c9e6d4ef0e79d2ffed8514c079e6c85908e8435753c

Contents?: true

Size: 1.35 KB

Versions: 10

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true
module ThemeCheck
  # Recommends using {% liquid ... %} if 3 or more consecutive {% ... %} are found.
  class LiquidTag < LiquidCheck
    severity :suggestion
    category :liquid
    doc "https://shopify.dev/docs/themes/liquid/reference/tags/theme-tags#liquid"

    def initialize(min_consecutive_statements: 10)
      @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

10 entries across 10 versions & 1 rubygems

Version Path
theme-check-0.4.0 lib/theme_check/checks/liquid_tag.rb
theme-check-0.3.3 lib/theme_check/checks/liquid_tag.rb
theme-check-0.3.2 lib/theme_check/checks/liquid_tag.rb
theme-check-0.3.1 lib/theme_check/checks/liquid_tag.rb
theme-check-0.3.0 lib/theme_check/checks/liquid_tag.rb
theme-check-0.2.2 lib/theme_check/checks/liquid_tag.rb
theme-check-0.2.0 lib/theme_check/checks/liquid_tag.rb
theme-check-0.1.2 lib/theme_check/checks/liquid_tag.rb
theme-check-0.1.1 lib/theme_check/checks/liquid_tag.rb
theme-check-0.1.0 lib/theme_check/checks/liquid_tag.rb