Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- added a large number of options from jshint

- added rake task options for all the jshint variables
  • Loading branch information...
commit e98d72b479f85f9c22a03101757fd8fe37f9bc5d 1 parent 7f470c3
@whoward authored
View
16 lib/jslint-v8/rake_task.rb
@@ -18,6 +18,15 @@ class RakeTask < ::Rake::TaskLib
# output stream for this task
attr_accessor :output_stream
+ # custom lint options for the task
+ attr_accessor :lint_options
+
+ # metaprogrammatically define accessors for each lint option expected
+ Runner::DefaultOptions.keys.each do |key|
+ define_method(key) { lint_options[key] }
+ define_method("#{key}=") {|value| lint_options[key] = value }
+ end
+
def initialize
# a default name
@name = "lint"
@@ -34,6 +43,9 @@ def initialize
# by default use standard output for writing information
@output_stream = STDOUT
+ # by default provide no overridden lint options
+ @lint_options = {}
+
# if a block was given allow the block to call elements on this object
yield self if block_given?
@@ -72,7 +84,9 @@ def files_to_run
private
def runner
- JSLintV8::Runner.new(files_to_run)
+ runner = JSLintV8::Runner.new(files_to_run)
+ runner.jslint_options.merge!(lint_options)
+ runner
end
end
View
108 lib/jslint-v8/runner.rb
@@ -4,6 +4,100 @@ module JSLintV8
class Runner
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
+
attr_reader :file_list
def initialize(files)
@@ -63,19 +157,7 @@ def jslint_result
end
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
- }
+ @jslint_options ||= DefaultOptions.dup
end
end
View
11 test/test_runner.rb
@@ -43,6 +43,17 @@ def test_runtime_caching
assert_equal @runner.runtime.object_id, first.object_id
end
+ def test_overrideable_options
+ # have same values as default options
+ assert_equal @runner.jslint_options, JSLintV8::Runner::DefaultOptions
+
+ # but not be the options
+ assert_not_equal @runner.jslint_options.object_id, JSLintV8::Runner::DefaultOptions.object_id
+
+ # but be memoized
+ assert_equal @runner.jslint_options.object_id, @runner.jslint_options.object_id
+ end
+
def test_jslint_function_proxy
assert_not_nil @runner.jslint_function
end
View
28 test/test_task.rb
@@ -52,23 +52,38 @@ def test_default_output_stream
assert_equal STDOUT, task.output_stream
end
+ def test_default_lint_options
+ task = JSLintV8::RakeTask.new
+
+ assert_equal({}, task.lint_options)
+ end
+
def test_creation_block
tempfile = Tempfile.new("foo")
task = JSLintV8::RakeTask.new do |task|
task.name = "foo"
+
task.description = "Points out the bad codezzzz"
+
task.include_pattern = "js/**/*.js"
task.exclude_pattern = "js/**/*.txt"
+
task.output_stream = tempfile
+
+ task.browser = true
+ task.strict = true
end
rake_task = Rake.application.lookup("foo")
+ expected_options = {"browser" => true, "strict" => true}
+
assert_equal "foo", rake_task.name
assert_equal "Points out the bad codezzzz", rake_task.comment
assert_equal "js/**/*.js", task.include_pattern
assert_equal "js/**/*.txt", task.exclude_pattern
+ assert_equal expected_options, task.lint_options
assert_equal tempfile.object_id, task.output_stream.object_id
end
@@ -83,6 +98,19 @@ def test_files_to_run
assert_equal expected_files, task.files_to_run
end
+ def test_custom_lint_options
+ task = JSLintV8::RakeTask.new do |task|
+ task.browser = true
+ task.evil = true
+ end
+
+ runner = task.send(:runner)
+
+ assert_equal true, runner.jslint_options["browser"]
+ assert_equal true, runner.jslint_options["evil"]
+ assert_equal false, runner.jslint_options["rhino"]
+ end
+
def test_valid_output
result = String.new
Please sign in to comment.
Something went wrong with that request. Please try again.