jslintrb_v8 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,188 @@
1
+ require 'v8'
2
+ # JSLint bindings for ruby using v8.
3
+ #
4
+ # Usage:
5
+ #
6
+ # require 'jslintrb-v8'
7
+ # puts JSLint.new.check("var x = 5")
8
+ #
9
+ # will output:
10
+ #
11
+ # Error at line 1 character 1: Missing "use strict" statement.
12
+ # var x = 5
13
+ # Error at line 1 character 10: Missing semicolon.
14
+ # var x = 5
15
+ #
16
+ # Pass options into the constructor:
17
+ #
18
+ # JSLint.new(:undef => false, :sub => true)
19
+ class JSLint
20
+ # if ADsafe should be enforced
21
+ attr_accessor :adsafe
22
+ # if bitwise operators should not be allowed
23
+ attr_accessor :bitwise
24
+ # if the standard browser globals should be predefined
25
+ attr_accessor :browser
26
+ # if upper case HTML should be allowed
27
+ attr_accessor :cap
28
+ # if CSS workarounds should be tolerated
29
+ attr_accessor :css
30
+ # if debugger statements should be allowed
31
+ attr_accessor :debug
32
+ # if === should be required
33
+ attr_accessor :eqeqeq
34
+ # if eval should be allowed
35
+ attr_accessor :evil
36
+ # if for in statements must filter
37
+ attr_accessor :forin
38
+ # if HTML fragments should be allowed
39
+ attr_accessor :fragment
40
+ # if immediate invocations must be wrapped in parens
41
+ attr_accessor :immed
42
+ # if line breaks should not be checked
43
+ attr_accessor :laxbreak
44
+ # if constructor names must be capitalized
45
+ attr_accessor :newcap
46
+ # if names should be checked
47
+ attr_accessor :nomen
48
+ # if HTML event handlers should be allowed
49
+ attr_accessor :on
50
+ # if only one var statement per function should be allowed
51
+ attr_accessor :onevar
52
+ # if the scan should stop on first error
53
+ attr_accessor :passfail
54
+ # if increment/decrement should not be allowed
55
+ attr_accessor :plusplus
56
+ # if the . should not be allowed in regexp literals
57
+ attr_accessor :regexp
58
+ # if the Rhino environment globals should be predefined
59
+ attr_accessor :rhino
60
+ # if variables should be declared before used
61
+ attr_accessor :undef
62
+ # if use of some browser features should be restricted
63
+ attr_accessor :safe
64
+ # if the System object should be predefined
65
+ attr_accessor :sidebar
66
+ # require the "use strict"; pragma
67
+ attr_accessor :strict
68
+ # if all forms of subscript notation are tolerated
69
+ attr_accessor :sub
70
+ # if strict whitespace rules apply
71
+ attr_accessor :white
72
+ # if the Yahoo Widgets globals should be predefined
73
+ attr_accessor :widget
74
+
75
+ def initialize(opts = {})
76
+ @adsafe = opts.fetch( :adsafe ){ false }
77
+ @bitwise = opts.fetch( :bitwise ){ true }
78
+ @browser = opts.fetch( :browser ){ false }
79
+ @cap = opts.fetch( :cap ){ false }
80
+ @css = opts.fetch( :css ){ false }
81
+ @debug = opts.fetch( :debug ){ false }
82
+ @eqeqeq = opts.fetch( :eqeqeq ){ true }
83
+ @evil = opts.fetch( :evil ){ false }
84
+ @forin = opts.fetch( :forin ){ false }
85
+ @fragment = opts.fetch( :fragment ){ false }
86
+ @immed = opts.fetch( :immed ){ true }
87
+ @laxbreak = opts.fetch( :laxbreak ){ false }
88
+ @newcap = opts.fetch( :newcap ){ true }
89
+ @nomen = opts.fetch( :nomen ){ true }
90
+ @on = opts.fetch( :on ){ false }
91
+ @onevar = opts.fetch( :onevar ){ true }
92
+ @passfail = opts.fetch( :passfail ){ false }
93
+ @plusplus = opts.fetch( :plusplus ){ true }
94
+ @regexp = opts.fetch( :regexp ){ true }
95
+ @rhino = opts.fetch( :rhino ){ false }
96
+ @undef = opts.fetch( :undef ){ true }
97
+ @safe = opts.fetch( :safe ){ false }
98
+ @sidebar = opts.fetch( :sidebar ){ false }
99
+ @strict = opts.fetch( :strict ){ true }
100
+ @sub = opts.fetch( :sub ){ false }
101
+ @white = opts.fetch( :white ){ false }
102
+ @widget = opts.fetch( :widget ){ false }
103
+ end
104
+
105
+ def check(input)
106
+ errors = []
107
+ V8::Context.new do |context|
108
+ context.load(File.join(File.dirname(__FILE__), 'jslintrb-v8', 'jslint.js'))
109
+
110
+ context['JSLintRBinput'] = lambda{ input }
111
+ context['JSLintRBadsafe'] = @adsafe
112
+ context['JSLintRBbitwise'] = @bitwise
113
+ context['JSLintRBbrowser'] = @browser
114
+ context['JSLintRBcap'] = @cap
115
+ context['JSLintRBcss'] = @css
116
+ context['JSLintRBdebug'] = @debug
117
+ context['JSLintRBeqeqeq'] = @eqeqeq
118
+ context['JSLintRBevil'] = @evil
119
+ context['JSLintRBforin'] = @forin
120
+ context['JSLintRBfragment'] = @fragment
121
+ context['JSLintRBimmed'] = @immed
122
+ context['JSLintRBlaxbreak'] = @laxbreak
123
+ context['JSLintRBnewcap'] = @newcap
124
+ context['JSLintRBnomen'] = @nomen
125
+ context['JSLintRBon'] = @on
126
+ context['JSLintRBonevar'] = @onevar
127
+ context['JSLintRBpassfail'] = @passfail
128
+ context['JSLintRBplusplus'] = @plusplus
129
+ context['JSLintRBregexp'] = @regexp
130
+ context['JSLintRBrhino'] = @rhino
131
+ context['JSLintRBundef'] = @undef
132
+ context['JSLintRBsafe'] = @safe
133
+ context['JSLintRBsidebar'] = @sidebar
134
+ context['JSLintRBstrict'] = @strict
135
+ context['JSLintRBsub'] = @sub
136
+ context['JSLintRBwhite'] = @white
137
+ context['JSLintRBwidget'] = @widget
138
+ context['JSLintRBreportErrors'] = lambda{|js_errors|
139
+ js_errors.each do |e|
140
+ if V8::Object === e
141
+ e = V8::To.rb(e)
142
+ errors << "Error at line #{e['line'].to_i + 1} " +
143
+ "character #{e['character'].to_i + 1}: #{e['reason']}"
144
+ errors << " #{e['evidence']}"
145
+ end
146
+ end
147
+ }
148
+ context.eval %{
149
+ JSLINT(JSLintRBinput(), {
150
+ adsafe : JSLintRBadsafe,
151
+ bitwise : JSLintRBbitwise,
152
+ browser : JSLintRBbrowser,
153
+ cap : JSLintRBcap,
154
+ css : JSLintRBcss,
155
+ debug : JSLintRBdebug,
156
+ eqeqeq : JSLintRBeqeqeq,
157
+ evil : JSLintRBevil,
158
+ forin : JSLintRBforin,
159
+ fragment : JSLintRBfragment,
160
+ immed : JSLintRBimmed,
161
+ laxbreak : JSLintRBlaxbreak,
162
+ newcap : JSLintRBnewcap,
163
+ nomen : JSLintRBnomen,
164
+ on : JSLintRBon,
165
+ onevar : JSLintRBonevar,
166
+ passfail : JSLintRBpassfail,
167
+ plusplus : JSLintRBplusplus,
168
+ regexp : JSLintRBregexp,
169
+ rhino : JSLintRBrhino,
170
+ undef : JSLintRBundef,
171
+ safe : JSLintRBsafe,
172
+ sidebar : JSLintRBsidebar,
173
+ strict : JSLintRBstrict,
174
+ sub : JSLintRBsub,
175
+ white : JSLintRBwhite,
176
+ widget : JSLintRBwidget
177
+ });
178
+ JSLintRBreportErrors(JSLINT.errors);
179
+ }
180
+ end
181
+
182
+ if errors.empty?
183
+ return nil
184
+ else
185
+ return errors.join("\n")
186
+ end
187
+ end
188
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jslintrb_v8
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Nick Gauthier
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-15 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: therubyracer
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 7
31
+ - 5
32
+ version: 0.7.5
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description:
36
+ email: ngauthier@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib/jslintrb-v8.rb
45
+ - lib/jslintrb-v8/jslint.js
46
+ - README.rdoc
47
+ has_rdoc: true
48
+ homepage: http://www.github.com/ngauthier/jslintrb_v8
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.7
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Run JSLint from Ruby
79
+ test_files: []
80
+