Class: JSLintV8::Runner

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

Constant Summary collapse

JSLintLibraryFilename =
File.expand_path("js/jslint.js", File.dirname(__FILE__))
DefaultOptions =
{
   "asi" => false,
   "bitwise" => true,
   "boss" => false,
   "browser" => false,
   "couch" => false,
   "curly" => false,
   "debug" => false,
   "devel" => false,
   "dojo" => false,
   "eqeqeq" => true,
   "eqnull" => false,
   "es5" => false,
   "evil" => false,
   "expr" => false,
   "forin" => false,
   "globalstrict" => false,
   "immed" => true,
   "jquery" => false,
   "latedef" => false,
   "laxbreak" => false,
   "loopfunc" => false,
   "mootools" => false,
   "newcap" => true,
   "noarg" => false,
   "node" => false,
   "noempty" => false,
   "nonew" => false,
   "nomen" => true,
   "onevar" => true,
   "passfail" => false,
   "plusplus" => true,
   "prototypejs" => false,
   "regexdash" => false,
   "regexp" => true,
   "rhino" => false,
   "undef" => true,
   "scripturl" => false,
   "shadow" => false,
   "strict" => false,
   "sub" => false,
   "supernew" => false,
   "trailing" => false,
   "white" => false,
   "wsh" => false
}.freeze
OptionDescriptions =
{
   "asi"          => "if automatic semicolon insertion should be tolerated",
   "bitwise"      => "if bitwise operators should not be allowed",
   "boss"         => "if advanced usage of assignments should be allowed",
   "browser"      => "if the standard browser globals should be predefined",
   "couch"        => "if CouchDB globals should be predefined",
   "curly"        => "if curly braces around blocks should be required (even in if/for/while)",
   "debug"        => "if debugger statements should be allowed",
   "devel"        => "if logging globals should be predefined (console, alert, etc.)",
   "dojo"         => "if Dojo Toolkit globals should be predefined",
   "eqeqeq"       => "if === should be required",
   "eqnull"       => "if == null comparisons should be tolerated",
   "es5"          => "if ES5 syntax should be allowed",
   "evil"         => "if eval should be allowed",
   "expr"         => "if ExpressionStatement should be allowed as Programs",
   "forin"        => "if for in statements must filter",
   "globalstrict" => "if global \"use strict\"; should be allowed (also enables 'strict')",
   "immed"        => "if immediate invocations must be wrapped in parens",
   "jquery"       => "if jQuery globals should be predefined",
   "latedef"      => "if the use before definition should not be tolerated",
   "laxbreak"     => "if line breaks should not be checked",
   "loopfunc"     => "if functions should be allowed to be defined within loops",
   "mootools"     => "if MooTools globals should be predefined",
   "newcap"       => "if constructor names must be capitalized",
   "noarg"        => "if arguments.caller and arguments.callee should be disallowed",
   "node"         => "if the Node.js environment globals should be predefined",
   "noempty"      => "if empty blocks should be disallowed",
   "nonew"        => "if using `new` for side-effects should be disallowed",
   "nomen"        => "if names should be checked",
   "onevar"       => "if only one var statement per function should be allowed",
   "passfail"     => "if the scan should stop on first error",
   "plusplus"     => "if increment/decrement should not be allowed",
   "prototypejs"  => "if Prototype and Scriptaculous globals should be predefined",
   "regexdash"    => "if unescaped last dash (-) inside brackets should be tolerated",
   "regexp"       => "if the . should not be allowed in regexp literals",
   "rhino"        => "if the Rhino environment globals should be predefined",
   "undef"        => "if variables should be declared before used",
   "scripturl"    => "if script-targeted URLs should be tolerated",
   "shadow"       => "if variable shadowing should be tolerated",
   "strict"       => "require the \"use strict\"; pragma",
   "sub"          => "if all forms of subscript notation are tolerated",
   "supernew"     => "if `new function () { ... };` and `new Object;` should be tolerated",
   "trailing"     => "if trailing whitespace rules apply",
   "white"        => "if strict whitespace rules apply",
   "wsh"          => "if the Windows Scripting Host environment globals should be predefined",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ Runner

Returns a new instance of Runner.



103
104
105
106
107
108
109
# File 'lib/jslint-v8/runner.rb', line 103

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.



101
102
103
# File 'lib/jslint-v8/runner.rb', line 101

def file_list
  @file_list
end

Instance Method Details

#jslint(source_code) ⇒ Object



144
145
146
147
# File 'lib/jslint-v8/runner.rb', line 144

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

#jslint_functionObject



149
150
151
# File 'lib/jslint-v8/runner.rb', line 149

def jslint_function
   runtime["JSHINT"];
end

#jslint_optionsObject



159
160
161
# File 'lib/jslint-v8/runner.rb', line 159

def jslint_options
   @jslint_options ||= DefaultOptions.dup
end

#jslint_resultObject



153
154
155
156
157
# File 'lib/jslint-v8/runner.rb', line 153

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

#runObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/jslint-v8/runner.rb', line 111

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



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/jslint-v8/runner.rb', line 132

def runtime
   @runtime ||= lambda do
      runtime = V8::Context.new

      # load the jslint library into the runtime
      runtime.eval(File.read JSLintLibraryFilename)

      # return the runtime
      runtime
   end.call
end