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.



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

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

Instance Method Details

#as_jsonObject



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
150
151
152
153
154
# 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) ⇒ Object



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

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

#fatal_error(error) ⇒ Object



92
93
94
95
# File 'lib/querly/cli/formatter.rb', line 92

def fatal_error(error)
  super
  @fatal = error
end

#finishObject



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

def finish
  STDOUT.print as_json.to_json
end

#issue_found(script, rule, pair) ⇒ Object



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

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

#script_error(path, error) ⇒ Object



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

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