Class: Juicer::JsLint

Inherits:
Object
  • Object
show all
Includes:
Binary
Defined in:
lib/juicer/jslint.rb

Overview

A Ruby API to Douglas Crockfords genious JsLint program www.jslint.com/

JsLint parses JavaScript code and identifies (potential) problems. Effectively, JsLint defines a subset of JavaScript which is safe to use, and among other things make code minification a substantially less dangerous task.

Defined Under Namespace

Classes: Error, Report

Instance Method Summary collapse

Methods included from Binary

#execute, #get_opt, #locate, #method_missing, #options, #path, #set_opt, #set_opts

Constructor Details

#initialize(options = {}) ⇒ JsLint

Returns a new instance of JsLint.



16
17
18
19
# File 'lib/juicer/jslint.rb', line 16

def initialize(options = {})
  super(options[:java] || "java")
  path << options[:bin_path] if options[:bin_path]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Juicer::Binary

Instance Method Details

#check(file) ⇒ Object

Checks if a files has problems. Also includes experimental support for CSS files. CSS files should begin with the line @charset “UTF-8”;

Returns a Juicer::JsLint::Report object

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/juicer/jslint.rb', line 39

def check(file)
  rhino_jar = rhino
  js_file = locate_lib

  raise FileNotFoundError.new("Unable to locate Rhino jar '#{rhino_jar}'") if !rhino_jar || !File.exists?(rhino_jar)
  raise FileNotFoundError.new("Unable to locate JsLint '#{js_file}'") if !js_file || !File.exists?(js_file)
  raise FileNotFoundError.new("Unable to locate input file '#{file}'") unless File.exists?(file)

  lines = execute("-jar", rhino, locate_lib, file).split("\n")
  return Report.new if lines.length == 1 && lines[0] =~ /jslint: No problems/

  report = Report.new
  lines = lines.reject { |line| !line || "#{line}".strip == "" }
  report.add_error(lines.shift, lines.shift) while lines.length > 0

  return report
end

#commandObject

Constructs the command to use



23
24
25
26
27
28
29
30
31
# File 'lib/juicer/jslint.rb', line 23

def command
  java_opts = []
  if ENV['JAVA_OPTS'].nil?
    java_opts = []
  else
    java_opts = ENV['JAVA_OPTS'].split(" ")
  end
  @command = ([@binary] + java_opts).flatten
end

#locate_libObject



62
63
64
65
# File 'lib/juicer/jslint.rb', line 62

def locate_lib
  files = locate("**/jslint-*.js", "JSLINT_HOME")
  !files || files.empty? ? nil : files.sort.last
end

#rhinoObject



57
58
59
60
# File 'lib/juicer/jslint.rb', line 57

def rhino
  files = locate("**/rhino*.jar", "RHINO_HOME")
  !files || files.empty? ? nil : files.sort.last
end