jslintrb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2010 Steven Parkes
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.
21
+
22
+ See lib/jslintrb/fulljslint.js for the jslint license.
23
+ json2.js is in the public domain (see lib/jslintrb/json2.js).
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = jslintrb
2
+
3
+ jslintrb is a packaged version of Douglas Crockford's JSLint
4
+ JavaScript code checker. jslintrb uses SpiderMonkey via the Johnson
5
+ Ruby gem to interpret the JSLint javascript.
6
+
7
+ == Usage
8
+
9
+ jslintrb <javascript files to be checked>
10
+
11
+ You can provide a .jslinrbrc file in the directory from which the
12
+ command is run to set jslint options. The file is JSON-formated. See
13
+ the jslint documentation (http://jslint.com) for allowed options.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Steven Parkes. See LICENSE for details.
data/bin/jslintrb ADDED
@@ -0,0 +1,240 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ require 'jslintrb/options'
6
+
7
+ ENV["JSLINTRB_JS_PATH"] =
8
+ JSLINT_DIR =
9
+ File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'jslintrb'))
10
+
11
+ args = [ "env",
12
+ "JSLINT_FILES=#{ARGV.join(':')}",
13
+ "johnson",
14
+ File.join(JSLINT_DIR,"fulljslint.js"),
15
+ File.join(JSLINT_DIR,"json2.js"),
16
+ File.join(JSLINT_DIR,"jslintrb.js") ]
17
+
18
+ cmd = args.join(" ")
19
+
20
+ puts cmd if $jslintrb_verbose
21
+
22
+ if !system cmd
23
+ exit $?.exitstatus
24
+ end
25
+
26
+ exit
27
+
28
+ intro = File.join(JSLINT_DIR, "intro.js")
29
+ outro = File.join(JSLINT_DIR, "outro.js")
30
+
31
+ args << intro
32
+
33
+ introd = true
34
+ outrod = true
35
+
36
+ ARGV.each do |f|
37
+
38
+ if f =~ /\.x?html?$/ || f =~ %r(^https?://)
39
+ if introd && !outrod
40
+ args << outro
41
+ args << "about:blank"
42
+ outrod = true
43
+ introd = false
44
+ end
45
+ if !introd
46
+ args << intro
47
+ end
48
+ args << f
49
+ introd = true
50
+ outrod = false
51
+ elsif f == "about:blank"
52
+ if introd && !outrod
53
+ args << outro
54
+ outrod = true
55
+ introd = false
56
+ end
57
+ args << f
58
+ else
59
+ args << f
60
+ end
61
+
62
+ end
63
+
64
+ args << outro
65
+
66
+ cmd = args.join(" ")
67
+
68
+ puts cmd if $jslintrb_verbose
69
+
70
+ require 'eventmachine'
71
+ require 'nokogiri'
72
+
73
+ class Restart < Exception; end
74
+
75
+ def colour(text, colour_code)
76
+ # return text unless ENV['RSPEC_COLOR'] || (colour? & (autospec? || output_to_tty?))
77
+ "#{colour_code}#{text}\e[0m"
78
+ end
79
+
80
+ def green(text); colour(text, "\e[32m"); end
81
+ def red(text); colour(text, "\e[31m"); end
82
+ def yellow(text); colour(text, "\e[33m"); end
83
+ def blue(text); colour(text, "\e[34m"); end
84
+
85
+ $spec_info = {}
86
+ $spec_info[:specs] = []
87
+ $spec_info[:failures] = []
88
+ $spec_info[:pending] = []
89
+
90
+ class SAX < Nokogiri::XML::SAX::Document
91
+
92
+ def start_element name, attrs
93
+ # p attrs
94
+ attrs = Hash[*attrs]
95
+ # p attrs
96
+ case name
97
+ when "testsuites";
98
+ when "testsuite";
99
+ @testsuite_name = attrs["name"]
100
+ if @testsuite_pending = ( attrs["skipped"] == "true" )
101
+ $spec_info[:specs] << (tc = {})
102
+ tc[:name] = @testsuite_name
103
+ $spec_info[:pending] << $spec_info[:specs].last
104
+ end
105
+ when "testcase";
106
+ $spec_info[:specs] << (tc = {})
107
+ tc[:name] = @testsuite_name + " : " + attrs["name"]
108
+ @testcase_failed = false
109
+ when "failure";
110
+ $spec_info[:failures] << (tc = $spec_info[:specs].last)
111
+ tc[:message] = attrs["message"]
112
+ @testcase_failed = true
113
+ else; raise "hell: #{name}"
114
+ end
115
+ end
116
+
117
+ def cdata_block string
118
+ if @testcase_failed
119
+ $spec_info[:failures].last[:stack] ||= ""
120
+ $spec_info[:failures].last[:stack] << string
121
+ end
122
+ end
123
+
124
+ def end_element name
125
+ case name
126
+ when "testsuites";
127
+ raise Restart
128
+ when "testsuite";
129
+ if !$jslintrb_xml
130
+ if @testsuite_pending
131
+ char = yellow("*")
132
+ print char
133
+ $stdout.flush
134
+ end
135
+ end
136
+ when "testcase";
137
+ if !$jslintrb_xml
138
+ char = nil
139
+ if @testcase_failed
140
+ char = red("F")
141
+ else
142
+ char = green(".")
143
+ end
144
+ print char
145
+ $stdout.flush
146
+ end
147
+ when "failure";
148
+ else; raise "hell: #{name}"
149
+ end
150
+ end
151
+ end
152
+
153
+ $failed_processes = 0
154
+
155
+ module WriteToNokogiri
156
+ def post_init
157
+ @parser = Nokogiri::XML::SAX::PushParser.new( SAX.new )
158
+ end
159
+ def receive_data data
160
+ begin
161
+ if $jslintrb_xml
162
+ # $stderr.print data
163
+ puts data
164
+ $stdout.flush
165
+ end
166
+ @parser << data
167
+ rescue Restart
168
+ @parser = Nokogiri::XML::SAX::PushParser.new( SAX.new )
169
+ rescue Exception => e
170
+ $stderr.puts "Parser raised #{e} on #{data}"
171
+ exit 1
172
+ end
173
+ end
174
+ def unbind
175
+ $failed_processes += 1 if get_status.exitstatus &&
176
+ get_status.exitstatus > 0
177
+ EM.stop
178
+ end
179
+ end
180
+
181
+ date = Time.now
182
+ failures = nil
183
+ examples = nil
184
+ pending = nil
185
+
186
+ EM.run do
187
+ EM.popen cmd, WriteToNokogiri
188
+ end
189
+
190
+ puts
191
+ puts
192
+
193
+ if !$jslintrb_xml
194
+ if $spec_info[:pending].length > 0
195
+ puts "Pending:"
196
+ $spec_info[:pending].each do |pending|
197
+ puts
198
+ puts yellow(pending[:name])
199
+ end
200
+ puts "\n"
201
+ end
202
+ $spec_info[:failures].each_with_index do |failure,i|
203
+ print i+1,")\n"
204
+ print red("#{failure[:name]} FAILED\n")
205
+ print red(failure[:message]),"\n"
206
+ # double check, but is there really anything in the jslint stack that's useful?
207
+ stack = failure[:stack]
208
+ stack and ( stack = stack.split "\n" )
209
+ stack and stack.each do |line|
210
+ if line =~ %r(/jasmine.js:\d+) || line =~ %r(/jasmine/src/.*\.js:\d+)
211
+ break
212
+ end
213
+ print line, "\n"
214
+ end
215
+ # p stack
216
+ # print failure[:stack],"\n"
217
+ end
218
+ puts "Finished in #{Time.now - date} seconds"
219
+ puts
220
+ examples = $spec_info[:specs].length
221
+ # require 'pp'
222
+ # pp $spec_info[:specs]
223
+ failures = $spec_info[:failures].length
224
+ pending = $spec_info[:pending].length
225
+ if $failed_processes > 0
226
+ examples += $failed_processes
227
+ failures += $failed_processes
228
+ end
229
+ msg = "#{examples} examples, #{failures} failures, #{pending} pending"
230
+ if failures > 0
231
+ msg = red(msg)
232
+ elsif pending > 0
233
+ msg = yellow(msg)
234
+ else
235
+ msg = green(msg)
236
+ end
237
+ puts msg
238
+ end
239
+
240
+ exit ( !failures.nil? && failures > 0 ) ? 1 : 0
data/bin/jslintrb~ ADDED
@@ -0,0 +1,223 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ require 'jslintrb/options'
6
+
7
+ ENV["JSLINTRB_JS_PATH"] =
8
+ JSLINT_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'jslintrb'))
9
+
10
+ intro = File.join(JSLINT_DIR, "intro.js")
11
+ outro = File.join(JSLINT_DIR, "outro.js")
12
+
13
+ args = [ "envjsrb" ]
14
+
15
+ args << intro
16
+
17
+ introd = true
18
+ outrod = true
19
+
20
+ ARGV.each do |f|
21
+
22
+ if f =~ /\.x?html?$/ || f =~ %r(^https?://)
23
+ if introd && !outrod
24
+ args << outro
25
+ args << "about:blank"
26
+ outrod = true
27
+ introd = false
28
+ end
29
+ if !introd
30
+ args << intro
31
+ end
32
+ args << f
33
+ introd = true
34
+ outrod = false
35
+ elsif f == "about:blank"
36
+ if introd && !outrod
37
+ args << outro
38
+ outrod = true
39
+ introd = false
40
+ end
41
+ args << f
42
+ else
43
+ args << f
44
+ end
45
+
46
+ end
47
+
48
+ args << outro
49
+
50
+ cmd = args.join(" ")
51
+
52
+ puts cmd if $jslintrb_verbose
53
+
54
+ require 'eventmachine'
55
+ require 'nokogiri'
56
+
57
+ class Restart < Exception; end
58
+
59
+ def colour(text, colour_code)
60
+ # return text unless ENV['RSPEC_COLOR'] || (colour? & (autospec? || output_to_tty?))
61
+ "#{colour_code}#{text}\e[0m"
62
+ end
63
+
64
+ def green(text); colour(text, "\e[32m"); end
65
+ def red(text); colour(text, "\e[31m"); end
66
+ def yellow(text); colour(text, "\e[33m"); end
67
+ def blue(text); colour(text, "\e[34m"); end
68
+
69
+ $spec_info = {}
70
+ $spec_info[:specs] = []
71
+ $spec_info[:failures] = []
72
+ $spec_info[:pending] = []
73
+
74
+ class SAX < Nokogiri::XML::SAX::Document
75
+
76
+ def start_element name, attrs
77
+ # p attrs
78
+ attrs = Hash[*attrs]
79
+ # p attrs
80
+ case name
81
+ when "testsuites";
82
+ when "testsuite";
83
+ @testsuite_name = attrs["name"]
84
+ if @testsuite_pending = ( attrs["skipped"] == "true" )
85
+ $spec_info[:specs] << (tc = {})
86
+ tc[:name] = @testsuite_name
87
+ $spec_info[:pending] << $spec_info[:specs].last
88
+ end
89
+ when "testcase";
90
+ $spec_info[:specs] << (tc = {})
91
+ tc[:name] = @testsuite_name + " : " + attrs["name"]
92
+ @testcase_failed = false
93
+ when "failure";
94
+ $spec_info[:failures] << (tc = $spec_info[:specs].last)
95
+ tc[:message] = attrs["message"]
96
+ @testcase_failed = true
97
+ else; raise "hell: #{name}"
98
+ end
99
+ end
100
+
101
+ def cdata_block string
102
+ if @testcase_failed
103
+ $spec_info[:failures].last[:stack] ||= ""
104
+ $spec_info[:failures].last[:stack] << string
105
+ end
106
+ end
107
+
108
+ def end_element name
109
+ case name
110
+ when "testsuites";
111
+ raise Restart
112
+ when "testsuite";
113
+ if !$jslintrb_xml
114
+ if @testsuite_pending
115
+ char = yellow("*")
116
+ print char
117
+ $stdout.flush
118
+ end
119
+ end
120
+ when "testcase";
121
+ if !$jslintrb_xml
122
+ char = nil
123
+ if @testcase_failed
124
+ char = red("F")
125
+ else
126
+ char = green(".")
127
+ end
128
+ print char
129
+ $stdout.flush
130
+ end
131
+ when "failure";
132
+ else; raise "hell: #{name}"
133
+ end
134
+ end
135
+ end
136
+
137
+ $failed_processes = 0
138
+
139
+ module WriteToNokogiri
140
+ def post_init
141
+ @parser = Nokogiri::XML::SAX::PushParser.new( SAX.new )
142
+ end
143
+ def receive_data data
144
+ begin
145
+ if $jslintrb_xml
146
+ # $stderr.print data
147
+ puts data
148
+ $stdout.flush
149
+ end
150
+ @parser << data
151
+ rescue Restart
152
+ @parser = Nokogiri::XML::SAX::PushParser.new( SAX.new )
153
+ rescue Exception => e
154
+ $stderr.puts "Parser raised #{e} on #{data}"
155
+ exit 1
156
+ end
157
+ end
158
+ def unbind
159
+ $failed_processes += 1 if get_status.exitstatus &&
160
+ get_status.exitstatus > 0
161
+ EM.stop
162
+ end
163
+ end
164
+
165
+ date = Time.now
166
+ failures = nil
167
+ examples = nil
168
+ pending = nil
169
+
170
+ EM.run do
171
+ EM.popen cmd, WriteToNokogiri
172
+ end
173
+
174
+ puts
175
+ puts
176
+
177
+ if !$jslintrb_xml
178
+ if $spec_info[:pending].length > 0
179
+ puts "Pending:"
180
+ $spec_info[:pending].each do |pending|
181
+ puts
182
+ puts yellow(pending[:name])
183
+ end
184
+ puts "\n"
185
+ end
186
+ $spec_info[:failures].each_with_index do |failure,i|
187
+ print i+1,")\n"
188
+ print red("#{failure[:name]} FAILED\n")
189
+ print red(failure[:message]),"\n"
190
+ # double check, but is there really anything in the jslint stack that's useful?
191
+ stack = failure[:stack]
192
+ stack and ( stack = stack.split "\n" )
193
+ stack and stack.each do |line|
194
+ if line =~ %r(/jasmine.js:\d+) || line =~ %r(/jasmine/src/.*\.js:\d+)
195
+ break
196
+ end
197
+ print line, "\n"
198
+ end
199
+ # p stack
200
+ # print failure[:stack],"\n"
201
+ end
202
+ puts "Finished in #{Time.now - date} seconds"
203
+ puts
204
+ examples = $spec_info[:specs].length
205
+ # require 'pp'
206
+ # pp $spec_info[:specs]
207
+ failures = $spec_info[:failures].length
208
+ pending = $spec_info[:pending].length
209
+ if $failed_processes > 0
210
+ examples += $failed_processes
211
+ failures += $failed_processes
212
+ end
213
+ msg = "#{examples} examples, #{failures} failures, #{pending} pending"
214
+ if failures > 0
215
+ msg = red(msg)
216
+ elsif pending > 0
217
+ msg = yellow(msg)
218
+ else
219
+ msg = green(msg)
220
+ end
221
+ puts msg
222
+ end
223
+ exit ( !failures.nil? && failures > 0 ) ? 1 : 0