Class: PlatformosCheck::DeprecatedFilter
- Inherits:
-
LiquidCheck
- Object
- Check
- LiquidCheck
- PlatformosCheck::DeprecatedFilter
- Defined in:
- lib/platformos_check/checks/deprecated_filter.rb
Constant Summary collapse
- MAX_SIZE =
The image_url filter does not accept width or height values greater than this numbr.
5760
- SIZE_REGEX =
/^\d*x\d*$/
- NAMED_SIZES =
{ "pico" => 16, "icon" => 32, "thumb" => 50, "small" => 100, "compact" => 160, "medium" => 240, "large" => 480, "grande" => 600, "original" => 1024, "master" => nil }
Constants inherited from Check
Check::CATEGORIES, Check::SEVERITIES, Check::SEVERITY_VALUES
Instance Attribute Summary
Attributes inherited from Check
#ignored_patterns, #offenses, #options, #platformos_app
Instance Method Summary collapse
- #add_default_offense(node, filter, alternatives) ⇒ Object
- #add_img_url_offense(node) ⇒ Object
- #on_variable(node) ⇒ Object
Methods included from ChecksTracking
Methods included from ParsingHelpers
Methods inherited from Check
#==, #add_offense, all, can_disable, #can_disable?, categories, #categories, category, #code_name, doc, #doc, docs_url, #ignore!, #ignored?, #severity, severity, #severity=, #severity_value, severity_value, single_file, #single_file?, #to_s, #whole_platformos_app?
Methods included from JsonHelpers
#format_json_parse_error, #pretty_json
Instance Method Details
#add_default_offense(node, filter, alternatives) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/platformos_check/checks/deprecated_filter.rb', line 41 def add_default_offense(node, filter, alternatives) alternatives = alternatives.map { |alt| "`#{alt}`" } add_offense( "Deprecated filter `#{filter}`, consider using an alternative: #{alternatives.join(', ')}", node: ) end |
#add_img_url_offense(node) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/platformos_check/checks/deprecated_filter.rb', line 49 def add_img_url_offense(node) img_url_filter = node.filters.find { |filter| filter[0] == "img_url" } _name, img_url_filter_size, img_url_filter_props = img_url_filter size_spec = img_url_filter_size&.dig(0) scale = img_url_filter_props&.delete("scale") # Can't correct those. return add_default_offense(node, 'img_url', ['image_url']) unless (size_spec.nil? || size_spec.is_a?(String)) && (scale.nil? || scale.is_a?(Numeric)) return add_default_offense(node, 'img_url', ['image_url']) if size_spec.is_a?(String) && size_spec !~ SIZE_REGEX && !NAMED_SIZES.key?(size_spec) node_source = node.markup node_start_index = node.start_index match = node_source.match(/img_url[^|]*/) img_url_character_range = (node_start_index + match.begin(0))...(node_start_index + match.end(0)) scale = (scale || 1).to_i width, height = if size_spec.nil? [100, 100] elsif NAMED_SIZES.key?(size_spec) [NAMED_SIZES[size_spec], NAMED_SIZES[size_spec]] else size_spec.split('x') end.map { |v| v.to_i * scale } image_url_filter_params = [ width && width > 0 ? "width: #{[width, MAX_SIZE].min}" : nil, height && height > 0 ? "height: #{[height, MAX_SIZE].min}" : nil ] image_url_filter_params += (img_url_filter_props || {}) .map do |k, v| case v when Liquid::VariableLookup "#{k}: #{v.name}" when String "#{k}: '#{v}'" else "#{k}: #{v}" end end image_url_filter_params = image_url_filter_params .reject(&:nil?) .join(", ") trailing_whitespace = match[0].match(/\s*\Z/)[0] image_url_filter = "image_url" image_url_filter += ": " + image_url_filter_params unless image_url_filter_params.empty? image_url_filter += trailing_whitespace add_offense( "Deprecated filter `img_url`, consider using `image_url`", node:, markup: match[0] ) do |corrector| corrector.replace( node, image_url_filter, img_url_character_range ) end # If anything goes wrong, fail gracefully by returning the default offense. rescue StandardError add_default_offense(node, 'img_url', ['image_url']) end |
#on_variable(node) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/platformos_check/checks/deprecated_filter.rb', line 26 def on_variable(node) used_filters = node.filters.map { |name, *_rest| name } used_filters.each do |filter| alternatives = PlatformosLiquid::DeprecatedFilter.alternatives(filter) next unless alternatives case filter when 'img_url' add_img_url_offense(node) else add_default_offense(node, filter, alternatives) end end end |