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

Constructor Details

#initializeJSON

Returns a new instance of JSON.



60
61
62
63
64
65
# File 'lib/querly/cli/formatter.rb', line 60

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

Instance Method Details

#as_jsonObject



88
89
90
91
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
# File 'lib/querly/cli/formatter.rb', line 88

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,
          },
          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



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

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

#fatal_error(error) ⇒ Object



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

def fatal_error(error)
  super
  @fatal = error
end

#finishObject



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

def finish
  STDOUT.print as_json.to_json
end

#issue_found(script, rule, pair) ⇒ Object



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

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

#script_error(path, error) ⇒ Object



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

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