fdlint 0.1.3 → 0.2.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +2 -9
  3. data/Gemfile.lock +14 -4
  4. data/Rakefile +9 -85
  5. data/bin/fdlint +71 -8
  6. data/lib/fdlint/cli.rb +102 -0
  7. data/lib/{file_validator.rb → fdlint/file_validator.rb} +0 -0
  8. data/lib/fdlint/helper/code_type.rb +55 -0
  9. data/lib/fdlint/helper/file_reader.rb +19 -0
  10. data/lib/fdlint/helper/logger.rb +35 -0
  11. data/lib/fdlint/log_entry.rb +51 -0
  12. data/lib/fdlint/parser/base_parser.rb +151 -0
  13. data/lib/{css/parser.rb → fdlint/parser/css/css_parser.rb} +31 -28
  14. data/lib/{css → fdlint/parser/css}/struct.rb +5 -5
  15. data/lib/{encoding_error.rb → fdlint/parser/encoding_error.rb} +2 -2
  16. data/lib/{html/parser.rb → fdlint/parser/html/html_parser.rb} +26 -20
  17. data/lib/{html → fdlint/parser/html}/query.rb +0 -0
  18. data/lib/{html → fdlint/parser/html}/rule/check_tag_rule.rb +0 -0
  19. data/lib/{html → fdlint/parser/html}/struct.rb +82 -38
  20. data/lib/fdlint/parser/js/expr/expr.rb +71 -0
  21. data/lib/fdlint/parser/js/expr/left_hand.rb +65 -0
  22. data/lib/fdlint/parser/js/expr/operate.rb +94 -0
  23. data/lib/fdlint/parser/js/expr/primary.rb +168 -0
  24. data/lib/{js/parser.rb → fdlint/parser/js/js_parser.rb} +11 -9
  25. data/lib/fdlint/parser/js/stat/if.rb +27 -0
  26. data/lib/fdlint/parser/js/stat/iter.rb +88 -0
  27. data/lib/fdlint/parser/js/stat/stat.rb +120 -0
  28. data/lib/fdlint/parser/js/stat/switch.rb +67 -0
  29. data/lib/fdlint/parser/js/stat/try.rb +30 -0
  30. data/lib/fdlint/parser/js/stat/var.rb +42 -0
  31. data/lib/fdlint/parser/js/struct.rb +257 -0
  32. data/lib/fdlint/parser/node.rb +27 -0
  33. data/lib/fdlint/parser/parse_error.rb +10 -0
  34. data/lib/fdlint/parser/parser_visitable.rb +134 -0
  35. data/lib/{position_info.rb → fdlint/parser/position_info.rb} +12 -1
  36. data/lib/fdlint/parser.rb +3 -0
  37. data/lib/fdlint/printer/base_printer.rb +20 -0
  38. data/lib/fdlint/printer/console_printer.rb +80 -0
  39. data/lib/fdlint/printer/nocolor_printer.rb +29 -0
  40. data/lib/fdlint/printer/vim_printer.rb +21 -0
  41. data/lib/fdlint/printer.rb +1 -0
  42. data/lib/fdlint/rule/dsl.rb +83 -0
  43. data/lib/fdlint/rule/validation.rb +79 -0
  44. data/lib/fdlint/rule.rb +81 -0
  45. data/lib/fdlint/support/core/array.rb +5 -0
  46. data/lib/fdlint/support/core/file.rb +8 -0
  47. data/lib/fdlint/support/core/hash.rb +5 -0
  48. data/lib/fdlint/support/core/nil.rb +7 -0
  49. data/lib/fdlint/support/core/string.rb +7 -0
  50. data/lib/fdlint/support/core_ext.rb +5 -0
  51. data/lib/fdlint/validator.rb +102 -0
  52. data/lib/fdlint/version.rb +3 -0
  53. data/lib/fdlint.rb +5 -0
  54. data/rules.d/css.rule.rb +100 -0
  55. data/rules.d/filename.rule.rb +49 -0
  56. data/rules.d/html.rule.rb +169 -0
  57. data/rules.d/js.jquery.rule.rb +49 -0
  58. data/rules.d/js.rule.rb +205 -0
  59. data/test/default_test.rb +14 -0
  60. data/test/fixtures/js/scope-test.js +15 -2
  61. data/test/test_helper.rb +9 -0
  62. metadata +269 -221
  63. data/lib/base_parser.rb +0 -143
  64. data/lib/cmd_runner.rb +0 -145
  65. data/lib/context.rb +0 -31
  66. data/lib/css/reader.rb +0 -30
  67. data/lib/css/rule/check_compression_rule.rb +0 -48
  68. data/lib/css/rule/checklist.rb +0 -45
  69. data/lib/helper/code_type.rb +0 -50
  70. data/lib/helper/color_string.rb +0 -44
  71. data/lib/helper/file_reader.rb +0 -22
  72. data/lib/helper/strenc.rb +0 -65
  73. data/lib/js/expr/expr.rb +0 -66
  74. data/lib/js/expr/left_hand.rb +0 -63
  75. data/lib/js/expr/operate.rb +0 -92
  76. data/lib/js/expr/primary.rb +0 -166
  77. data/lib/js/rule/all.rb +0 -35
  78. data/lib/js/rule/checklist.rb +0 -41
  79. data/lib/js/rule/file_checker.rb +0 -42
  80. data/lib/js/rule/helper.rb +0 -96
  81. data/lib/js/rule/no_global.rb +0 -87
  82. data/lib/js/stat/if.rb +0 -25
  83. data/lib/js/stat/iter.rb +0 -85
  84. data/lib/js/stat/stat.rb +0 -117
  85. data/lib/js/stat/switch.rb +0 -65
  86. data/lib/js/stat/try.rb +0 -28
  87. data/lib/js/stat/var.rb +0 -40
  88. data/lib/js/struct.rb +0 -248
  89. data/lib/log_entry.rb +0 -49
  90. data/lib/node.rb +0 -28
  91. data/lib/parse_error.rb +0 -13
  92. data/lib/parser_visitable.rb +0 -138
  93. data/lib/printer/base_printer.rb +0 -24
  94. data/lib/printer/console_printer.rb +0 -66
  95. data/lib/printer/nocolor_printer.rb +0 -27
  96. data/lib/printer/vim_printer.rb +0 -19
  97. data/lib/rule.rb +0 -241
  98. data/lib/rule_helper.rb +0 -14
  99. data/lib/runner.rb +0 -225
  100. data/rules.d/css.rule +0 -127
  101. data/rules.d/html.dtd.rule +0 -22
  102. data/rules.d/html.prop.rule +0 -51
  103. data/rules.d/html.tag.rule +0 -136
  104. data/rules.d/js.file.rule +0 -13
  105. data/rules.d/js.jquery.rule +0 -56
  106. data/rules.d/js.mergefile.rule +0 -71
  107. data/rules.d/js.rule +0 -84
@@ -1,138 +0,0 @@
1
- require 'observer'
2
-
3
- require_relative 'log_entry'
4
-
5
- module XRay
6
-
7
- class VisitResult < LogEntry
8
-
9
- attr_reader :node
10
-
11
- def initialize(node, message, level)
12
- @node = node
13
- pos = node.position || Position.new(0, 0, 0)
14
- super message, level, pos.row, pos.column
15
- end
16
- end
17
-
18
-
19
- module ParserVisitable
20
-
21
- include Observable
22
-
23
- def self.included(klass)
24
- klass.public_instance_methods(true).each do |method|
25
- wrap(klass, method)
26
- end
27
-
28
- def klass.method_added(method)
29
- unless @flag
30
- @flag = true
31
- ParserVisitable.wrap(self, method)
32
- @flag = false
33
- end
34
- end
35
- end
36
-
37
- def self.wrap(klass, method)
38
- method = method.to_s
39
- unless method.index 'parse_'
40
- return
41
- end
42
-
43
- klass.instance_eval do
44
- old_method = "#{method}_without_visit"
45
- name = method.sub /^parse_/, ''
46
- alias_method old_method, method
47
-
48
- define_method(method) do |*args, &block|
49
- before name, *args
50
- node = self.send(old_method, *args, &block)
51
- node && visit(name, node)
52
- node
53
- end
54
- end
55
- end
56
-
57
- def parse_no_throw
58
- root = []
59
- begin
60
- root = self.parse
61
- rescue ParseError => e
62
- @results ||= []
63
- @results << LogEntry.new(e.to_s, :fatal, e.position.row, e.position.column)
64
- end
65
- root
66
- end
67
-
68
- def add_visitor(visitor)
69
- @visitors ||= {}
70
-
71
- visitor.class.public_instance_methods(true).each do |method|
72
- method = method.to_s
73
- if method.index('visit_') == 0 ||
74
- method.index('before_parse_') == 0
75
- cache = @visitors[method] ||= []
76
- cache << visitor
77
- end
78
- end
79
- end
80
-
81
- def add_visitors(visitors)
82
- visitors.each { |visitor| add_visitor visitor }
83
- end
84
-
85
- def results
86
- @results || []
87
- end
88
-
89
- private
90
-
91
- def visit(name, node)
92
- walk(name, 'visit_', node) do |result|
93
- result && notify(node, result)
94
- end
95
- end
96
-
97
- def before(name, *args)
98
- walk name, 'before_parse_', *args
99
- end
100
-
101
- def walk(name, prefix, *args, &block)
102
- unless @visitors
103
- return
104
- end
105
-
106
- method = prefix + name
107
- visitors = @visitors[method]
108
- visitors && visitors.each do |visitor|
109
- result = visitor.send method, *args
110
- block && block.call(result)
111
- end
112
- end
113
-
114
- def notify(node, results)
115
- @results ||= []
116
- if results.is_a?(Array) && results.size > 0
117
- if results[0].is_a? Array
118
- results.each do |ret|
119
- do_notify node, ret, ret.is_a?(VisitResult)
120
- end
121
- else
122
- do_notify node, results
123
- end
124
- elsif results.is_a? VisitResult
125
- do_notify node, results, true
126
- end
127
- end
128
-
129
- def do_notify(node, ret, flag = false)
130
- ret = VisitResult.new(node, ret[0], ret[1]) unless flag
131
- @results << ret
132
- self.changed
133
- notify_observers ret, self
134
- end
135
-
136
- end
137
-
138
- end
@@ -1,24 +0,0 @@
1
- module XRay
2
-
3
- def self.printer
4
- @@printer
5
- end
6
-
7
- def self.register_printer(klass)
8
- @@printer = klass
9
- end
10
-
11
- class BasePrinter
12
-
13
- XRay.register_printer self
14
-
15
- def initialize(results, opt)
16
- @results, @opt = results, opt
17
- end
18
-
19
- def print
20
- end
21
-
22
- end
23
-
24
- end
@@ -1,66 +0,0 @@
1
- require_relative 'base_printer'
2
-
3
- if ENV['OS'] =~ /windows/i
4
- require 'win32console'
5
- end
6
-
7
-
8
- module XRay
9
-
10
- class ConsolePrinter < BasePrinter
11
-
12
- XRay.register_printer self
13
-
14
- def print
15
- if @opt[:source]
16
- print_with_source
17
- else
18
- if @results.empty?
19
- puts "[OK]".white.green_bg << " #{@opt[:file]}"
20
- else
21
- prf = @opt[:prefix]
22
- suf = @opt[:suffix]
23
-
24
- puts ""
25
- puts "[EE] #{@opt[:file]}".white.magenta_bg
26
-
27
- @results.each do |r|
28
- puts "#{prf}#{r.to_color_s}#{suf}"
29
- end
30
- end
31
- end
32
- end
33
-
34
-
35
- def print_with_source
36
- if @results.empty?
37
- puts "[OK]".white.green_bg << " #{@opt[:file]}"
38
- else
39
- source = @opt[:source]
40
- lines = source.split(/\r\n|\n|\r/)
41
- prf = @opt[:prefix]
42
- suf = @opt[:suffix]
43
-
44
- puts "[EE] #{@opt[:file]}".white.magenta_bg
45
-
46
- @results.each do |r|
47
- if r.row && r.row > 0
48
- col = r.column - 1
49
- row = r.row - 1
50
- left = col - 50
51
- right = col + 50
52
- left = 0 if left < 0
53
-
54
- puts "#{prf}#{lines[row][left..right].gsub(/\t/, ' ')}"
55
- puts "#{prf}#{' ' * (col - left)}^ #{r.to_color_s}"
56
- puts "\n"
57
- else
58
- puts "#{r.to_color_s}#{suf}\n"
59
- end
60
- end
61
- end
62
-
63
- end
64
- end
65
-
66
- end
@@ -1,27 +0,0 @@
1
- require_relative 'base_printer'
2
-
3
- module XRay
4
-
5
- class NoColorPrinter < BasePrinter
6
-
7
- XRay.register_printer self
8
-
9
- def print
10
- if @results.empty?
11
- puts "[OK] #{@opt[:file]}"
12
- else
13
- prf = @opt[:prefix]
14
- suf = @opt[:suffix]
15
- out = @opt[:out]
16
-
17
- puts "[EE] #{@opt[:file]}"
18
-
19
- @results.each do |r|
20
- puts "#{prf}#{r}#{suf}"
21
- end
22
- end
23
- end
24
-
25
- end
26
-
27
- end
@@ -1,19 +0,0 @@
1
- require_relative 'base_printer'
2
-
3
- module XRay
4
-
5
- class VimPrinter < BasePrinter
6
-
7
- XRay.register_printer self
8
-
9
- def print
10
- prf = (@opt[:file]||'-').dup.utf8!
11
- @results.each do |r|
12
- puts "#{prf}:[#{r.level}]:#{r.row},#{r.column}:#{r.message}"
13
- end
14
-
15
- end
16
-
17
- end
18
-
19
- end
data/lib/rule.rb DELETED
@@ -1,241 +0,0 @@
1
- require 'find'
2
- require_relative 'js/parser'
3
- require_relative 'html/parser'
4
- require_relative 'css/parser'
5
- require_relative 'helper/file_reader'
6
- require_relative 'node'
7
-
8
- module XRay
9
-
10
- module Rule
11
-
12
- STYLE_TYPES = [:css, :js, :html]
13
-
14
- def self.methods_to_keywords(klass)
15
- klass.instance_methods.grep( /^parse_/ ).map {|m| m[/^parse_(.*)/,1]}
16
- end
17
-
18
- RULE_PATH = File.expand_path '../rules.d', File.dirname(__FILE__)
19
- KEYWORDS = %w(file merge_importing merge_file)
20
-
21
- KEYWORDS.concat methods_to_keywords(XRay::JS::Parser)
22
- KEYWORDS.concat methods_to_keywords(XRay::CSS::Parser)
23
- KEYWORDS.concat methods_to_keywords(XRay::HTML::Parser)
24
-
25
- @@common_rules = []
26
- @@context = nil
27
-
28
- def syntax(*type)
29
- if type.empty?
30
- @syntax || 'common'
31
- else
32
- @syntax = type.first
33
- end
34
- end
35
-
36
- def common(&block)
37
- syntax nil
38
- yield if block_given?
39
- end
40
-
41
- KEYWORDS.each do |kw|
42
- eval <<-RUBY
43
- def #{kw}_rules
44
- @@common_rules.select {|r| r[:name].to_s.include? "#{kw}" }
45
- end
46
-
47
- def check_#{kw}( *target, &block )
48
- if block_given?
49
- self.send :"add_\#{syntax}_rule", :check_#{kw}, &block
50
- elsif target.size > 0
51
- do_check #{kw}_rules, *target
52
- end
53
- end
54
-
55
- RUBY
56
-
57
- STYLE_TYPES.each do |syn|
58
- eval <<-RUBY
59
- def #{syn}_#{kw}_rules
60
- #{syn}_rules.select {|r| r[:name].to_s.include? "#{kw}" }
61
- end
62
-
63
- def only_#{syn}_#{kw}_rules
64
- only_#{syn}_rules.select {|r| r[:name].to_s.include? "#{kw}" }
65
- end
66
-
67
- def check_#{syn}_#{kw}( *target, &block )
68
- if block_given?
69
- self.send :"add_#{syn}_rule", :check_#{syn}_#{kw}, &block
70
- elsif target.size > 0
71
- do_check #{syn}_#{kw}_rules, *target
72
- end
73
- end
74
-
75
- RUBY
76
- end
77
- end
78
-
79
- STYLE_TYPES.each do |syn|
80
-
81
- eval <<-RUBY
82
-
83
- @@#{syn}_rules = []
84
-
85
- def #{syn}
86
- syntax #{syn.inspect}
87
- yield if block_given?
88
- end
89
-
90
- def add_#{syn}_rule( name, &block )
91
- @@#{syn}_rules << { :name => name, :block => block }
92
- end
93
-
94
- def #{syn}_rules
95
- @@common_rules + @@#{syn}_rules
96
- end
97
-
98
- def only_#{syn}_rules
99
- @@#{syn}_rules
100
- end
101
-
102
- def clear_#{syn}_rules
103
- @@#{syn}_rules.clear
104
- end
105
- RUBY
106
- end
107
-
108
- def clear_all_rules
109
- @@imported.clear
110
- @@common_rules.clear
111
- STYLE_TYPES.each { |syn| send(:"clear_#{syn}_rules") }
112
- end
113
-
114
- def do_check( rules, *args )
115
- @@context = self
116
- target = args.first
117
- rules.inject([]) do |results, r|
118
- result = r[:block].call(*args)
119
- results << result if result
120
- results
121
- end
122
- end
123
-
124
- def context
125
- @@context
126
- end
127
-
128
- def method_missing( name , *args, &block )
129
- if cmd = cmd_name(name) and self.respond_to? cmd
130
- send(cmd, name, &block )
131
- def_rule_cmd(cmd, name, &block) if block_given?
132
- else
133
- super
134
- end
135
- end
136
-
137
- def def_rule_cmd(cmd, name, &block)
138
- XRay::Rule.instance_eval do
139
- define_method(name) do |*tar, &b|
140
- if tar.size > 0
141
- @@context = self
142
- block.call *tar
143
- else
144
- send cmd, name, &b
145
- end
146
- end
147
- end
148
- end
149
- private :def_rule_cmd
150
-
151
- def cmd_name(name)
152
- name = name.to_s
153
- KEYWORDS.each do |kw|
154
- if name.start_with? "check_#{kw}"
155
- return :"check_#{kw}"
156
- else
157
- STYLE_TYPES.each do |s|
158
- cmd = "check_#{s}_#{kw}"
159
- return cmd if name.start_with? cmd
160
- end
161
- end
162
- end
163
- nil
164
- end
165
-
166
- def add_common_rule( name, &block )
167
- @@common_rules << { :name => name , :block => block }
168
- end
169
-
170
- alias :add_rule :add_common_rule
171
-
172
-
173
-
174
- @@imported = []
175
-
176
- def import( name )
177
- if name.is_a? Symbol
178
- path = File.join(RULE_PATH, "#{name}.rule")
179
- else
180
- path = name
181
- end
182
- path = File.expand_path path
183
- unless imported? path
184
- @@imported << path
185
- src, enc = ::XRay::Helper::FileReader.readfile path
186
- instance_eval src
187
- end
188
- end
189
-
190
- def import_all
191
- Find.find( RULE_PATH ) do |rule|
192
- import rule if rule.end_with? '.rule'
193
- end
194
- end
195
-
196
- def self.import_all
197
- "".extend(self).import_all
198
- end
199
-
200
- def imported?(path)
201
- @@imported.include? path
202
- end
203
-
204
- def imported
205
- @@imported
206
- end
207
-
208
- end
209
-
210
- end
211
-
212
-
213
- if __FILE__ == $0
214
-
215
- extend XRay::Rule
216
-
217
- ## Rule DSL example
218
- css {
219
- check_file_without_min { |file|
220
- ['should not be min file', :error] if file =~ /-min\.css$/
221
- }
222
-
223
- }
224
-
225
- common {
226
- check_file { |file|
227
- ['should use "-" instead of "_"', :warn] if file =~ /_/
228
- }
229
- }
230
-
231
- ## Test
232
- import_all
233
-
234
- puts file_rules
235
- puts css_file_rules
236
-
237
- puts check_file_without_min('test-min.css').inspect
238
- puts check_css_file('test_a-min.css').inspect
239
- puts check_css_value('expression(test)\0').inspect
240
-
241
- end
data/lib/rule_helper.rb DELETED
@@ -1,14 +0,0 @@
1
- module XRay
2
- module RuleHelper
3
-
4
- def dispatch(items, node)
5
- results = []
6
- items.each do |item|
7
- result = self.send(item, node)
8
- result && (results << result.flatten)
9
- end
10
- results
11
- end
12
-
13
- end
14
- end