Class: Querly::CLI::Formatter::JSON

Inherits:
Base
  • Object
show all
Defined in:
lib/querly/cli/formatter.rb

Instance Method Summary collapse

Methods inherited from Base

#config_load, #script_load, #start

Methods included from Querly::Concerns::BacktraceFormatter

#format_backtrace

Constructor Details

#initializeJSON

Returns a new instance of JSON.



64
65
66
67
68
69
# File 'lib/querly/cli/formatter.rb', line 64

def initialize
  @issues = []
  @script_errors = []
  @config_errors = []
  @fatal = nil
end

Instance Method Details

#as_jsonObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/querly/cli/formatter.rb', line 92

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) ⇒ Object



71
72
73
# File 'lib/querly/cli/formatter.rb', line 71

def config_error(path, error)
  @config_errors << [path, error]
end

#fatal_error(error) ⇒ Object



87
88
89
90
# File 'lib/querly/cli/formatter.rb', line 87

def fatal_error(error)
  super
  @fatal = error
end

#finishObject



83
84
85
# File 'lib/querly/cli/formatter.rb', line 83

def finish
  STDOUT.print as_json.to_json
end

#issue_found(script, rule, pair) ⇒ Object



79
80
81
# File 'lib/querly/cli/formatter.rb', line 79

def issue_found(script, rule, pair)
  @issues << [script, rule, pair]
end

#script_error(path, error) ⇒ Object



75
76
77
# File 'lib/querly/cli/formatter.rb', line 75

def script_error(path, error)
  @script_errors << [path, error]
end