Class: RuboCop::Formatter::FormatterSet

Inherits:
Array
  • Object
show all
Defined in:
lib/rubocop/formatter/formatter_set.rb

Overview

This is a collection of formatters. A FormatterSet can hold multiple formatter instances and provides transparent formatter API methods which invoke same method of each formatters.

Constant Summary collapse

BUILTIN_FORMATTERS_FOR_KEYS =
{
  'progress' => ProgressFormatter,
  'simple'   => SimpleTextFormatter,
  'clang'    => ClangStyleFormatter,
  'fuubar'   => FuubarStyleFormatter,
  'emacs'    => EmacsStyleFormatter,
  'json'     => JSONFormatter,
  'html'     => HTMLFormatter,
  'files'    => FileListFormatter,
  'offenses' => OffenseCountFormatter,
  'disabled' => DisabledLinesFormatter
}
FORMATTER_APIS =
[:started, :finished]

Instance Method Summary collapse

Instance Method Details

#add_formatter(formatter_type, output_path = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubocop/formatter/formatter_set.rb', line 55

def add_formatter(formatter_type, output_path = nil)
  formatter_class = case formatter_type
                    when Class
                      formatter_type
                    when /\A[A-Z]/
                      custom_formatter_class(formatter_type)
                    else
                      builtin_formatter_class(formatter_type)
                    end

  if output_path
    dir_path = File.dirname(output_path)
    FileUtils.mkdir_p(dir_path) unless File.exist?(dir_path)
    output = File.open(output_path, 'w')
  else
    output = $stdout
  end

  self << formatter_class.new(output)
end

#close_output_filesObject



76
77
78
79
80
# File 'lib/rubocop/formatter/formatter_set.rb', line 76

def close_output_files
  each do |formatter|
    formatter.output.close if formatter.output.is_a?(File)
  end
end

#file_finished(file, offenses) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubocop/formatter/formatter_set.rb', line 42

def file_finished(file, offenses)
  if @cop_disabled_line_ranges[file].any? &&
     # Don't check unneeded disable if --only or --except option is
     # given, because these options override configuration.
     @excepted_cops.empty? && @only_cops.empty?
    cop = Cop::Lint::UnneededDisable.new
    cop.check(file, offenses, @cop_disabled_line_ranges, @comments)
    offenses += cop.offenses
  end
  offenses = offenses.sort.reject(&:disabled?)
  each { |f| f.file_finished(file, offenses) }
end

#file_started(file, options) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/formatter/formatter_set.rb', line 32

def file_started(file, options)
  @cop_disabled_line_ranges ||= {}
  @cop_disabled_line_ranges[file] = options[:cop_disabled_line_ranges]
  @comments ||= {}
  @comments[file] = options[:comments]
  @excepted_cops = options[:excepted_cops] || []
  @only_cops = options[:only_cops] || []
  each { |f| f.file_started(file, options) }
end