Sha256: 36bf5aefe546ac0e9d2e38fdb0cca27b6aa1ba4b570f89d43acabfdb780bf04f

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module PlatformosCheck
  # Recommends replacing `include` for `render`
  class ConvertIncludeToRender < LiquidCheck
    RENDER_INCOMPATIBLE_TAGS = %w[break include].freeze

    severity :suggestion
    category :liquid
    doc docs_url(__FILE__)

    def initialize
      @processed_files = {}
    end

    def on_include(node)
      return if allowed_usecase?(node)

      add_offense("`include` is deprecated - convert it to `render`", node:) do |corrector|
        match = node.markup.match(/(?<include>include\s*)/)
        corrector.replace(node, node.markup.sub(match[:include], 'render '), node.start_index...node.end_index)
      end
    end

    protected

    def allowed_usecase?(node)
      return true if name_is_variable?(node)
      return true if include_node_contains_render_incompatible_tag?(root_node_from_include(node.value.template_name_expr))

      false
    end

    def name_is_variable?(node)
      !node.value.template_name_expr.is_a?(String)
    end

    def include_node_contains_render_incompatible_tag?(node)
      return false if node.nil?

      node.nodelist.any? do |n|
        if RENDER_INCOMPATIBLE_TAGS.include?(n.respond_to?(:tag_name) && n.tag_name)
          true
        elsif n.respond_to?(:nodelist) && n.nodelist
          include_node_contains_render_incompatible_tag?(n)
        else
          false
        end
      end
    end

    def root_node_from_include(path)
      @platformos_app.grouped_files[PlatformosCheck::PartialFile][path]&.parse&.root
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
platformos-check-0.4.14 lib/platformos_check/checks/convert_include_to_render.rb
platformos-check-0.4.13 lib/platformos_check/checks/convert_include_to_render.rb
platformos-check-0.4.12 lib/platformos_check/checks/convert_include_to_render.rb
platformos-check-0.4.11 lib/platformos_check/checks/convert_include_to_render.rb
platformos-check-0.4.10 lib/platformos_check/checks/convert_include_to_render.rb
platformos-check-0.4.9 lib/platformos_check/checks/convert_include_to_render.rb