jshintrb 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # jshintrb
2
2
  [![Build Status](https://secure.travis-ci.org/stereobooster/jshintrb.png?branch=master)](http://travis-ci.org/stereobooster/jshintrb)
3
3
 
4
- Ruby wrapper for [JSHint](https://github.com/jshint/jshint/). The main difference from [jshint](https://github.com/liquid/jshint_on_rails) it does not depend on Java. Instead, it uses [ExecJS](https://github.com/sstephenson/execjs).
4
+ Ruby wrapper for [JSHint](https://github.com/jshint/jshint/). The main difference from [jshint](https://github.com/liquid/jshint_on_rails) it does not depend on Java. Instead it uses [ExecJS](https://github.com/sstephenson/execjs).
5
5
 
6
6
  ## Installation
7
7
 
@@ -13,45 +13,61 @@ Ensure that your environment has a JavaScript interpreter supported by [ExecJS](
13
13
 
14
14
  ## Usage
15
15
 
16
- require 'jshintrb'
17
-
18
- Jshintrb.lint(File.read("source.js"))
19
- # => array of warnings
20
-
21
- Jshintrb.report(File.read("source.js"))
22
- # => string
23
-
24
- When initializing jshintrb, you can pass options
25
-
26
- Uglifier.new(:undef => true).compile(source)
27
-
28
- # Or
29
- Uglifier.compile(source, :undef => true)
30
-
31
- Available options and their defaults are
32
-
33
- {
34
- :bitwise => true,
35
- :curly => true,
36
- :eqeqeq => true,
37
- :forin => true,
38
- :immed => true,
39
- :latedef => true,
40
- :newcap => true,
41
- :noarg => true,
42
- :noempty => true,
43
- :nonew => true,
44
- :plusplus => true,
45
- :regexp => true,
46
- :undef => true,
47
- :strict => true,
48
- :trailing => true,
49
- :browser => true
50
- }
16
+ ```ruby
17
+ require 'jshintrb'
18
+
19
+ Jshintrb.lint(File.read("source.js"))
20
+ # => array of warnings
21
+
22
+ Jshintrb.report(File.read("source.js"))
23
+ # => string
24
+ ```
25
+
26
+ Or you can use it with rake
27
+
28
+ ```ruby
29
+ require "jshintrb/jshinttask"
30
+ Jshintrb::JshintTask.new :jshint do |t|
31
+ t.pattern = 'javascript/**/*.js'
32
+ t.options = :defaults
33
+ end
34
+ ```
35
+
36
+ When initializing `Jshintrb`, you can pass options
37
+
38
+ ```ruby
39
+ Jshintrb.new(:undef => true).compile(source)
40
+ # Or
41
+ Jshintrb.compile(source, :undef => true)
42
+ ```
43
+
44
+ [List of all available options](http://www.jshint.com/options/)
45
+
46
+ If you pass `:defaults` as option, it is the same as if you pass following
47
+
48
+ ```
49
+ {
50
+ :bitwise => true,
51
+ :curly => true,
52
+ :eqeqeq => true,
53
+ :forin => true,
54
+ :immed => true,
55
+ :latedef => true,
56
+ :newcap => true,
57
+ :noarg => true,
58
+ :noempty => true,
59
+ :nonew => true,
60
+ :plusplus => true,
61
+ :regexp => true,
62
+ :undef => true,
63
+ :strict => true,
64
+ :trailing => true,
65
+ :browser => true
66
+ }
67
+ ```
51
68
 
52
69
  ## TODO
53
70
 
54
- - add rake task which will accept pattern for files
55
- - add color reporter
56
71
  - add more tests
57
-
72
+ - add color reporter
73
+ - add cli
data/Rakefile CHANGED
@@ -11,8 +11,6 @@ task :git do
11
11
  end
12
12
 
13
13
  require "rspec/core/rake_task"
14
-
15
- desc "Run specs"
16
14
  RSpec::Core::RakeTask.new
17
15
 
18
16
  task :default => :spec
@@ -22,3 +20,9 @@ task :default => :spec
22
20
  # t.rcov = true
23
21
  # t.rcov_opts = ["--exclude", "spec"]
24
22
  # end
23
+
24
+ require "./lib/jshintrb/jshinttask"
25
+ Jshintrb::JshintTask.new :jshint do |t|
26
+ t.pattern = 'vendor/jshint/tests/unit/fixtures/*.js'
27
+ t.options = :defaults
28
+ end
data/jshintrb.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.email = ["stereobooster@gmail.com"]
10
10
  s.homepage = "https://github.com/stereobooster/jshintrb"
11
11
  s.summary = %q{Ruby wrapper for JSHint}
12
- s.description = %q{Ruby wrapper for JSHint. The main difference from jshint it does not depend on Java. Instead, it uses ExecJS}
12
+ s.description = %q{Ruby wrapper for JSHint. The main difference from jshint gem it does not depend on Java. Instead, it uses ExecJS}
13
13
 
14
14
  s.rubyforge_project = "jshintrb"
15
15
 
@@ -0,0 +1,76 @@
1
+ # Define a task library for running JSHint contexts.
2
+
3
+ require 'rake'
4
+ require 'rake/tasklib'
5
+
6
+ require 'jshintrb'
7
+
8
+ module Jshintrb
9
+
10
+ class JshintTask < ::Rake::TaskLib
11
+ # Name of JSHint task. (default is :jshint)
12
+ attr_accessor :name
13
+
14
+ # Glob pattern to match JavaScript files. (default is './**/*.js')
15
+ attr_accessor :pattern
16
+
17
+ # options
18
+ attr_accessor :options
19
+
20
+ # Whether or not to fail Rake when an error occurs (typically when Jshint check fail).
21
+ # Defaults to true.
22
+ attr_accessor :fail_on_error
23
+
24
+ # Explicitly define the list of JavaScript files to be linted.
25
+ # +js_files+ is expected to be an array of file names (a
26
+ # FileList is acceptable). If both +pattern+ and +js_files+ are
27
+ # used, then the list of JavaScritp files is the union of the two.
28
+ attr_accessor :js_files
29
+
30
+ # Defines a new task, using the name +name+.
31
+ def initialize(name=:jshint)
32
+ @name = name
33
+ @pattern = nil
34
+ @js_files = nil
35
+ @options = nil
36
+ @fail_on_error = true
37
+
38
+ yield self if block_given?
39
+ @pattern = './**/*.js' if pattern.nil? && js_files.nil?
40
+ define
41
+ end
42
+
43
+ def define # :nodoc:
44
+
45
+ actual_name = Hash === name ? name.keys.first : name
46
+ unless ::Rake.application.last_comment
47
+ desc "Run JShint"
48
+ end
49
+ task name do
50
+ unless js_file_list.empty?
51
+ result = Jshintrb::report(js_file_list, @options, STDERR)
52
+ if result.size > 0
53
+ abort("JSHint check failed") if fail_on_error
54
+ end
55
+ end
56
+ end
57
+
58
+ self
59
+ end
60
+
61
+ def evaluate(o) # :nodoc:
62
+ case o
63
+ when Proc then o.call
64
+ else o
65
+ end
66
+ end
67
+
68
+ def js_file_list # :nodoc:
69
+ result = []
70
+ result += js_files.to_a if js_files
71
+ result += FileList[ pattern ].to_a if pattern
72
+ FileList[result]
73
+ end
74
+ end
75
+
76
+ end
data/lib/jshintrb/lint.rb CHANGED
@@ -32,13 +32,15 @@ module Jshintrb
32
32
 
33
33
  def initialize(options = nil)
34
34
 
35
- if options.nil? then
35
+ if options == :defaults then
36
36
  @options = DEFAULTS.dup
37
37
  elsif options.instance_of? Hash then
38
38
  @options = options.dup
39
39
  # @options = DEFAULTS.merge(options)
40
+ elsif options.nil?
41
+ @options = nil
40
42
  else
41
- raise ArgumentError
43
+ raise 'Unsupported option for Jshintrb: ' + options.to_s
42
44
  end
43
45
 
44
46
  @context = ExecJS.compile(File.open(SourcePath, "r:UTF-8").read)
@@ -48,7 +50,11 @@ module Jshintrb
48
50
  source = source.respond_to?(:read) ? source.read : source.to_s
49
51
 
50
52
  js = []
51
- js << "JSHINT(#{MultiJson.encode(source)}, #{MultiJson.encode(@options)});"
53
+ if @options.nil? then
54
+ js << "JSHINT(#{MultiJson.encode(source)});"
55
+ else
56
+ js << "JSHINT(#{MultiJson.encode(source)}, #{MultiJson.encode(@options)});"
57
+ end
52
58
  js << "return JSHINT.errors;"
53
59
 
54
60
  @context.exec js.join("\n")
@@ -2,12 +2,26 @@ module Jshintrb
2
2
  module Reporter
3
3
  class Default
4
4
 
5
- def format errors
5
+ def format errors, file
6
6
  result = ''
7
- errors.each do | error |
8
- result += 'line ' + error["line"].to_s + ', col ' +
9
- error["character"].to_s + ', ' + error["reason"].to_s + '\n'
7
+ indent = ''
8
+ if file then
9
+ indent = ' '
10
10
  end
11
+
12
+ errors.each do |error|
13
+ if error.nil? then
14
+ result += indent + 'fatal error'
15
+ else
16
+ result += indent + 'line ' + error["line"].to_s + ', col ' +
17
+ error["character"].to_s + ', ' + error["reason"].to_s + "\n"
18
+ end
19
+ end
20
+
21
+ if file && result.size > 0 then
22
+ result = 'file: ' + file + "\n" + result
23
+ end
24
+
11
25
  result
12
26
  end
13
27
 
@@ -1,3 +1,3 @@
1
1
  module Jshintrb
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/jshintrb.rb CHANGED
@@ -10,10 +10,31 @@ module Jshintrb
10
10
  Lint.new(options).lint(source)
11
11
  end
12
12
 
13
- def self.report(source, options = nil)
14
- reporter = Reporter::Default
15
- errors = Lint.new(options).lint(source)
16
- reporter.new.format errors
13
+ def self.report(source, options = nil, out = nil)
14
+ reporter = Reporter::Default.new
15
+ linter = Lint.new(options)
16
+ report = ''
17
+ if source.is_a?(Array) then
18
+ source.each do |src|
19
+ if !src.is_a?(String) then
20
+ p src.to_s
21
+ raise ('Expected array of strings. Instead get ' + src.class.to_s)
22
+ end
23
+ errors = linter.lint(File.read(src))
24
+ rep = reporter.format errors, src
25
+ if out && rep.size > 0 then
26
+ out.puts rep
27
+ end
28
+ report += rep
29
+ end
30
+ else
31
+ errors = linter.lint(source)
32
+ report = reporter.format errors
33
+ if out then
34
+ out.puts report
35
+ end
36
+ end
37
+ report
17
38
  end
18
39
 
19
40
  end
@@ -1,6 +1,10 @@
1
1
  # encoding: UTF-8
2
2
  require "jshintrb"
3
3
 
4
+ def gen_file source, option, value
5
+ "/*jshint " + option.to_s + ": " + value.to_s + "*/\n" + source
6
+ end
7
+
4
8
  describe "Jshintrb" do
5
9
 
6
10
  it "support options" do
@@ -28,6 +32,10 @@ describe "Jshintrb" do
28
32
  Jshintrb.lint(source, option => true).length.should eq 1
29
33
  end
30
34
 
35
+ options.each do |option, source|
36
+ Jshintrb.lint(gen_file(source, option, false)).length.should eq 0
37
+ Jshintrb.lint(gen_file(source, option, true)).length.should eq 1
38
+ end
31
39
  end
32
40
 
33
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jshintrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &21167640 !ruby/object:Gem::Requirement
16
+ requirement: &21721428 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *21167640
24
+ version_requirements: *21721428
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &21167352 !ruby/object:Gem::Requirement
27
+ requirement: &21721056 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *21167352
35
+ version_requirements: *21721056
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: multi_json
38
- requirement: &21167100 !ruby/object:Gem::Requirement
38
+ requirement: &21720636 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *21167100
46
+ version_requirements: *21720636
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: execjs
49
- requirement: &21166800 !ruby/object:Gem::Requirement
49
+ requirement: &21720024 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,9 +54,9 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *21166800
58
- description: Ruby wrapper for JSHint. The main difference from jshint it does not
59
- depend on Java. Instead, it uses ExecJS
57
+ version_requirements: *21720024
58
+ description: Ruby wrapper for JSHint. The main difference from jshint gem it does
59
+ not depend on Java. Instead, it uses ExecJS
60
60
  email:
61
61
  - stereobooster@gmail.com
62
62
  executables: []
@@ -72,6 +72,7 @@ files:
72
72
  - jshintrb.gemspec
73
73
  - lib/js/jshint.js
74
74
  - lib/jshintrb.rb
75
+ - lib/jshintrb/jshinttask.rb
75
76
  - lib/jshintrb/lint.rb
76
77
  - lib/jshintrb/reporter/default.rb
77
78
  - lib/jshintrb/version.rb