class Querly::CLI::Formatter::JSON

Public Class Methods

new() click to toggle source
# File lib/querly/cli/formatter.rb, line 69
def initialize
  @issues = []
  @script_errors = []
  @config_errors = []
  @fatal = nil
end

Public Instance Methods

as_json() click to toggle source
# File lib/querly/cli/formatter.rb, line 97
def as_json
  case
  when @fatal
    # Fatal error found
    {
      fatal_error: {
        message: @fatal.inspect,
        backtrace: @fatal.backtrace
      }
    }
  when !@config_errors.empty?
    # Error found during config load
    {
      config_errors: @config_errors.map {|(path, error)|
        {
          path: path.to_s,
          error: {
            message: error.inspect,
            backtrace: error.backtrace
          }
        }
      }
    }
  else
    # Successfully checked
    {
      issues: @issues.map {|(script, rule, pair)|
        {
          script: script.path.to_s,
          rule: {
            id: rule.id,
            messages: rule.messages,
            justifications: rule.justifications,
            examples: rule.examples.map {|example|
              {
                before: example.before,
                after: example.after
              }
            }
          },
          location: {
            start: [pair.node.loc.first_line, pair.node.loc.column],
            end: [pair.node.loc.last_line, pair.node.loc.last_column]
          }
        }
      },
      errors: @script_errors.map {|path, error|
        {
          path: path.to_s,
          error: {
            message: error.inspect,
            backtrace: error.backtrace
          }
        }
      }
    }
  end
end
config_error(path, error) click to toggle source
# File lib/querly/cli/formatter.rb, line 76
def config_error(path, error)
  @config_errors << [path, error]
end
fatal_error(error) click to toggle source
# File lib/querly/cli/formatter.rb, line 92
def fatal_error(error)
  super
  @fatal = error
end
finish() click to toggle source
# File lib/querly/cli/formatter.rb, line 88
def finish
  STDOUT.print as_json.to_json
end
issue_found(script, rule, pair) click to toggle source
# File lib/querly/cli/formatter.rb, line 84
def issue_found(script, rule, pair)
  @issues << [script, rule, pair]
end
script_error(path, error) click to toggle source
# File lib/querly/cli/formatter.rb, line 80
def script_error(path, error)
  @script_errors << [path, error]
end