Class: ThemeCheck::TranslationKeyExists

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

Constant Summary

Constants inherited from Check

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

Instance Attribute Summary

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, #pretty_json

Constructor Details

#initializeTranslationKeyExists

Returns a new instance of TranslationKeyExists.



8
9
10
11
# File 'lib/theme_check/checks/translation_key_exists.rb', line 8

def initialize
  @schema_locales = {}
  @nodes = {}
end

Instance Method Details

#on_document(node) ⇒ Object



13
14
15
# File 'lib/theme_check/checks/translation_key_exists.rb', line 13

def on_document(node)
  @nodes[node.theme_file.name] = []
end

#on_endObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/theme_check/checks/translation_key_exists.rb', line 30

def on_end
  @nodes.each_pair do |_file_name, file_nodes|
    file_nodes.each do |node|
      next unless (key_node = node.children.first)
      next unless key_node.value.is_a?(String)
      next if key_exists?(key_node.value, @theme.default_locale_json.content) || key_exists?(key_node.value, @schema_locales) || ShopifyLiquid::SystemTranslations.include?(key_node.value)
      add_offense(
        @schema_locales.empty? ? "'#{key_node.value}' does not have a matching entry in '#{@theme.default_locale_json.relative_path}'" : "'#{key_node.value}' does not have a matching entry in '#{@theme.default_locale_json.relative_path}' or '#{node.theme_file.relative_path}'",
        node: node,
        markup: key_node.value
      ) do |corrector|
        corrector.add_translation(@theme.default_locale_json, key_node.value.split("."), "TODO")
      end
    end
  end
end

#on_schema(node) ⇒ Object



24
25
26
27
28
# File 'lib/theme_check/checks/translation_key_exists.rb', line 24

def on_schema(node)
  if (schema_locales = node.inner_json&.dig("locales", @theme.default_locale))
    @schema_locales = schema_locales
  end
end

#on_variable(node) ⇒ Object



17
18
19
20
21
22
# File 'lib/theme_check/checks/translation_key_exists.rb', line 17

def on_variable(node)
  return unless @theme.default_locale_json&.content&.is_a?(Hash)
  return unless node.filters.any? { |name, _| name == "t" || name == "translate" }

  @nodes[node.theme_file.name] << node
end