Class: Jshint::Reporters::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/jshint/reporters/default.rb

Overview

Outputs a basic lint report suitable for STDOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results = {}) ⇒ Default

Sets up the output string for the final report

Parameters:

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

    Key value pairs containing the filename and associated errors



11
12
13
14
# File 'lib/jshint/reporters/default.rb', line 11

def initialize(results = {})
  @results = results
  @output = ''
end

Instance Attribute Details

#outputString (readonly)

Returns the report output.

Returns:

  • (String)

    the report output



6
7
8
# File 'lib/jshint/reporters/default.rb', line 6

def output
  @output
end

Instance Method Details

This method returns an undefined value.

Appends new error strings to the Report output

Examples:

foo/bar/baz.js: line 4, col 46, Bad operand.
foo/bar/baz.js: line 39, col 7, Missing semicolon.

Parameters:

  • file (String)

    The filename containing the errors

  • errors (Array)

    The errors for the file



46
47
48
49
50
# File 'lib/jshint/reporters/default.rb', line 46

def print_errors_for_file(file, errors)
  errors.map do |error|
    output << "#{file}: line #{error['line']}, col #{error['character']}, #{error['reason']}\n" unless error.nil?
  end
end

This method returns an undefined value.

Appends a footer summary to the Report output

Examples:

1 error
3 errors

Parameters:

  • len (Fixnum)

    The number of errors in this Report



60
61
62
# File 'lib/jshint/reporters/default.rb', line 60

def print_footer(len)
  output << "\n#{len} error#{len === 1 ? nil : 's'}"
end

#reportString

Loops through all the errors and generates the report

Examples:

foo/bar/baz.js: line 4, col 46, Bad operand.
foo/bar/baz.js: line 39, col 7, Missing semicolon.

2 errors

Returns:

  • (String)

    The default report



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jshint/reporters/default.rb', line 25

def report
  len = 0
  @results.each do |file, errors|
    len += errors.length
    print_errors_for_file(file, errors)
  end
  if output
    print_footer(len)
    output
  end
end