jshint4r 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "vendor/jshint"]
2
+ path = vendor/jshint
3
+ url = https://github.com/jshint/jshint.git
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem 'execjs'
6
+ gem 'rake'
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "rspec"
12
+ gem "bundler"
13
+ gem "jeweler"
14
+ gem "rcov"
15
+ gem 'rdoc'
16
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 wtnabe
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = jshint4r
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to jshint4r
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 wtnabe. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+ Dir.glob( File.dirname(__FILE__) + '/lib/tasks/*.rake' ) { |f|
14
+ load f
15
+ }
16
+
17
+ require 'jeweler'
18
+ Jeweler::Tasks.new do |gem|
19
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
20
+ gem.name = "jshint4r"
21
+ gem.homepage = "http://github.com/wtnabe/jshint4r"
22
+ gem.license = "MIT"
23
+ gem.summary = %Q{jshint4r}
24
+ gem.description = %Q{jshint runner for ruby}
25
+ gem.email = "wtnabe@gmail.com"
26
+ gem.authors = ["wtnabe"]
27
+ gem.files.include Dir.glob('vendor/jshint/*')
28
+ # dependencies defined in Gemfile
29
+ end
30
+ Jeweler::RubygemsDotOrgTasks.new
31
+
32
+ require 'rspec/core'
33
+ require 'rspec/core/rake_task'
34
+ RSpec::Core::RakeTask.new(:spec) do |spec|
35
+ spec.pattern = FileList['spec/**/*_spec.rb']
36
+ end
37
+
38
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
39
+ spec.pattern = 'spec/**/*_spec.rb'
40
+ spec.rcov = true
41
+ end
42
+
43
+ task :default => :spec
44
+
45
+ require 'rdoc/task'
46
+ RDoc::Task.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "jshint4r #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/jshint4r ADDED
@@ -0,0 +1,8 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ unless ( ENV['PATH'].split( File::PATH_SEPARATOR ).include?( File.dirname( __FILE__ ) ) )
4
+ $LOAD_PATH << File.expand_path( "../lib", File.dirname( __FILE__ ) )
5
+ end
6
+
7
+ require 'jshint4r'
8
+ JSHint4r::CLI.run
@@ -0,0 +1,78 @@
1
+ require 'optparse'
2
+
3
+ module JSHint4r
4
+ class CLI
5
+ def self.run
6
+ self.new.run
7
+ end
8
+
9
+ def run
10
+ paths = args.permute( ARGV )
11
+ l = Linter.new( Source.context, config.opts )
12
+ r = JSHint4r.reporter( config.reporter )
13
+ targets( paths ).each { |f|
14
+ s = r.report( f, l.lint( f ), config.verbose )
15
+ puts s if s
16
+ }
17
+ end
18
+
19
+ #
20
+ # [param] String path
21
+ # [return] JSHint4r::Config
22
+ #
23
+ def config( path = nil )
24
+ if ( !@config )
25
+ @config = Config.new( path )
26
+ end
27
+
28
+ @config
29
+ end
30
+
31
+ #
32
+ # [param] Array path
33
+ # [return] JSHint4r::Target
34
+ #
35
+ def targets( paths )
36
+ Target.new( config.targets + paths || [], config.excludes.uniq )
37
+ end
38
+
39
+ #
40
+ # [return] OptionParser
41
+ #
42
+ def args
43
+ opts = OptionParser.new { |opt|
44
+ opt.banner = 'Usage: jshint4r [options] path1 path2 ...'
45
+ opt.on( '-c', '--config PATH' ) { |path|
46
+ if File.exist?( path )
47
+ config( path )
48
+ end
49
+ }
50
+ opt.on( '-e', '--exclude PATH' ) { |path|
51
+ config.excludes = path
52
+ }
53
+ opt.on( '-o', '--options KEY=VALUE' ) { |opt|
54
+ o = opt.split('=', 2)
55
+ if o.size == 2
56
+ config.opts = { o[0] => o[1] }
57
+ end
58
+ }
59
+ opt.on( '-r', '--reporter REPORTER' ) { |reporter|
60
+ config.reporter = reporter
61
+ }
62
+ opt.on( '-V', '--verbose' ) {
63
+ config.verbose = true
64
+ }
65
+ opt.on( '-s', '--silent' ) {
66
+ config.verbose = false
67
+ }
68
+ }
69
+
70
+ if ARGV.size == 0
71
+ puts opts.help
72
+ exit
73
+ else
74
+ opts
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,67 @@
1
+ module JSHint4r
2
+ class Config
3
+ def initialize( path = nil )
4
+ @excludes = []
5
+ @opts = {}
6
+ @path = nil
7
+ @reporter = :text
8
+ @targets = []
9
+ @verbose = false
10
+
11
+ if path and read_config_file( path )
12
+ @path = path
13
+ end
14
+ end
15
+ attr_reader :excludes, :opts, :targets, :path
16
+ attr_accessor :reporter, :verbose
17
+
18
+ #
19
+ # [param] String path
20
+ # [return] Boolean
21
+ #
22
+ def read_config_file( path )
23
+ r = false
24
+
25
+ if File.exist?( path )
26
+ conf = YAML.load_file( path )
27
+ if ( conf.is_a? Hash )
28
+ self.excludes = conf['excludes'] if conf.has_key? 'excludes'
29
+ self.opts = conf['options'] if conf.has_key? 'options'
30
+ self.reporter = conf['reporter'] if conf.has_key? 'reporter'
31
+ self.targets = conf['src'] if conf.has_key? 'src'
32
+ self.verbose = conf['verbose'] if conf.has_key? 'verbose'
33
+
34
+ r = true
35
+ end
36
+ end
37
+
38
+ return r
39
+ end
40
+
41
+ #
42
+ # [param] Objecte ex
43
+ # [return] Array
44
+ #
45
+ def excludes=( ex )
46
+ ex = [ex] unless ex.is_a? Array
47
+ @excludes += ex
48
+ end
49
+
50
+ #
51
+ # [param] Hash opts
52
+ # [return] Hash
53
+ #
54
+ def opts=( opts )
55
+ @opts.merge!( opts )
56
+ end
57
+
58
+ #
59
+ # [param] Object t
60
+ # [return] Array
61
+ #
62
+ def targets=( t )
63
+ t = [t] unless t.is_a? Array
64
+ @targets += t
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,5 @@
1
+ JSHINT.run = function( src, options ) {
2
+ if ( !JSHINT( src, options ) ) {
3
+ return JSHINT.errors;
4
+ }
5
+ };
@@ -0,0 +1,21 @@
1
+ module JSHint4r
2
+ class Linter
3
+ #
4
+ # [param] ExecJS::*::Context context
5
+ # [param] Hash opts
6
+ #
7
+ def initialize( context, opts = nil )
8
+ @context = context
9
+ @opts = opts || {}
10
+ end
11
+ attr_reader :context, :opts
12
+
13
+ #
14
+ # [param] File
15
+ # [return] Array or nil
16
+ #
17
+ def lint( target )
18
+ context.call('JSHINT.run', File.read(target), opts )
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module JSHint4r
2
+ module Reporter
3
+ class Compilation
4
+ include Reporter
5
+
6
+ #
7
+ # [param] String target
8
+ # [param] Array errors
9
+ # [return] String or nil
10
+ #
11
+ def report( target, errors, verbose = false )
12
+ if errors and errors.size > 0
13
+ errors.map { |error|
14
+ e = key_symbolize( error )
15
+ sprintf( "%s:%s:%s: %s\n%s",
16
+ target,
17
+ e[:line], e[:character], e[:reason], e[:evidence] )
18
+ }.join("\n") << "\n"
19
+ elsif verbose
20
+ sprintf( "%s ... ok\n", target )
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ module JSHint4r
2
+ module Reporter
3
+ #
4
+ # like output of npm's jshint
5
+ #
6
+ class Text
7
+ include Reporter
8
+
9
+ #
10
+ # [param] String target
11
+ # [param] Array errors
12
+ # [return] String
13
+ #
14
+ def report( target, errors, verbose = false )
15
+ if errors
16
+ errors.map { |error|
17
+ e = key_symbolize( error )
18
+ sprintf( "%s: line %s, col %s, %s",
19
+ target, e[:line], e[:character], e[:reason] )
20
+ }.join("\n") + <<EOD
21
+
22
+
23
+ #{errors.size} errors
24
+ EOD
25
+ elsif verbose
26
+ sprintf( "%s ... ok\n", target )
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module JSHint4r
2
+ class << self
3
+ def reporter( reporter = :text )
4
+ JSHint4r::Reporter.const_get( reporter.to_s.capitalize ).new
5
+ end
6
+ end
7
+
8
+ module Reporter
9
+ #
10
+ # [param] Hash error
11
+ # [return] Hash
12
+ #
13
+ def key_symbolize( error )
14
+ Hash[*error.map { |k, v|
15
+ [k.to_sym, v]
16
+ }.flatten]
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module JSHint4r
2
+ class Result
3
+ def initialize( target, errors )
4
+ @target = target
5
+ @errors = parse( errors )
6
+ end
7
+ attr_reader :target, :errors
8
+
9
+ #
10
+ # [param] String errors
11
+ # [return] Object
12
+ #
13
+ def parse( errors )
14
+ errors = errors || ''
15
+ ExecJS.eval( errors )
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ module JSHint4r
2
+ class Source
3
+ JSHINT = File.dirname(__FILE__) + '/../../vendor/jshint/jshint.js'
4
+ RUNNER = File.dirname(__FILE__) + '/jshint_runner.js'
5
+
6
+ class << self
7
+ #
8
+ # [return] String
9
+ #
10
+ def src
11
+ [JSHINT, RUNNER].map { |f| File.read(f) }.join
12
+ end
13
+
14
+ #
15
+ # [return] ExecJS::*::Context
16
+ #
17
+ def context
18
+ if ( !@context )
19
+ @context = ExecJS.compile( src )
20
+ end
21
+
22
+ @context
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,60 @@
1
+ require 'rake'
2
+
3
+ module JSHint4r
4
+ class Target
5
+ def initialize( targets, excludes = nil )
6
+ @targets = targets || []
7
+ @excludes = excludes || []
8
+ end
9
+ attr_reader :targets, :excludes
10
+
11
+ def each( &block )
12
+ real_targets.each { |f|
13
+ block.call( f )
14
+ }
15
+ end
16
+
17
+ #
18
+ # [return] Rake::FileList
19
+ #
20
+ def real_targets
21
+ if ( !@real_targets )
22
+ @real_targets = target_files
23
+ end
24
+
25
+ @real_targets
26
+ end
27
+
28
+ protected
29
+ #
30
+ # [return] Rake::FileList
31
+ #
32
+ def target_files
33
+ t = clean( FileList[targets] )
34
+
35
+ t = FileList[t.select { |e|
36
+ e if File.exist?( e )
37
+ }.map { |e|
38
+ if File.directory?( e )
39
+ Dir.glob( File.join( e, '**/*.js' ) )
40
+ else
41
+ e
42
+ end
43
+ }.flatten.uniq]
44
+
45
+ clean( t )
46
+ end
47
+
48
+ #
49
+ # [param] Rake::FileList targets
50
+ # [return] Rake::FileList
51
+ #
52
+ def clean( targets )
53
+ excludes.each { |ex|
54
+ targets.exclude( ex )
55
+ }
56
+
57
+ targets
58
+ end
59
+ end
60
+ end
data/lib/jshint4r.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'execjs'
2
+
3
+ module JSHint4r
4
+ end
5
+
6
+ Dir.glob( File.dirname(__FILE__) + '/jshint4r/**/*.rb' ) { |f|
7
+ require f
8
+ }
@@ -0,0 +1,6 @@
1
+ namespace :git do
2
+ desc "list git-untracked files"
3
+ task :untracked do
4
+ sh 'git ls-files -o --exclude-from=.gitignore'
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ src:
2
+ - spec/fixtures/
3
+ excludes:
4
+ - '**/no_errors.js'
5
+ verbose: true
6
+ reporter: :compilation
7
+ options:
8
+ undef: false
9
+ asi: true
@@ -0,0 +1 @@
1
+ a = 1
@@ -0,0 +1 @@
1
+ a = 1;
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSHint4r::Config do
4
+ before(:all) {
5
+ @config = JSHint4r::Config.new( fixture( 'config.yml' ) )
6
+ }
7
+
8
+ describe 'initialize' do
9
+ context 'with filename' do
10
+ subject {
11
+ JSHint4r::Config.new( fixture( 'config.yml' ) ).path
12
+ }
13
+ it {
14
+ should_not be_nil
15
+ }
16
+ end
17
+ end
18
+
19
+ describe 'excludes' do
20
+ subject {
21
+ @config.excludes
22
+ }
23
+ it {
24
+ should eq(['**/no_errors.js'])
25
+ }
26
+ end
27
+
28
+ describe 'excludes =' do
29
+ subject {
30
+ @config.excludes = 'foobar'
31
+ @config.excludes
32
+ }
33
+ it {
34
+ should eq( %w( **/no_errors.js foobar ) )
35
+ }
36
+ end
37
+
38
+ describe 'opts' do
39
+ subject {
40
+ @config.opts
41
+ }
42
+ it {
43
+ should eq( 'undef' => false,
44
+ 'asi' => true )
45
+ }
46
+ end
47
+
48
+ describe 'opts =' do
49
+ subject {
50
+ @config.opts = {'laxbreak' => true}
51
+ @config.opts
52
+ }
53
+ it {
54
+ should eq( 'undef' => false,
55
+ 'asi' => true,
56
+ 'laxbreak' => true )
57
+ }
58
+ end
59
+
60
+ describe 'reporter' do
61
+ subject {
62
+ @config.reporter
63
+ }
64
+ it {
65
+ should eq(:compilation)
66
+ }
67
+ end
68
+
69
+ describe 'reporter =' do
70
+ subject {
71
+ @config.reporter = :xml
72
+ @config.reporter
73
+ }
74
+ it {
75
+ should eq(:xml)
76
+ }
77
+ end
78
+
79
+ describe 'targets' do
80
+ subject {
81
+ @config.targets
82
+ }
83
+ it {
84
+ should eq( ['spec/fixtures/'] )
85
+ }
86
+ end
87
+
88
+ describe 'targets =' do
89
+ subject {
90
+ @config.targets = 'vendor/jshint'
91
+ @config.targets
92
+ }
93
+ it {
94
+ should eq( %w( spec/fixtures/ vendor/jshint ) )
95
+ }
96
+ end
97
+
98
+ describe 'verbose' do
99
+ subject {
100
+ @config.verbose
101
+ }
102
+ it {
103
+ should be_true
104
+ }
105
+ end
106
+
107
+ describe 'verbose =' do
108
+ subject {
109
+ @config.verbose = false
110
+ }
111
+ it {
112
+ should be_false
113
+ }
114
+ end
115
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSHint4r::Linter do
4
+ before(:all) {
5
+ @linter = JSHint4r::Linter.new( JSHint4r::Source.context,
6
+ {'undef' => false,
7
+ 'asi' => false} )
8
+ }
9
+
10
+ describe 'lint' do
11
+ context 'no errors' do
12
+ subject {
13
+ @linter.lint( fixture( 'no_errors.js' ) )
14
+ }
15
+ it {
16
+ should be_nil
17
+ }
18
+ end
19
+ describe 'missing smicolon' do
20
+ subject {
21
+ @linter.lint( fixture( 'missing_semicolon.js' ) )
22
+ }
23
+ describe 'class Array' do
24
+ it {
25
+ subject.class.should eq(Array)
26
+ }
27
+ end
28
+ describe 'Array of one Hash' do
29
+ it {
30
+ subject.first.class.should eq(Hash)
31
+ }
32
+ end
33
+ end
34
+ end
35
+ end
36
+