Sha256: bacf5d0b6a3ee0b25e60411847fe06a23a035a929592cc5c642266b4e93e8d4b

Contents?: true

Size: 1.29 KB

Versions: 18

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true
module ThemeCheck
  # Reports deeply nested {% include ... %} or {% render ... %}
  class NestedSnippet < LiquidCheck
    severity :suggestion
    category :liquid
    doc docs_url(__FILE__)

    class TemplateInfo < Struct.new(:includes)
      def with_deep_nested(templates, max, current_level = 0)
        includes.each do |node|
          if current_level >= max
            yield node
          else
            template_name = "snippets/#{node.value.template_name_expr}"
            templates[template_name]
              &.with_deep_nested(templates, max, current_level + 1) { yield node }
          end
        end
      end
    end

    def initialize(max_nesting_level: 3)
      @max_nesting_level = max_nesting_level
      @templates = {}
    end

    def on_document(node)
      @templates[node.theme_file.name] = TemplateInfo.new(Set.new)
    end

    def on_include(node)
      if node.value.template_name_expr.is_a?(String)
        @templates[node.theme_file.name].includes << node
      end
    end
    alias_method :on_render, :on_include

    def on_end
      @templates.each_pair do |_, info|
        info.with_deep_nested(@templates, @max_nesting_level) do |node|
          add_offense("Too many nested snippets", node: node)
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

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