Class: ThemeCheck::AssetSizeAppBlockJavaScript

Inherits:
LiquidCheck show all
Defined in:
lib/theme_check/checks/asset_size_app_block_javascript.rb

Overview

Reports errors when too much JS is being referenced from a Theme App Extension block

Constant Summary

Constants inherited from Check

Check::CATEGORIES, Check::SEVERITIES, Check::SEVERITY_VALUES

Instance Attribute Summary collapse

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #theme

Instance Method Summary collapse

Methods included from ChecksTracking

#inherited

Methods included from ParsingHelpers

#outside_of_strings

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_theme?

Methods included from JsonHelpers

#format_json_parse_error

Constructor Details

#initialize(threshold_in_bytes: 10_000) ⇒ AssetSizeAppBlockJavaScript

Returns a new instance of AssetSizeAppBlockJavaScript.



16
17
18
# File 'lib/theme_check/checks/asset_size_app_block_javascript.rb', line 16

def initialize(threshold_in_bytes: 10_000)
  @threshold_in_bytes = threshold_in_bytes
end

Instance Attribute Details

#threshold_in_bytesObject (readonly)

Returns the value of attribute threshold_in_bytes.



14
15
16
# File 'lib/theme_check/checks/asset_size_app_block_javascript.rb', line 14

def threshold_in_bytes
  @threshold_in_bytes
end

Instance Method Details

#on_schema(node) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/theme_check/checks/asset_size_app_block_javascript.rb', line 20

def on_schema(node)
  schema = JSON.parse(node.value.nodelist.join)

  if (javascript = schema["javascript"])
    size = asset_size(javascript)
    if size && size > threshold_in_bytes
      add_offense(
        "JavaScript in Theme App Extension blocks exceeds compressed size threshold (#{threshold_in_bytes} Bytes)",
        node: node
      )
    end
  end
rescue JSON::ParserError
  # Ignored, handled in ValidSchema.
end