Sha256: 69767fff982b73c371e6bd0e7f3b61dbd1f5e1a22f71c2faad9e824aac02ab83

Contents?: true

Size: 1.29 KB

Versions: 25

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module PlatformosCheck
  # 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
      return unless node.parent.block? && !node.value.strip.empty?

      reset_consecutive_statements
    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
      add_offense("Use {% liquid ... %} to write multiple tags", node: @first_statement) if @consecutive_statements >= @min_consecutive_statements
      @first_statement = nil
      @consecutive_statements = 0
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
platformos-check-0.2.0 lib/platformos_check/checks/liquid_tag.rb
platformos-check-0.1.0 lib/platformos_check/checks/liquid_tag.rb
platformos-check-0.0.3 lib/platformos_check/checks/liquid_tag.rb
platformos-check-0.0.2 lib/platformos_check/checks/liquid_tag.rb
platformos-check-0.0.1 lib/platformos_check/checks/liquid_tag.rb