Class: JSLintJohnson::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/jslint-johnson/runner.rb

Constant Summary collapse

JSLintLibraryFilename =
File.expand_path("js/jslint.js", File.dirname(__FILE__))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
15
16
# File 'lib/jslint-johnson/runner.rb', line 9

def initialize(files)
  if(files.is_a?(Array))
    @file_list = files
  else
    @file_list = [files]
  end

end

Instance Attribute Details

#file_listObject (readonly)

Returns the value of attribute file_list.



7
8
9
# File 'lib/jslint-johnson/runner.rb', line 7

def file_list
  @file_list
end

Instance Method Details

#jslint(source_code) ⇒ Object



51
52
53
54
# File 'lib/jslint-johnson/runner.rb', line 51

def jslint(source_code)
  jslint_function.call(source_code, jslint_options)
  jslint_result
end

#jslint_functionObject



56
57
58
# File 'lib/jslint-johnson/runner.rb', line 56

def jslint_function
  runtime["JSLINT"];
end

#jslint_optionsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jslint-johnson/runner.rb', line 66

def jslint_options
  {
    "bitwise" => true,
    "eqeqeq" => true,
    "immed" => true,
    "newcap" => true,
    "nomen" => true,
    "onevar" => true,
    "plusplus" => true,
    "regexp" => true,
    "rhino" => true,
    "undef" => true,
    "white" => true
  }
end

#jslint_resultObject



60
61
62
63
64
# File 'lib/jslint-johnson/runner.rb', line 60

def jslint_result
  runtime["JSLINT"]["errors"].to_a.compact.map do |error_object|
    JSLintJohnson::LintError.new(error_object)
  end
end

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jslint-johnson/runner.rb', line 18

def run
  # make sure all files exit
  file_list.each do |file|
    raise "file not found: #{file}" unless File.exist?(file)
  end

  result = {}

  file_list.each do |file|
    errors = jslint(File.read(file))

    yield(file, errors) if block_given?

    next if errors.empty?

    result[file] = errors
  end

  result
end

#runtimeObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jslint-johnson/runner.rb', line 39

def runtime
  @runtime ||= lambda do
    runtime = Johnson::Runtime.new

    # load the jslint library into the runtime
    runtime.evaluate("eval(Ruby.File.read('#{JSLintLibraryFilename}'));")

    # return the runtime
    runtime
  end.call
end