jslint-rb 0.5 → 0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/jslint-rb/error.rb +15 -1
- data/lib/jslint-rb/formatter.rb +24 -5
- data/lib/jslint-rb/option_parser.rb +30 -9
- data/lib/jslint-rb/runner.rb +7 -20
- metadata +1 -1
data/lib/jslint-rb/error.rb
CHANGED
@@ -1,6 +1,20 @@
|
|
1
1
|
module JslintRb
|
2
|
+
##
|
3
|
+
#This object encapsulates the output from JSHint. Does some
|
4
|
+
#minimal formatting
|
2
5
|
class Error
|
3
|
-
|
6
|
+
##
|
7
|
+
#Line number the JSHint error occured on
|
8
|
+
attr_reader :line_number
|
9
|
+
##
|
10
|
+
#Column number of the JSHint error
|
11
|
+
attr_reader :character
|
12
|
+
##
|
13
|
+
#The actual error that was discovered
|
14
|
+
attr_reader :reason
|
15
|
+
##
|
16
|
+
#The snippet of code that caused the error
|
17
|
+
attr_reader :evidence
|
4
18
|
|
5
19
|
def initialize(jsobject)
|
6
20
|
@line_number = jsobject["line"]
|
data/lib/jslint-rb/formatter.rb
CHANGED
@@ -14,20 +14,39 @@ module JslintRb
|
|
14
14
|
errors.each do |error|
|
15
15
|
fmt_err = "#{filename}:#{error.line_number}:"\
|
16
16
|
"#{error.character}:#{error.reason}"
|
17
|
-
fmt_err << " | #{error.evidence}" unless error.evidence.nil?
|
18
17
|
results << fmt_err
|
19
18
|
end
|
20
19
|
results
|
21
20
|
end
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
##
|
23
|
+
#Multiline output suitible for a terminal window
|
24
|
+
MULTI_LINE = Proc.new do |errors, filename|
|
25
|
+
results = []
|
26
|
+
|
27
|
+
#header
|
28
|
+
results << "#########################################"
|
29
|
+
results << "# Lint output for: #{filename}"
|
30
|
+
results << "#########################################"
|
31
|
+
|
32
|
+
errors.each do |error|
|
33
|
+
results << "Error occured on line #{error.line_number} at col #{error.character}:"
|
34
|
+
results << " #{error.reason}"
|
35
|
+
results << "EVIDENCE: #{error.evidence}" unless error.evidence.nil?
|
36
|
+
results << "\n"
|
26
37
|
end
|
27
|
-
|
38
|
+
results
|
28
39
|
end
|
29
40
|
|
41
|
+
##
|
42
|
+
#Uses MULTI_LINE as a default
|
43
|
+
def initialize(formatter)
|
44
|
+
formatter = JslintRb::Formatter::MULTI_LINE if formatter.nil?
|
45
|
+
@command = formatter
|
46
|
+
end
|
30
47
|
|
48
|
+
##
|
49
|
+
#Put the formatted JSHINT results to STDOUT
|
31
50
|
def print(errors, filename)
|
32
51
|
output = @command.call(errors, filename)
|
33
52
|
puts output
|
@@ -7,11 +7,16 @@ module JslintRb
|
|
7
7
|
require 'optparse'
|
8
8
|
require 'yaml'
|
9
9
|
|
10
|
+
##
|
11
|
+
#The options for JSHint and global vars to exclude from
|
12
|
+
#undef error reports
|
10
13
|
attr_reader :options
|
14
|
+
##
|
15
|
+
#The file that we are executing JSHint against
|
11
16
|
attr_reader :filename
|
12
17
|
|
13
18
|
##
|
14
|
-
#These are just some defaults that I wanted.
|
19
|
+
#These are just some JSHINT defaults that I wanted.
|
15
20
|
#Part of the fun in writing software is imposing your
|
16
21
|
#opinions on people.
|
17
22
|
DEFAULT_OPT =
|
@@ -28,6 +33,10 @@ module JslintRb
|
|
28
33
|
eqnull: true
|
29
34
|
}
|
30
35
|
|
36
|
+
##
|
37
|
+
#The default globals are tailored for some of my projects
|
38
|
+
#and ExtJS. Should probably turn this into something more
|
39
|
+
#generic
|
31
40
|
DEFAULT_GLOBALS = {
|
32
41
|
Ext: true,
|
33
42
|
console: true,
|
@@ -42,8 +51,6 @@ module JslintRb
|
|
42
51
|
@options = {
|
43
52
|
#Location of the jslint.js file
|
44
53
|
lint_location: File.expand_path("js/jshint.js", File.dirname(__FILE__)),
|
45
|
-
#tempfile location; make sure it's writable by your user
|
46
|
-
temp_file: 'jslint_wrap.tmp',
|
47
54
|
#this is passed to JSLint so that it will not falsely call
|
48
55
|
#an undefined error on them
|
49
56
|
global_vars: {},
|
@@ -57,6 +64,8 @@ module JslintRb
|
|
57
64
|
parse_cmdline_args
|
58
65
|
end
|
59
66
|
|
67
|
+
private
|
68
|
+
|
60
69
|
def load_globals globals
|
61
70
|
globals.each do |global|
|
62
71
|
key,value = global.split(':')
|
@@ -88,17 +97,28 @@ module JslintRb
|
|
88
97
|
return if config_file.nil?
|
89
98
|
config = YAML::load(config_file)
|
90
99
|
return if config == false
|
91
|
-
|
92
|
-
|
93
|
-
|
100
|
+
|
101
|
+
config.each do |key, val|
|
102
|
+
if key == 'global_vars'
|
103
|
+
load_globals val
|
104
|
+
elsif key == 'lint_options'
|
105
|
+
load_lint_configs val
|
106
|
+
elsif key == 'formatter_proc'
|
107
|
+
get_formatter_constant val
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
94
111
|
end
|
95
112
|
|
113
|
+
##
|
114
|
+
#Uses optparse to turn the cmdline arguments into
|
115
|
+
#config objects
|
96
116
|
def parse_cmdline_args
|
97
117
|
|
98
118
|
ARGV.options do |opt|
|
99
119
|
opt.banner = "Usage: jslint-rb [FILENAME] [OPTIONS]"
|
100
120
|
|
101
|
-
opt.on('-o', "--options ['OPTIONNAME : VALUE'
|
121
|
+
opt.on('-o', "--options ['OPTIONNAME : VALUE']", Array,
|
102
122
|
"List of JsHint options. "\
|
103
123
|
"Passing default : true will "\
|
104
124
|
"enable default options") do |options|
|
@@ -111,8 +131,9 @@ module JslintRb
|
|
111
131
|
end
|
112
132
|
|
113
133
|
opt.on('-f', "--format FORMAT", String,
|
114
|
-
"The string constant for the Formatter proc
|
115
|
-
|
134
|
+
"The string constant for the Formatter proc
|
135
|
+
Options:
|
136
|
+
VIM -- format for VIM make errorformat"
|
116
137
|
) do |option|
|
117
138
|
get_formatter_constant option
|
118
139
|
end
|
data/lib/jslint-rb/runner.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
module JslintRb
|
2
|
-
##
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#into a gem, you'll be able to set these by config file
|
7
|
-
#or command line options.
|
2
|
+
##
|
3
|
+
#This class uses ExecJS to execute the JSHINT function with the
|
4
|
+
#config options provided. Uses MultiJson to dump the Ruby objects to
|
5
|
+
#well formatted JS.
|
8
6
|
class Runner
|
9
7
|
require 'execjs'
|
10
8
|
require 'multi_json'
|
@@ -14,8 +12,10 @@ module JslintRb
|
|
14
12
|
@config = options
|
15
13
|
end
|
16
14
|
|
15
|
+
##
|
16
|
+
#Executes JSHint, with the options and globals provided.
|
17
|
+
#Converts the JS output to an array of JslintRb::Error objects
|
17
18
|
def execute
|
18
|
-
#set_globals
|
19
19
|
context = ExecJS.compile(File.read(@config[:lint_location]))
|
20
20
|
output = context.exec("JSHINT(#{MultiJson.dump(File.read(@filename))},"\
|
21
21
|
"#{MultiJson.dump(@config[:lint_options])},"\
|
@@ -24,18 +24,5 @@ module JslintRb
|
|
24
24
|
output.compact.map {|x| JslintRb::Error.new(x) }
|
25
25
|
end
|
26
26
|
|
27
|
-
##
|
28
|
-
#This prepends a list of globals to the file you're working on.
|
29
|
-
#Saves it off to the location defined by @config[:temp_file]
|
30
|
-
def set_globals
|
31
|
-
f = File.open(@filename, 'r')
|
32
|
-
contents = "/*global #{@config[:global_vars]} */ #{f.read}"
|
33
|
-
f.close
|
34
|
-
|
35
|
-
tmp_file_handle = File.open(@config[:temp_file], "w")
|
36
|
-
tmp_file_handle.write(contents)
|
37
|
-
tmp_file_handle.close()
|
38
|
-
end
|
39
|
-
|
40
27
|
end#Runner
|
41
28
|
end#JslintRb
|