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

#command, #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:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/juicer/jslint.rb', line 27

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(%Q{-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

#locate_libObject



50
51
52
53
# File 'lib/juicer/jslint.rb', line 50

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

#rhinoObject



45
46
47
48
# File 'lib/juicer/jslint.rb', line 45

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