Class: ThemeCheck::MatchingSchemaTranslations

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

Constant Summary

Constants inherited from Check

Check::CATEGORIES, Check::SEVERITIES

Instance Attribute Summary

Attributes inherited from Check

#offenses, #options, #theme

Instance Method Summary collapse

Methods inherited from LiquidCheck

#add_offense

Methods included from ChecksTracking

#inherited

Methods included from ParsingHelpers

#outside_of_strings

Methods inherited from Check

all, category, #category, #code_name, doc, #doc, #ignore!, #ignored?, severity, #severity, #to_s, #unignore!

Methods included from JsonHelpers

#format_json_parse_error

Instance Method Details

#on_schema(node) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/theme_check/checks/matching_schema_translations.rb', line 7

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

  # Get all locales used in the schema
  used_locales = Set.new([theme.default_locale])
  visit_object(schema) do |_, locales|
    used_locales += locales
  end
  used_locales = used_locales.to_a

  # Check all used locales are defined in each localized keys
  visit_object(schema) do |key, locales|
    missing = used_locales - locales
    if missing.any?
      add_offense("#{key} missing translations for #{missing.join(', ')}", node: node)
    end
  end

  check_locales(schema["locales"], node: node)

rescue JSON::ParserError
  # Ignored, handled in ValidSchema.
end