Class: Maximus::Lint

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/maximus/lint.rb

Overview

Since:

  • 0.1.0

Direct Known Subclasses

Brakeman, Jshint, Railsbp, Rubocop, Scsslint

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#check_default_config_path, #edit_yaml, #file_count, #file_list, #is_rails?, #lines_added_to_range, #node_module_exists, #path_exists, #prompt, #reporter_path, #root_dir, #truthy

Constructor Details

#initialize(opts = {}) ⇒ void

Perform a lint of relevant code

All defined lints require a “result” method

end

Inherits settings from Config#initialize

Examples:

the result method in the child class

def result
  @task = __method__.to_s
  @path ||= 'path/or/**/glob/to/files''
  lint_data = JSON.parse(`some-command-line-linter`)
  @output[:files_inspected] ||= files_inspected(extension, delimiter, base_path_replacement)
  refine data_from_output

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) options passed directly to the lint

  • git_files (Hash)

    a customizable set of options

  • file_paths (Hash)

    a customizable set of options

Options Hash (opts):

  • :config (Config object)

    custom Maximus::Config object

Since:

  • 0.1.0


33
34
35
36
37
38
39
40
41
42
# File 'lib/maximus/lint.rb', line 33

def initialize(opts = {})

  # Only run the config once
  @@config ||= opts[:config] || Maximus::Config.new(opts)
  @settings ||= @@config.settings

  @git_files = opts[:git_files]
  @path = opts[:file_paths] || @settings[:file_paths]
  @output = {}
end

Instance Attribute Details

#outputObject

Since:

  • 0.1.0


7
8
9
# File 'lib/maximus/lint.rb', line 7

def output
  @output
end

Instance Method Details

#refine(data) ⇒ Hash

Convert raw data into warnings, errors, conventions or refactors. Use this wisely.

Parameters:

  • data (Hash)

    unfiltered lint data

Returns:

  • (Hash)

    refined lint data and all the other bells and whistles

Since:

  • 0.1.0


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/maximus/lint.rb', line 48

def refine(data)
  # Prevent abortive empty JSON.parse error
  data = '{}' if data.blank?
  return puts "Error from #{@task}: #{data}" if data.is_a?(String) && data.include?('No such')

  data = data.is_a?(String) ? JSON.parse(data) : data

  @output[:relevant_lints] = relevant_lints( data, @git_files ) unless @git_files.blank?
  unless @settings[:commit].blank?
    data = @output[:relevant_lints]
  end

  lint_warnings = []
  lint_errors = []
  lint_conventions = []
  lint_refactors = []
  unless data.blank?
    data.each do |filename, error_list|
      error_list.each do |message|
        # so that :raw_data remains unaffected
        message = message.clone
        message.delete('length')
        message['filename'] = filename
        if message['severity'] == 'warning'
          message.delete('severity')
          lint_warnings << message
        elsif message['severity'] == 'error'
          message.delete('severity')
          lint_errors << message
        elsif message['severity'] == 'convention'
          message.delete('severity')
          lint_conventions << message
        elsif message['severity'] == 'refactor'
          message.delete('severity')
          lint_refactors << message
        end
      end
    end
  end
  @output[:lint_errors] = lint_errors
  @output[:lint_warnings] = lint_warnings
  @output[:lint_conventions] = lint_conventions
  @output[:lint_refactors] = lint_refactors
  lint_count = (lint_errors.length + lint_warnings.length + lint_conventions.length + lint_refactors.length)
  if @@config.is_dev?
    puts lint_dev_format(data) unless data.blank?
    puts lint_summarize
    lint_ceiling lint_count
  else
    @@config.log.info lint_summarize
    # Because this should be returned in the format it was received
    @output[:raw_data] = data.to_json
  end
  @@config.destroy_temp(@task)
  @output
end