Class: JSLint

Inherits:
Object
  • Object
show all
Defined in:
lib/support/jslint/jslint_checker.rb

Constant Summary collapse

OK_REASONS =
[ "Expected an identifier and instead saw 'undefined' (a reserved word).",
"Use '===' to compare with 'null'.",
"Use '!==' to compare with 'null'.",
"Expected an assignment or function call and instead saw an expression.",
"Expected a 'break' statement before 'case'.",
"'e' is already defined." ]
LINT_PATH =
File.join(File.dirname(__FILE__), "lint.js")

Class Method Summary collapse

Class Method Details

.lint_file(file) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/support/jslint/jslint_checker.rb', line 12

def self.lint_file(file)
  begin
    require 'rubygems'
    require 'v8'
  rescue LoadError
    puts "ERROR: Couldn't load therubyracer, which is needed to run JSLint checks. Install via \"gem install therubyracer\", or disable the JS lint checks."
    return []
  end

  errors = []
  V8::Context.new do |context|
    context.load(LINT_PATH)
    context['input'] = lambda{
      File.read(file)
    }

    context['reportErrors'] = lambda{|js_errors|
      js_errors.each do |e|
        next if e.nil? || OK_REASONS.include?(e.reason)

        errors << "\n\e[1;31mJSLINT: #{file}\e[0m"
        errors << "  Error at line #{e['line'].to_i + 1} " + 
          "character #{e['character'].to_i + 1}: \e[1;33m#{e['reason']}\e[0m"
        errors << "#{e['evidence']}"
      end
    }

    context.eval %{
        JSLINT(input(), { evil: true, forin: true, maxerr: 100 });
        reportErrors(JSLINT.errors);
      }
  end

  return errors
end