Class: PlatformosCheck::ParseJsonFormat

Inherits:
LiquidCheck show all
Defined in:
lib/platformos_check/checks/parse_json_format.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, #platformos_app

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

Methods included from JsonHelpers

#format_json_parse_error, #pretty_json

Constructor Details

#initialize(start_level: 0, indent: ' ') ⇒ ParseJsonFormat

Returns a new instance of ParseJsonFormat.



9
10
11
12
13
14
# File 'lib/platformos_check/checks/parse_json_format.rb', line 9

def initialize(start_level: 0, indent: '  ')
  @pretty_json_opts = {
    indent:,
    start_level:
  }
end

Instance Method Details

#on_parse_json(node) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/platformos_check/checks/parse_json_format.rb', line 16

def on_parse_json(node)
  parse_json = node.inner_json
  return if parse_json.nil?

  pretty_parse_json = pretty_json(parse_json, **@pretty_json_opts)
  return unless pretty_parse_json != node.inner_markup

  add_offense(
    "JSON formatting could be improved",
    node:
  ) do |corrector|
    corrector.replace_inner_json(node, parse_json, **@pretty_json_opts)
  end
end