fdlint 0.1.0

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.
Files changed (218) hide show
  1. data/Gemfile +8 -0
  2. data/Gemfile.lock +14 -0
  3. data/README.md +68 -0
  4. data/Rakefile +92 -0
  5. data/bin/fdlint +17 -0
  6. data/lib/base_parser.rb +143 -0
  7. data/lib/cmd_runner.rb +145 -0
  8. data/lib/context.rb +31 -0
  9. data/lib/css/parser.rb +186 -0
  10. data/lib/css/reader.rb +30 -0
  11. data/lib/css/rule/check_compression_rule.rb +48 -0
  12. data/lib/css/rule/checklist.rb +45 -0
  13. data/lib/css/struct.rb +111 -0
  14. data/lib/encoding_error.rb +6 -0
  15. data/lib/file_validator.rb +38 -0
  16. data/lib/helper/code_type.rb +50 -0
  17. data/lib/helper/color_string.rb +44 -0
  18. data/lib/helper/file_reader.rb +22 -0
  19. data/lib/helper/strenc.rb +65 -0
  20. data/lib/html/parser.rb +212 -0
  21. data/lib/html/query.rb +96 -0
  22. data/lib/html/rule/check_tag_rule.rb +80 -0
  23. data/lib/html/struct.rb +291 -0
  24. data/lib/js/expr/expr.rb +66 -0
  25. data/lib/js/expr/left_hand.rb +63 -0
  26. data/lib/js/expr/operate.rb +92 -0
  27. data/lib/js/expr/primary.rb +166 -0
  28. data/lib/js/parser.rb +116 -0
  29. data/lib/js/rule/all.rb +35 -0
  30. data/lib/js/rule/checklist.rb +41 -0
  31. data/lib/js/rule/file_checker.rb +42 -0
  32. data/lib/js/rule/helper.rb +96 -0
  33. data/lib/js/rule/no_global.rb +87 -0
  34. data/lib/js/stat/if.rb +25 -0
  35. data/lib/js/stat/iter.rb +85 -0
  36. data/lib/js/stat/stat.rb +117 -0
  37. data/lib/js/stat/switch.rb +65 -0
  38. data/lib/js/stat/try.rb +28 -0
  39. data/lib/js/stat/var.rb +40 -0
  40. data/lib/js/struct.rb +248 -0
  41. data/lib/log_entry.rb +49 -0
  42. data/lib/node.rb +28 -0
  43. data/lib/parse_error.rb +13 -0
  44. data/lib/parser_visitable.rb +138 -0
  45. data/lib/position_info.rb +46 -0
  46. data/lib/printer/base_printer.rb +24 -0
  47. data/lib/printer/console_printer.rb +66 -0
  48. data/lib/printer/nocolor_printer.rb +27 -0
  49. data/lib/printer/vim_printer.rb +19 -0
  50. data/lib/rule.rb +241 -0
  51. data/lib/rule_helper.rb +14 -0
  52. data/lib/runner.rb +225 -0
  53. data/rules.d/css.rule +127 -0
  54. data/rules.d/html.dtd.rule +22 -0
  55. data/rules.d/html.prop.rule +51 -0
  56. data/rules.d/html.tag.rule +136 -0
  57. data/rules.d/js.file.rule +13 -0
  58. data/rules.d/js.jquery.rule +56 -0
  59. data/rules.d/js.mergefile.rule +71 -0
  60. data/rules.d/js.rule +84 -0
  61. data/test/all_tests.rb +84 -0
  62. data/test/cli/cli_test.rb +70 -0
  63. data/test/cli/log_level_test.rb +51 -0
  64. data/test/cli/output_format_test.rb +47 -0
  65. data/test/cli/type_test.rb +77 -0
  66. data/test/css/mac_line_end_support_test.rb +38 -0
  67. data/test/css/parser_test.rb +276 -0
  68. data/test/css/rule/check_encoding_test.rb +66 -0
  69. data/test/css/rule/check_list_rule_test.rb +167 -0
  70. data/test/css/rule/compression_test.rb +53 -0
  71. data/test/css/rule/file_name_test.rb +76 -0
  72. data/test/fixtures/css/broken.css +4 -0
  73. data/test/fixtures/css/cbu/36.css +52 -0
  74. data/test/fixtures/css/cbu/china_top.css +324 -0
  75. data/test/fixtures/css/cbu/default-merge.css +3 -0
  76. data/test/fixtures/css/cbu/default.css +13 -0
  77. data/test/fixtures/css/cbu/diy-merge.css +25 -0
  78. data/test/fixtures/css/cbu/fns-v1.css +27 -0
  79. data/test/fixtures/css/cbu/index_v0.1.css +12 -0
  80. data/test/fixtures/css/cbu/merge.css +11 -0
  81. data/test/fixtures/css/cbu/min.css +2 -0
  82. data/test/fixtures/css/cbu/my_home_admin.css +126 -0
  83. data/test/fixtures/css/cbu/nav.css +95 -0
  84. data/test/fixtures/css/cbu/pic_list.css +386 -0
  85. data/test/fixtures/css/cbu/quote-edit.css +18 -0
  86. data/test/fixtures/css/cbu/selloffer.shopwindow.css +1 -0
  87. data/test/fixtures/css/cbu/v1.css +9 -0
  88. data/test/fixtures/css/css3.css +30 -0
  89. data/test/fixtures/css/empty-min.css +0 -0
  90. data/test/fixtures/css/empty.css +0 -0
  91. data/test/fixtures/css/font-family.css +4 -0
  92. data/test/fixtures/css/gb-good.css +14 -0
  93. data/test/fixtures/css/gb_using_star.css +4 -0
  94. data/test/fixtures/css/import.css +18 -0
  95. data/test/fixtures/css/mac-line-sep-err-min.css +1 -0
  96. data/test/fixtures/css/mac-line-sep-err.css +1 -0
  97. data/test/fixtures/css/mac-line-sep-good-min.css +1 -0
  98. data/test/fixtures/css/mac-line-sep-good.css +1 -0
  99. data/test/fixtures/css/multi-encoding-in-a-file.css +0 -0
  100. data/test/fixtures/css/simple.css +1 -0
  101. data/test/fixtures/css/using_expr.css +8 -0
  102. data/test/fixtures/css/using_hack.css +21 -0
  103. data/test/fixtures/css/using_id.css +1 -0
  104. data/test/fixtures/css/using_star.css +4 -0
  105. data/test/fixtures/css/utf8_good.css +6 -0
  106. data/test/fixtures/css/utf8_good_declaring_charset.css +7 -0
  107. data/test/fixtures/css/utf8_using_star.css +5 -0
  108. data/test/fixtures/html/1-1.html +120 -0
  109. data/test/fixtures/html/1-2.html +120 -0
  110. data/test/fixtures/html/cms.html +373 -0
  111. data/test/fixtures/html/css_out_of_head.html +9 -0
  112. data/test/fixtures/html/fdev-template.html +22 -0
  113. data/test/fixtures/html/google.com.html +33 -0
  114. data/test/fixtures/html/mixed_log_levels.html +4 -0
  115. data/test/fixtures/html/mixed_types.html +13 -0
  116. data/test/fixtures/html/no_dtd.html +6 -0
  117. data/test/fixtures/html/readme.html +94 -0
  118. data/test/fixtures/html/review.board.html +163 -0
  119. data/test/fixtures/html/syntax_err.html +3 -0
  120. data/test/fixtures/html/train/detail/345/233/276/346/226/207/347/273/223/345/220/210.html +208 -0
  121. data/test/fixtures/html/train/detail/347/232/204Flash.html +212 -0
  122. data/test/fixtures/html/train/detail/347/232/204Vedio.html +212 -0
  123. data/test/fixtures/html/train/index.html +37 -0
  124. data/test/fixtures/html/train/test.html +1 -0
  125. data/test/fixtures/html/train//344/277/256/346/224/271/344/270/200/347/272/247/345/210/206/347/261/273.html +112 -0
  126. data/test/fixtures/html/train//344/277/256/346/224/271/345/255/220/345/210/206/347/261/273.html +108 -0
  127. data/test/fixtures/html/train//344/277/256/346/224/271/350/257/276/347/250/213.html +195 -0
  128. data/test/fixtures/html/train//345/215/232/345/256/242/350/256/276/347/275/256.html +142 -0
  129. data/test/fixtures/html/train//346/265/217/350/247/210/350/256/260/345/275/225.html +191 -0
  130. data/test/fixtures/html/train//346/267/273/345/212/240/344/270/200/347/272/247/345/210/206/347/261/273.html +113 -0
  131. data/test/fixtures/html/train//346/267/273/345/212/240/345/255/220/345/210/206/347/261/273.html +112 -0
  132. data/test/fixtures/html/train//346/267/273/345/212/240/350/257/276/347/250/213.html +195 -0
  133. data/test/fixtures/html/train//347/231/273/345/275/225.html +20 -0
  134. data/test/fixtures/html/train//347/256/241/347/220/206/345/210/206/347/261/273.html +210 -0
  135. data/test/fixtures/html/train//347/256/241/347/220/206/345/217/215/351/246/210.html +222 -0
  136. data/test/fixtures/html/train//347/256/241/347/220/206/350/257/276/347/250/213.html +284 -0
  137. data/test/fixtures/html/train//347/256/241/347/220/206/350/264/246/346/210/267.html +107 -0
  138. data/test/fixtures/html/train//347/275/221/344/270/212/345/237/271/350/256/255home/351/241/265.html +354 -0
  139. data/test/fixtures/html/train//347/275/221/345/225/206/345/237/271/350/256/255list/351/241/265.html +255 -0
  140. data/test/fixtures/html/train//350/256/276/347/275/256/351/246/226/351/241/265/346/216/250/350/215/220.html +168 -0
  141. data/test/fixtures/html/train//350/257/264/346/230/216.txt +3 -0
  142. data/test/fixtures/html/train//351/246/226/351/241/265/345/271/277/345/221/212/350/256/276/347/275/256.html +297 -0
  143. data/test/fixtures/html/unescaped.html +2 -0
  144. data/test/fixtures/html/view.vm +916 -0
  145. data/test/fixtures/js/jquery-1.7.js +9300 -0
  146. data/test/fixtures/js/scope-test.js +22 -0
  147. data/test/helper.rb +41 -0
  148. data/test/html/mixed_type_test.rb +35 -0
  149. data/test/html/parser/parse_comment_test.rb +47 -0
  150. data/test/html/parser/parse_dtd_test.rb +46 -0
  151. data/test/html/parser/parse_script_tag_test.rb +55 -0
  152. data/test/html/parser/parse_with_auto_close_tag_test.rb +41 -0
  153. data/test/html/parser/parse_with_diff_case_test.rb +38 -0
  154. data/test/html/parser/parse_with_emtpy_test.rb +22 -0
  155. data/test/html/parser/parse_with_multi_children_test.rb +27 -0
  156. data/test/html/parser/parse_with_multi_line_test.rb +41 -0
  157. data/test/html/parser/parse_with_prop_test.rb +88 -0
  158. data/test/html/parser/parse_with_script_tag_test.rb +26 -0
  159. data/test/html/parser/parse_with_selfclosing_test.rb +39 -0
  160. data/test/html/parser/parse_with_simple_tag_test.rb +44 -0
  161. data/test/html/parser/parse_with_simple_tree_test.rb +40 -0
  162. data/test/html/parser/parse_with_style_tag_test.rb +22 -0
  163. data/test/html/parser/parse_with_text_test.rb +45 -0
  164. data/test/html/parser_test.rb +52 -0
  165. data/test/html/query_test.rb +52 -0
  166. data/test/html/rule/check_block_level_element_test.rb +52 -0
  167. data/test/html/rule/check_button_test.rb +45 -0
  168. data/test/html/rule/check_class_count_test.rb +36 -0
  169. data/test/html/rule/check_css_in_head_test.rb +53 -0
  170. data/test/html/rule/check_dtd_test.rb +46 -0
  171. data/test/html/rule/check_form_element_name_test.rb +49 -0
  172. data/test/html/rule/check_head_contain_meta_and_title_test.rb +52 -0
  173. data/test/html/rule/check_html_template_test.rb +103 -0
  174. data/test/html/rule/check_hyperlink_with_target_test.rb +40 -0
  175. data/test/html/rule/check_hyperlink_with_title_test.rb +43 -0
  176. data/test/html/rule/check_id_n_class_downcase_test.rb +40 -0
  177. data/test/html/rule/check_img_with_alt_prop_test.rb +33 -0
  178. data/test/html/rule/check_no_import_css_test.rb +36 -0
  179. data/test/html/rule/check_prop_have_value_test.rb +32 -0
  180. data/test/html/rule/check_prop_seperator_test.rb +32 -0
  181. data/test/html/rule/check_style_prop_test.rb +30 -0
  182. data/test/html/rule/check_tag_closed_test.rb +59 -0
  183. data/test/html/rule/check_tag_downcase_test.rb +51 -0
  184. data/test/html/rule/check_unescape_char_test.rb +35 -0
  185. data/test/html/rule/check_unique_import_test.rb +56 -0
  186. data/test/html/rule_test.rb +62 -0
  187. data/test/js/expr/expr.rb +57 -0
  188. data/test/js/expr/left_hand.rb +25 -0
  189. data/test/js/expr/operate.rb +145 -0
  190. data/test/js/expr/primary.rb +89 -0
  191. data/test/js/parser_test.rb +98 -0
  192. data/test/js/rule/alert_check_test.rb +37 -0
  193. data/test/js/rule/all_test.rb +23 -0
  194. data/test/js/rule/base_test.rb +34 -0
  195. data/test/js/rule/file_checker_test.rb +131 -0
  196. data/test/js/rule/jq_check_test.rb +90 -0
  197. data/test/js/rule/nest_try_catch_test.rb +71 -0
  198. data/test/js/rule/new_object_and_new_array_test.rb +38 -0
  199. data/test/js/rule/no_eval_test.rb +34 -0
  200. data/test/js/rule/no_global_test.rb +88 -0
  201. data/test/js/rule/private_method_check_test.rb +58 -0
  202. data/test/js/rule/semicolon_test.rb +63 -0
  203. data/test/js/rule/stat_if_with_brace_test.rb +68 -0
  204. data/test/js/rule/stat_if_with_muti_else_test.rb +68 -0
  205. data/test/js/rule/use_strict_equal_test.rb +44 -0
  206. data/test/js/rule_test.rb +47 -0
  207. data/test/js/stat/if.rb +26 -0
  208. data/test/js/stat/iter.rb +115 -0
  209. data/test/js/stat/stat.rb +91 -0
  210. data/test/js/stat/switch.rb +37 -0
  211. data/test/js/stat/try.rb +32 -0
  212. data/test/js/stat/var.rb +38 -0
  213. data/test/parser_visitable_test.rb +102 -0
  214. data/test/position_info_test.rb +66 -0
  215. data/test/rule_dsl/dsl_basic_test.rb +91 -0
  216. data/test/rule_dsl/importing_test.rb +48 -0
  217. data/test/runner/log_level_test.rb +58 -0
  218. metadata +317 -0
@@ -0,0 +1,145 @@
1
+ module XRayTest
2
+ module JS
3
+ module Expr
4
+
5
+ module Operate
6
+
7
+ def test_parse_expr_postfix
8
+ jses = [
9
+ 'hello++',
10
+ 'i--',
11
+ 'j\n++'
12
+ ]
13
+ exprs = [
14
+ '(++,hello,)',
15
+ '(--,i,)',
16
+ 'j'
17
+ ]
18
+ add_test :parse_expr_postfix, jses, exprs
19
+ end
20
+
21
+ def test_parse_expr_unary
22
+ jses = [
23
+ '++i',
24
+ '-1',
25
+ 'delete a[hello]',
26
+ '!a'
27
+ ]
28
+ exprs = [
29
+ '(++,,i)',
30
+ '(-,,1)',
31
+ '(delete,,([,a,hello))',
32
+ '(!,,a)'
33
+ ]
34
+ add_test :parse_expr_unary, jses, exprs
35
+ end
36
+
37
+ def test_parse_expr_mul
38
+ jses = [
39
+ 'a * b / c % d * e / f'
40
+ ]
41
+ exprs = [
42
+ '(/,(*,(%,(/,(*,a,b),c),d),e),f)'
43
+ ]
44
+ add_test :parse_expr_mul, jses, exprs
45
+ end
46
+
47
+ def test_parse_expr_add
48
+ jses = [
49
+ 'a * b + c - d / e + f % d + a'
50
+ ]
51
+ exprs = [
52
+ '(+,(+,(-,(+,(*,a,b),c),(/,d,e)),(%,f,d)),a)'
53
+ ]
54
+ add_test :parse_expr_add, jses, exprs
55
+ end
56
+
57
+ def test_parse_expr_shift
58
+ jses = [
59
+ 'a << 123',
60
+ 'a + 1 >> 1 + 2',
61
+ '1 * 100 >> 2 >>> 1 + 2 * 3 - 4'
62
+ ]
63
+ exprs = [
64
+ '(<<,a,123)',
65
+ '(>>,(+,a,1),(+,1,2))',
66
+ '(>>>,(>>,(*,1,100),2),(-,(+,1,(*,2,3)),4))'
67
+ ]
68
+ add_test :parse_expr_shift, jses, exprs
69
+ end
70
+
71
+ def test_parse_expr_relation
72
+ jses = [
73
+ 'a + 1 < b',
74
+ 'a + 2 * 3 >= a++',
75
+ '++a-- > --b + c',
76
+ 'instanceofA instanceof instanceofB',
77
+ 'innameA in innameC'
78
+ ]
79
+
80
+ exprs = [
81
+ '(<,(+,a,1),b)',
82
+ '(>=,(+,a,(*,2,3)),(++,a,))',
83
+ '(>,(++,,(--,a,)),(+,(--,,b),c))',
84
+ '(instanceof,instanceofA,instanceofB)',
85
+ '(in,innameA,innameC)'
86
+ ]
87
+
88
+ add_test :parse_expr_relation, jses, exprs
89
+ end
90
+
91
+ def test_parse_expr_equal
92
+ jses = [
93
+ 'a + 1 === c',
94
+ 'a + 1 * c !== 11 + 22 < 3',
95
+ 'a + 1 < 3 == 1 << 3',
96
+ 'a << 4 - 5 == 1 << 3 === 5'
97
+ ]
98
+
99
+ exprs = [
100
+ '(===,(+,a,1),c)',
101
+ '(!==,(+,a,(*,1,c)),(<,(+,11,22),3))',
102
+ '(==,(<,(+,a,1),3),(<<,1,3))',
103
+ '(===,(==,(<<,a,(-,4,5)),(<<,1,3)),5)'
104
+ ]
105
+
106
+ add_test :parse_expr_equal, jses, exprs
107
+
108
+ end
109
+
110
+ def test_parse_expr_bit
111
+ jses = [
112
+ '1 & b + 1',
113
+ '1 & 2 ^ 3 & 4 ^ 5 + 6 & 7',
114
+ '1 + 2 | 3 + 4 & 5 ^ 6 | 7 - 8'
115
+ ]
116
+
117
+ exprs = [
118
+ '(&,1,(+,b,1))',
119
+ '(^,(^,(&,1,2),(&,3,4)),(&,(+,5,6),7))',
120
+ '(|,(|,(+,1,2),(^,(&,(+,3,4),5),6)),(-,7,8))'
121
+ ]
122
+
123
+ add_test :parse_expr_bit_or, jses, exprs
124
+ end
125
+
126
+
127
+ def test_parse_expr_logical
128
+ jses = [
129
+ 'a + 1 == 0 && b - 1 > 3',
130
+ 'a + 1 == 0 && b === c || a - b == c - d && c > 0'
131
+ ]
132
+
133
+ exprs = [
134
+ '(&&,(==,(+,a,1),0),(>,(-,b,1),3))',
135
+ '(||,(&&,(==,(+,a,1),0),(===,b,c)),(&&,(==,(-,a,b),(-,c,d)),(>,c,0)))'
136
+ ]
137
+
138
+ add_test :parse_expr_logical_or, jses, exprs
139
+ end
140
+
141
+ end
142
+
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,89 @@
1
+ module XRayTest
2
+ module JS
3
+ module Expr
4
+
5
+ module Primary
6
+
7
+ def test_parse_expr_parentheses
8
+ expr = parse_js :parse_expr_parentheses, '(12.56)'
9
+
10
+ assert_equal 'parentheses', expr.type
11
+ assert_equal '12.56', expr.node.text
12
+ end
13
+
14
+ def test_parse_expr_array
15
+ js = '[123, 456, "hello world", /this is re/]'
16
+ expr = parse_js :parse_expr_array, js
17
+
18
+ assert_equal 'array', expr.type
19
+ assert_equal ['123', '456', '"hello world"', '/this is re/'],
20
+ expr.node.elements.collect(&:text)
21
+ end
22
+
23
+ def test_parse_expr_object
24
+ js = '{
25
+ name: "hello 1234",
26
+ "key 2": 123.567e12,
27
+ 12.59: 12.58,
28
+ 18: /hello/
29
+ }'
30
+
31
+ expr = parse_js :parse_expr_object, js
32
+
33
+ assert_equal 'object', expr.type
34
+
35
+ assert_equal ['name', '"key 2"', '12.59', '18'],
36
+ expr.node.elements.collect(&:left).collect(&:text)
37
+
38
+ assert_equal ['"hello 1234"', '123.567e12', '12.58', '/hello/'],
39
+ expr.node.elements.collect(&:right).collect(&:text)
40
+ end
41
+
42
+ def test_parse_expr_literal
43
+ jses = [
44
+ 'this.a = 123',
45
+ 'null === a',
46
+ 'true === false',
47
+ 'false === true',
48
+
49
+ '123 == a',
50
+ '.12 = b',
51
+ '0.123 = c',
52
+ '+123e+1 = d',
53
+ '123.123e-1 = e',
54
+ '12345.123e123',
55
+
56
+ '/\\\\/g,a = "bcd"'
57
+ ]
58
+
59
+ str1 = %q("hello this is a single line str\\"ing")
60
+ str2 = %q('hello this is a \'single line string')
61
+
62
+ str3 = '"hello this is an mutiline string\\
63
+ hello this is an mutiline string\\
64
+ hello this is an mutiline string"'
65
+
66
+ re1 = %q{/((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g}
67
+
68
+ re2 = %q{/\/\*[^*]*\*+([^/][^*]*\*+)*\//gm}
69
+
70
+ jses << str1 << str2 << str3 << re1 << re2
71
+
72
+ exprs = jses.collect do |js|
73
+ parse_js :parse_expr_primary, js
74
+ end
75
+
76
+ eqs = %w(
77
+ this null true false
78
+ 123 .12 0.123 +123e+1 123.123e-1 12345.123e123
79
+ /\\\\/g
80
+ )
81
+ eqs << str1 << str2 << str3 << re1 << re2
82
+ assert_equal eqs, exprs.collect(&:node).collect(&:text)
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../helper', File.dirname(__FILE__))
3
+
4
+ require 'js/parser'
5
+
6
+ require_relative 'expr/expr'
7
+ require_relative 'stat/stat'
8
+
9
+
10
+ module XRayTest
11
+ module JS
12
+ class ParserTest < Test::Unit::TestCase
13
+ include XRay::JS
14
+
15
+ include Expr::Expr
16
+ include Stat::Stat
17
+
18
+
19
+ def test_parse_program()
20
+ js = '
21
+ /**
22
+ * this is mutiline comment
23
+ */
24
+ function say(name1, name2/*inline comment*/, name3) {
25
+ alert(name + " hello world");
26
+ console.debug(name2);
27
+ name1 + name2 + // line comment
28
+ name3;
29
+ }
30
+ ; // hello this is comment
31
+ var a = 1;
32
+ a++;
33
+ '
34
+ parser = create_parser(js)
35
+ program = parser.parse_program
36
+ elms = program.elements
37
+
38
+ assert_equal 4, elms.length
39
+
40
+ s_comments = parser.singleline_comments
41
+ assert_equal 2, s_comments.length
42
+
43
+ m_comments = parser.mutiline_comments
44
+ assert_equal 2, m_comments.length
45
+ end
46
+
47
+ def test_parse_function_declaration
48
+ js = '
49
+ function sum(a1, a2, a3) {
50
+ a1 = a2 + a3;
51
+ a2 = a2 * a3;
52
+ a3++;
53
+ }
54
+ '
55
+
56
+ parser = create_parser(js)
57
+ func = parser.parse_function_declaration
58
+
59
+ assert_equal 'sum', func.name.text
60
+ assert_equal '[a1,a2,a3]', func.parameters.text
61
+ end
62
+
63
+ def test_parse_singleline_comment
64
+ js = '
65
+ // this is simple comment
66
+ a = 123;
67
+ '
68
+
69
+ comment = parse_js :parse_singleline_comment, js
70
+ assert_equal '// this is simple comment', comment.text
71
+ end
72
+
73
+
74
+ def create_parser(js)
75
+ Parser.new js, XRayTest::Logger.new
76
+
77
+ end
78
+
79
+ def parse_js(action, js)
80
+ parser = create_parser js
81
+ parser.send(action)
82
+ end
83
+
84
+ def add_test(action, jses, exprs)
85
+ if jses.class == String
86
+ jses = [jses]
87
+ exprs = [exprs]
88
+ end
89
+ jses.each_with_index do |js, index|
90
+ expr = parse_js action, js
91
+ assert_equal exprs[index], expr.text
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ require_relative 'base_test'
3
+
4
+ require 'js/rule/checklist'
5
+
6
+ module XRayTest
7
+ module JS
8
+ module Rule
9
+
10
+ class AlertCheckTest < BaseTest
11
+
12
+ def test_visit_alert
13
+ js = 'alert'
14
+ ret = visit js
15
+ assert_equal 1, ret.length
16
+ end
17
+
18
+ def test_vist_window_alert
19
+ js = "window.alert"
20
+ ret = visit js
21
+ assert_equal [], ret
22
+ end
23
+
24
+ private
25
+
26
+ def visit(js)
27
+ expr = parse js, 'expr_member'
28
+ rule = XRay::JS::Rule::ChecklistRule.new
29
+ rule.send :visit_expr_member, expr
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,23 @@
1
+ require_relative '../../helper'
2
+
3
+ require 'js/rule/all'
4
+
5
+ module XRayTest
6
+ module JS
7
+ module Rule
8
+
9
+ class AllTest < Test::Unit::TestCase
10
+
11
+ def test_initialize
12
+ all = XRay::JS::Rule::All.new
13
+
14
+ assert all.respond_to?(:each)
15
+ assert all.size > 0
16
+ assert all.size == all.rules.size
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,34 @@
1
+ require_relative '../../helper'
2
+
3
+ require 'js/parser'
4
+ require 'parser_visitable'
5
+
6
+ module XRayTest
7
+
8
+ module JS
9
+ module Rule
10
+
11
+ class VisitableParser < XRay::JS::Parser
12
+ include XRay::ParserVisitable
13
+ end
14
+
15
+ class BaseTest < Test::Unit::TestCase
16
+
17
+ def parse(js, action = 'parse_program')
18
+ parser = XRay::JS::Parser.new js, XRayTest::Logger.new
19
+ parser.send "parse_#{action}"
20
+ end
21
+
22
+ def parse_with_rule(js, rule, options = {})
23
+ parser = VisitableParser.new js, XRayTest::Logger.new
24
+ rule = rule.new if rule.is_a? Class
25
+ parser.add_visitor rule
26
+ parser.parse_program
27
+ parser.results
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,131 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative 'base_test'
4
+
5
+ require 'js/rule/file_checker'
6
+
7
+ module XRayTest
8
+ module JS
9
+ module Rule
10
+
11
+ class FileCheckerTest < BaseTest
12
+
13
+ def setup
14
+ @checker = XRay::JS::Rule::FileChecker.new
15
+ end
16
+
17
+ def test_merge_file?
18
+ assert @checker.merge_file?('abc-merge.js')
19
+ assert @checker.merge_file?('abc-merge-12345.js')
20
+ assert @checker.merge_file?('abc-merge12345.js')
21
+
22
+ assert !@checker.merge_file?('abc-merge.css')
23
+ assert !@checker.merge_file?('abc-merge.jss')
24
+ assert !@checker.merge_file?('-merge.js')
25
+ assert !@checker.merge_file?('abc-mergeabc.js')
26
+ assert !@checker.merge_file?('abc-merge-.js')
27
+ end
28
+
29
+ def test_min_file?
30
+ assert @checker.min_file?('abc-min.js')
31
+
32
+ assert !@checker.min_file?('abc-min.jss')
33
+ assert !@checker.min_file?('-min.js')
34
+ assert !@checker.min_file?('abc-min1.js')
35
+ end
36
+
37
+ def test_grep_import_js_path
38
+ pathes = @checker.grep_import_js_path fixture_01
39
+ assert_equal 6, pathes.length
40
+ assert_equal %w(
41
+ http://style.china.alibaba.com/js/common/aliclick.js
42
+ http://style.china.alibaba.com/js/detail/xpclick.js
43
+ http://style.china.alibaba.com/js/lib/fdev-v4/widget/util/debug-min.js
44
+ http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/alitalk-min.js
45
+ http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/sweet-min.js
46
+ http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/sweet-min.js
47
+ ), pathes.collect { |path| path[:url] }
48
+ end
49
+
50
+ def test_check_one_line_one_import
51
+ pathes = @checker.grep_import_js_path fixture_01
52
+ ret = []
53
+ pathes.each { |import|
54
+ r = @checker.check_js_merge_importing import, pathes, ""
55
+ ret.concat r
56
+ }
57
+ assert_equal [
58
+ ["merge文件需要引用压缩版的js, 如a-min.js", :warn, 12, 63],
59
+ ["merge文件需要引用压缩版的js, 如a-min.js", :warn, 13, 62],
60
+ ["一行只能有一个import文件", :error, 18, 96],
61
+ ["merge文件格式不正确", :error, 19, 1],
62
+ ["merge文件格式不正确", :error, 20, 1]
63
+ ], ret
64
+ end
65
+
66
+ def test_has_doc_comment
67
+ js = '
68
+ /**
69
+ * 功能文件头必须要有文档注释
70
+ * 现在没有对doc格式进行规范
71
+ * 所以暂时不做文档注释内容检查
72
+ * 只检则有注释
73
+ */
74
+
75
+ i++;
76
+ '
77
+ assert @checker.has_doc_comment?(js)
78
+
79
+ js = '
80
+ i++;
81
+ '
82
+ assert !@checker.has_doc_comment?(js)
83
+
84
+ js = '
85
+ /*
86
+ * 多行注释不属于文档注释
87
+ * 文档注释需要以/*开头
88
+ */
89
+ '
90
+ assert !@checker.has_doc_comment?(js)
91
+ end
92
+
93
+ def test_check_import_js_scope
94
+ #not implements
95
+ end
96
+
97
+ def test_check_import_js_exist_and_min
98
+ # not implements
99
+ end
100
+
101
+ def fixture_01
102
+ <<END
103
+ (function(){
104
+ ImportJavscript = {
105
+ url:function(url){
106
+ document.write('<script type="text/javascript" src="'+url+'"></scr'+'ipt>');
107
+ }
108
+ };
109
+ })();
110
+
111
+ /**
112
+ * 打点依赖库
113
+ */
114
+ ImportJavscript.url('http://style.china.alibaba.com/js/common/aliclick.js');
115
+ ImportJavscript.url(http://style.china.alibaba.com/js/detail/xpclick.js);
116
+
117
+ /**
118
+ * 旺铺基础JS
119
+ */
120
+ ImportJavscript.url("http://style.china.alibaba.com/js/lib/fdev-v4/widget/util/debug-min.js"); ImportJavscript.url("http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/alitalk-min.js");
121
+ ImportJavscript.url( 'http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/sweet-min.js' ) ;
122
+ ImportJavscript.url('http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/sweet-min.js')
123
+ END
124
+ end
125
+
126
+ end
127
+
128
+ end
129
+ end
130
+ end
131
+
@@ -0,0 +1,90 @@
1
+ # encoding: utf-8
2
+ require_relative 'base_test'
3
+
4
+ require 'js/rule/checklist'
5
+
6
+ module XRayTest
7
+ module JS
8
+ module Rule
9
+
10
+ class JqCheckTest < BaseTest
11
+ def test_visit_expr_member
12
+ js = 'jQuery.noConflict'
13
+ ret = visit js
14
+ assert_equal 2, ret.length
15
+ end
16
+
17
+ def test_check_direct_jquery_call
18
+ js = "jQuery.namespace"
19
+ ret = visit js
20
+ assert_equal [], ret
21
+
22
+ jses = [
23
+ 'jQuery.bind',
24
+ 'jQuery[method]',
25
+ 'jQuery(function($) {})',
26
+ 'jQuery.extend'
27
+ ]
28
+
29
+ jses.each do |js|
30
+ r = visit js
31
+ assert r.include? ['禁止直接使用jQuery变量,使用全局闭包写法"(function($, NS){....})(jQuery,Namespace);",jQuery.namespace例外', :error]
32
+ end
33
+ end
34
+
35
+ def test_check_forbit_method_call
36
+ jses = %w(
37
+ $.sub
38
+ $.noConflict
39
+ jQuery.sub
40
+ jQuery.noConflict
41
+ )
42
+
43
+ jses.each do |js|
44
+ r = visit js
45
+ assert r.include?( ['禁止使用jQuery.sub()和jQuery.noConflict方法', :error] )
46
+ end
47
+ end
48
+
49
+ def test_check_data_call_param
50
+ jses = %w(
51
+ $.data("doc-config")
52
+ jQuery.data('doc-config')
53
+ )
54
+ jses.each do |js|
55
+ r = visit js
56
+ assert r.include? ['使用".data()"读写自定义属性时需要转化成驼峰形式', :error]
57
+ end
58
+ end
59
+
60
+ def test_check_ctor_selector
61
+ jses = [
62
+ "$('.myclass', div)",
63
+ "$('.myclass')",
64
+ "$('[name=123]')",
65
+ "$(':first')"
66
+ ]
67
+
68
+ jses.each do |js|
69
+ r = visit js
70
+ assert r.include? ['使用选择器时,能确定tagName的,必须加上tagName', :warn]
71
+ end
72
+
73
+ js = '$()'
74
+ ret = visit js
75
+ assert_equal [], ret
76
+ end
77
+
78
+ private
79
+
80
+ def visit(js)
81
+ expr = parse js, 'expr_member'
82
+ rule = XRay::JS::Rule::ChecklistRule.new
83
+ rule.send :visit_expr_member, expr
84
+ end
85
+
86
+ end
87
+
88
+ end
89
+ end
90
+ end