Sha256: a466a510ed8e5888bdb6543d553c121b89a3a3fe9695b6b066bb7edd58a4c475

Contents?: true

Size: 1.2 KB

Versions: 19

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true
module ThemeCheck
  class AssetUrlFilters < LiquidCheck
    severity :suggestion
    categories :liquid, :performance
    doc docs_url(__FILE__)

    HTML_FILTERS = [
      'stylesheet_tag',
      'script_tag',
      'img_tag',
    ]
    ASSET_URL_FILTERS = [
      'asset_url',
      'asset_img_url',
      'file_img_url',
      'file_url',
      'global_asset_url',
      'img_url',
      'payment_type_img_url',
      'shopify_asset_url',
    ]

    def on_variable(node)
      record_variable_offense(node)
    end

    private

    def record_variable_offense(variable_node)
      # We flag HTML tags with URLs not hosted by Shopify
      return if !html_resource_drop?(variable_node) || variable_hosted_by_shopify?(variable_node)
      add_offense("Use one of the asset_url filters to serve assets", node: variable_node)
    end

    def html_resource_drop?(variable_node)
      variable_node.value.filters
        .any? { |(filter_name, *_filter_args)| HTML_FILTERS.include?(filter_name) }
    end

    def variable_hosted_by_shopify?(variable_node)
      variable_node.value.filters
        .any? { |(filter_name, *_filter_args)| ASSET_URL_FILTERS.include?(filter_name) }
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
theme-check-1.8.0 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.7.2 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.7.1 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.7.0 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.6.2 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.6.1 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.6.0 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.5.2 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.5.1 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.5.0 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.4.0 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.3.0 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.2.0 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.1.0 lib/theme_check/checks/asset_url_filters.rb
theme-check-1.0.0 lib/theme_check/checks/asset_url_filters.rb
theme-check-0.10.2 lib/theme_check/checks/asset_url_filters.rb
theme-check-0.10.1 lib/theme_check/checks/asset_url_filters.rb
theme-check-0.10.0 lib/theme_check/checks/asset_url_filters.rb
theme-check-0.9.1 lib/theme_check/checks/asset_url_filters.rb